Skip to content

Instantly share code, notes, and snippets.

@IrakliJani
Created July 30, 2016 19:01
Show Gist options
  • Save IrakliJani/fa456a5bfea04d56808a8b77e486b262 to your computer and use it in GitHub Desktop.
Save IrakliJani/fa456a5bfea04d56808a8b77e486b262 to your computer and use it in GitHub Desktop.
Combine reducers
const rootReducer = combineReducers({
user: usersReducer,
task: combineReducers({
list: tasksReducer,
current: currentTaskReducer
})
})
function usersReducer (state = Map(), action) {
[...]
}
function tasksReducer (state = Map(), action) {
switch (action.type) {
case ADD_TASK:
return state.set(action.task.id, action.task)
case REMOVE_TASK:
return state.delete(action.id)
case COMPLETE_TASK:
return state.updateIn([action.id, 'completed'], true)
default:
return state
}
}
function currentTaskReducer (state = null, action) {
switch (action.type) {
case SET_CURRENT_TASK:
return state = action.id
case UNSET_CURRENT_TASK:
return state = null
default:
return state
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment