Created
April 11, 2018 05:12
-
-
Save agoldis/f5d1a42794de799cdeec63e78a469160 to your computer and use it in GitHub Desktop.
redux blog - initialState with reselect
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
| import { createSelector } from "reselect"; | |
| const initialState = { | |
| app: { | |
| users: [{ id: "user01", name: "John" }, { id: "user02", name: "Bob" }], | |
| tasks: [ | |
| { id: "task01", title: "Wash car", assignee: "user01" }, | |
| { id: "task02", title: "Watch tutorial", assignee: "user01" }, | |
| { id: "task03", title: "Do Homework", assignee: "user02" } | |
| ] | |
| }, | |
| ui: { /* ... */ } | |
| } | |
| const getUsers = state => state.app.users; | |
| const getTasks = state => state.app.tasks; | |
| const getUserTasks = createSelector([getUsers, getTasks], (users, tasks) => { | |
| return users.map(user => | |
| Object.assign({}, user, { | |
| tasks: tasks.filter(task => task.assignee === user.id) | |
| }) | |
| ); | |
| }); | |
| function userTasksCount() { | |
| return this.userTasks.map(user => | |
| Object.assign({}, user, { tasksCount: user.tasks.count }) | |
| ); | |
| } | |
| Object.defineProperty(initialState, "userTasks", { | |
| get: function() { | |
| return getUserTasks(this); | |
| }, | |
| enumerable: false | |
| }); | |
| Object.defineProperty(initialState, "userTasksCount", { | |
| get: userTasksCount, | |
| enumerable: false | |
| }); | |
| export default initialState; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment