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
| // Users list component mapper | |
| const mapStateToProps = ({ users, comments }) => ({ | |
| users: users.map(user => ({ | |
| ...user, | |
| booksCount: user.books.length, | |
| commentsCount: comments.filter(c => c.user === user.id).length | |
| })) | |
| }); | |
| // Books list component mapper |
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
| /* initialState.js */ | |
| const books = [ /* ... */ ] | |
| const users = [ /* ... */ ] | |
| const comments = [ /* ... */ ]; | |
| const usersSummary = function() { | |
| return this.users.map(user => ({ | |
| ...user, | |
| booksCount: user.books.length, | |
| commentsCount: this.comments.filter(c => c.user === user.id).length |
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
| /* initialState.js */ | |
| const books = [ /* ... */ ] | |
| const users = [ /* ... */ ] | |
| const comments = [ /* ... */ ]; | |
| const usersSummary = function() { | |
| return this.users.map(user => ({ | |
| ...user, | |
| booksCount: user.books.length, | |
| commentsCount: this.comments.filter(c => c.user === user.id).length |
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
| const deriveState = state => { | |
| state.foo = "bar"; | |
| return state; | |
| } | |
| const enhanceStore = createStore => (reducer, preloadedState, enhancer) => { | |
| const store = createStore(reducer, preloadedState, enhancer); | |
| const _originalGetState = store.getState; | |
| store.getState = () => deriveState(_originalGetState()); | |
| return store; |
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 { createStore, applyMiddleware, compose } from "redux"; | |
| import { initialState } from "./initialState"; | |
| import { reducer } from "./reducer"; | |
| import { enhanceStore } from "./storeEnhancer"; | |
| const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose; | |
| export const store = createStore( | |
| reducer, | |
| initialState, |
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 { compose } from "redux"; | |
| const usersSummary = function() { | |
| return this.users.map(user => ({ | |
| ...user, | |
| booksCount: user.books.length, | |
| commentsCount: this.comments.filter(c => c.user === user.id).length | |
| })); | |
| }; |
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 { deriveState } from "./deriveState"; | |
| export const enhanceStore = createStore => ( | |
| reducer, | |
| preloadedState, | |
| enhancer | |
| ) => { | |
| const store = createStore(reducer, preloadedState, enhancer); | |
| const _getState = store.getState; | |
| store.getState = () => deriveState(_getState()); |
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 { compose } from "redux"; | |
| const usersSummary = function() { | |
| return this.users.map(user => ({ | |
| ...user, | |
| booksCount: user.books.length, | |
| commentsCount: this.comments.filter(c => c.user === user.id).length | |
| })); | |
| }; |
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
| /* other derived state methods */ | |
| const moveToApp = state => | |
| Object.defineProperties(state, { | |
| users: { | |
| get: function() { | |
| return this.app.users; | |
| }, | |
| enumerable: false, | |
| configurable: true |