Reactjs redux reducer未更新状态
我是Redux的新手,在阅读文档的同时,我尝试制作一个基本的待办事项列表 我似乎无法让我的减速机在列表中添加项目。正确的动作创建者正在触发,我想我的Reactjs redux reducer未更新状态,reactjs,redux,Reactjs,Redux,我是Redux的新手,在阅读文档的同时,我尝试制作一个基本的待办事项列表 我似乎无法让我的减速机在列表中添加项目。正确的动作创建者正在触发,我想我的对象中可能有什么东西。赋值语句我不理解。下面是我的store.js文件 const defaultState = { todos:[ { text: 'walk gilbert' }, { text: 'cook dinner' }, { text: 'clean bathroom' } ]
对象中可能有什么东西。赋值语句我不理解。下面是我的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;
谢谢 看起来您对组合减速机的工作原理有点困惑
combineReducers
实用程序旨在定义状态树对象中的字段或“切片”,并将更新这些切片的工作委托给特定函数。在您的情况下,看起来您真的只是想要一个状态.todos
切片,但是调用combineReducers()
的方式实际上是创建状态.todos
和状态.modifyList
。此外,当您使用组合减速器时,每个切片减速器只能看到其整体状态树的一部分。换句话说,在todos()
reducer内部,state
参数就是todos
部分
所以,你想要的是更像这样的东西:
const defaultTodosState = [
{text : 'walk gilbert'},
{text : "cook dinner"},
{text : "clean bathroom"}
];
function todos(state = defaultTodosState, action) {
switch(action.type) {
case 'ADD_TODO': {
return [
...state,
{text: action.text}
]
}
default:
return state;
}
}
const rootReducer = combineReducers({todos});
您可能想通读Redux文档中讨论组合减速机和减速机的部分:、、、。您能提供完整的代码吗?你要把行动派往哪里?我试图重现这个问题,但我无能为力。我的回购协议已到期