Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs redux reducer未更新状态_Reactjs_Redux - Fatal编程技术网

Reactjs redux reducer未更新状态

Reactjs redux reducer未更新状态,reactjs,redux,Reactjs,Redux,我是Redux的新手,在阅读文档的同时,我尝试制作一个基本的待办事项列表 我似乎无法让我的减速机在列表中添加项目。正确的动作创建者正在触发,我想我的对象中可能有什么东西。赋值语句我不理解。下面是我的store.js文件 const defaultState = { todos:[ { text: 'walk gilbert' }, { text: 'cook dinner' }, { text: 'clean bathroom' } ]

我是Redux的新手,在阅读文档的同时,我尝试制作一个基本的待办事项列表

我似乎无法让我的减速机在列表中添加项目。正确的动作创建者正在触发,我想我的
对象中可能有什么东西。赋值
语句我不理解。下面是我的
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文档中讨论组合减速机和减速机的部分:、、、。

您能提供完整的代码吗?你要把行动派往哪里?我试图重现这个问题,但我无能为力。我的回购协议已到期