我是Redux的新手,在尝试制作基本任务清单时通读了文档。
我似乎无法让我的减速器在列表中添加一个项目。正确的动作创建者正在开除,我认为我的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;
Run Code Online (Sandbox Code Playgroud)
谢谢!