From 02222139e56ca09ed54ce5c1042ef98aa6f58883 Mon Sep 17 00:00:00 2001 From: Nathan Wenneker Date: Fri, 6 Nov 2015 11:24:01 -0700 Subject: [PATCH] proof of concept for reselect without connect --- containers/App.js | 20 +++++++++++++++++--- package.json | 3 ++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/containers/App.js b/containers/App.js index 599defd..f078a29 100644 --- a/containers/App.js +++ b/containers/App.js @@ -4,7 +4,8 @@ import { addTodo, completeTodo, setVisibilityFilter, changeTheme, VisibilityFilt import AddTodo from '../components/AddTodo' import TodoList from '../components/TodoList' import Footer from '../components/Footer' -import { memoize, createMemoizedFunction } from '../memoize' +// import { memoize, createMemoizedFunction } from '../memoize' +import { createSelector } from 'reselect' function selectTodos(todos, filter) { console.log("Recalculating selectTodos"); @@ -23,14 +24,27 @@ function selectMatchingTodos(todos, search) { return todos.filter((todo) => { return todo.text.search(search) >= 0; }); } +const visibleTodosSelector = createSelector( + [(component) => component.props.todos, (component) => component.props.visibilityFilter], + selectTodos +); + +const matchingTodosSelector = createSelector( + [(component) => component.visibleTodos(), (component) => component.state.search], + selectMatchingTodos +); + class App extends Component { constructor(props, context) { super(props, context); this.state = { search: '' }; } - visibleTodos = createMemoizedFunction(() => [this.props.todos, this.props.visibilityFilter], selectTodos); - matchingTodos = createMemoizedFunction(() => [this.visibleTodos(), this.state.search], selectMatchingTodos); + // visibleTodos = createMemoizedFunction(() => [this.props.todos, this.props.visibilityFilter], selectTodos); + // matchingTodos = createMemoizedFunction(() => [this.visibleTodos(), this.state.search], selectMatchingTodos); + + visibleTodos = visibleTodosSelector.bind(this, this); + matchingTodos = matchingTodosSelector.bind(this, this); updateSearch = function(e) { this.setState({ search: e.target.value }); diff --git a/package.json b/package.json index 5365197..0c51d5f 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "react": "^0.14.0", "react-dom": "^0.14.0", "react-redux": "^4.0.0", - "redux": "^3.0.0" + "redux": "^3.0.0", + "reselect": "^2.0" }, "devDependencies": { "babel-core": "^5.6.18",