Created
May 27, 2016 09:38
-
-
Save ryo33/aab63c300556616d2514f6541d4d9a0f to your computer and use it in GitHub Desktop.
combineReducersが辛い ref: http://qiita.com/ryo33/items/4f50ca9dd38ad47dc3ea
This file contains 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
// State | |
{ | |
todos: { | |
todoA: [], | |
todoB: [], | |
todoC: [] | |
}, | |
currentTodoKey: "todoA" | |
} | |
// Reducer | |
const reducer = (state = { todos: {}, currentTodoKey: "default" }, action) => { | |
const addTodo = (state, key, todo) { | |
return Object.assign({}, state, { | |
todos: Object.assign({}, state.todos, { | |
[key]: (state.todos[key] || []).concat(todo) | |
}) | |
}) | |
} | |
switch (action.type) { | |
case 'ADD_TODO': | |
var { key, todo } = action.payload | |
return addTodo(state, key, todo) | |
case 'ADD_TODO_TO_CURRENT_KEY': | |
var { todo } = action.payload | |
return addTodo(state, state.currentTodoKey, todo) | |
case 'UPDATE_CURRENT_KEY': | |
var { newKey } = action.payload | |
return Object.assign({}, state, { | |
currentTodoKey: newKey | |
}) | |
default: | |
return state | |
} | |
} |
This file contains 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 todos = (state = {}, action, entireState) => { | |
switch (action.type) { | |
case 'ADD_TODO': | |
var { key, todo } = action.payload | |
return Object.assign({}, state, { | |
[key]: (state[key] || []).concat(todo) | |
}) | |
case 'ADD_TODO_TO_CURRENT_KEY': | |
var { todo } = action.payload | |
var key = entireState.currentTodoKey | |
return Object.assign({}, state, { | |
[key]: (state[key] || []).concat(todo) | |
}) | |
default: | |
return state | |
} | |
} | |
const currentTodoKey = (state = "default", action) => { | |
switch (action.type) { | |
case 'UPDATE_CURRENT_KEY': | |
var { newKey } = action.payload | |
return newKey | |
default: | |
return state | |
} | |
} | |
const reducer = (state, action) => ({ | |
todos: todos(state.todos, action, state), | |
currentTodoKey: currentTodoKey(state.currentTodoKey, action) | |
}) |
This file contains 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 reducer = (state, action) => ({ | |
todos: todos(state.todos, action, state), | |
currentTodoKey: currentTodoKey(state.currentTodoKey, action) | |
}) |
This file contains 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 combineSectionReducers from 'combine-section-reducers' | |
const reducer = combineSectionReducers({ | |
todo, currentTodoKey | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment