Skip to content

Instantly share code, notes, and snippets.

@oreqizer
Last active July 6, 2017 11:48
Show Gist options
  • Save oreqizer/cb979f1de7b931834c90bd385b00cd05 to your computer and use it in GitHub Desktop.
Save oreqizer/cb979f1de7b931834c90bd385b00cd05 to your computer and use it in GitHub Desktop.
An example of a selector.
import { createSelector } from 'reselect';
// sub-selectors
const allTodoSelector = state => state.todo.todos; // an array of todos
const filterSelector = (_, props) => props.params.filter; // a url parameter
// a selector
const todoSelector = createSelector(
[allTodoSelector, filterSelector],
(todos, filter = 'all') => {
switch (filter) {
case 'all':
return todos;
case 'active':
return todos.filter(todo => !todo.done);
case 'done':
return todos.filter(todo => todo.done);
default:
return todos;
}
},
);
export default todoSelector;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment