javascript - React redux logic done in parent reducer vs. child reducer -
in react redux doc todo example, dan passes action type toggle_todo
todos
passes on each individual todo. notice logic checking todo.id
in todo
reducer. couldn't logic have been done in todos
well? me, seem better take care of logic @ higher level iterating through each todo rather passing work every todo , having them figure out if need toggle or now. there reason why dan did way?
const todo = (state = {}, action) => { switch (action.type) { case 'add_todo': return { id: action.id, text: action.text, completed: false } case 'toggle_todo': if (state.id !== action.id) { return state } return object.assign({}, state, { completed: !state.completed }) default: return state } } const todos = (state = [], action) => { switch (action.type) { case 'add_todo': return [ ...state, todo(undefined, action) ] case 'toggle_todo': return state.map(t => todo(t, action) ) default: return state } } export default todos
i think right, if take @ todomvc example redux source code repository, you'll see 1 todos
reducer.
the docs may bit outdated or such nested reducers may example of possibilities.
Comments
Post a Comment