Created
June 5, 2016 07:44
-
-
Save ZhihaoLau/f233c25e57795808af6b7818b261dd5b to your computer and use it in GitHub Desktop.
implement Redux's combineReducers from scratch
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
// accept a array of reducers | |
const combineReducers = (reducers) => { | |
// returns a huge reducer, looks like below gist | |
return (state = {}, action) => { | |
return Object.keys(reducers).reduce( | |
(nextState, key) => { | |
nextState[key] = reducers[key]( | |
state[key], | |
action | |
) | |
return nextState | |
}, | |
{} | |
) | |
} | |
} |
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
// a huge reducer looks like this | |
const todoApp = (state = {}, action) => { | |
// 'huge reducer also returns a huge state' | |
return { | |
todos: todosReducer( | |
state.todos, | |
action | |
), | |
visibilityFilter: VisibilityFilter( | |
state.visibilityFilter, | |
action | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
check Dan's redux tutorial videos