Created
January 18, 2019 13:03
-
-
Save talyssonoc/5fa623e0df9446bac661fa752668d03e to your computer and use it in GitHub Desktop.
Before and after using a Redux selector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* view/todo/TodoList.js */ | |
const TodoList = ({ todos, filter }) => ( | |
<ul> | |
{ | |
todos | |
.filter((todo) => todo.state === filter) | |
.map((todo) => | |
<li key={todo.id}>{ todo.text }</li> | |
) | |
} | |
</ul> | |
); | |
const mapStateToProps = ({ todos, filter }) => ({ | |
todos, | |
filter | |
}); | |
export default connect(mapStateToProps)(TodoList); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* state/todos.js */ | |
import * as Todo from '../domain/todo'; | |
export const getTodosByFilter = (todos, filter) => ( | |
// notice that we isolate the domain rule into the domain/todo entity | |
// so if the shape of the todo object changes it will only affect our entity file, not here :) | |
todos.filter((todo) => Todo.hasState(todo, filter)) | |
); | |
// --------------------------------- | |
/* view/todo/TodoList.js */ | |
import { getTodosByFilter } from '../../state/todos'; | |
const TodoList = ({ todos }) => ( | |
<ul> | |
{ | |
todos | |
.map((todo) => | |
<li key={todo.id}>{ todo.text }</li> | |
) | |
} | |
</ul> | |
); | |
const mapStateToProps = ({ todos, filter }) => ({ | |
todos: getTodosByFilter(todos, filter) | |
}); | |
export default connect(mapStateToProps)(TodoList); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment