【发布时间】:2017-05-23 03:57:25
【问题描述】:
我是 Redux 的新手,在尝试制作基本的待办事项列表时通读文档。
我似乎无法让我的 reducer 将项目添加到列表中。正确的动作创建者正在解雇,我认为我的Object.assign 声明中可能有一些我不理解的东西。以下是我的store.js 文件。
const defaultState = {
todos:[
{
text: 'walk gilbert'
},
{
text: 'cook dinner'
},
{
text: 'clean bathroom'
}
]
}
function todos(state = defaultState) {
return state;
}
function modifyList(state = defaultState, action) {
switch(action.type) {
case 'ADD_TODO':
return Object.assign({}, state, {
todos: [
...state.todos,
{
text: action.text,
}
]
})
default:
return state;
}
}
const rootReducer = combineReducers({todos, modifyList})
const store = createStore(rootReducer, defaultState);
export default store;
谢谢!
【问题讨论】:
-
能否提供完整代码?你在哪里调度动作?我试图重现该问题,但我无法。 codepen.io/olivercs/pen/dWwzpj?editors=0012