小编Ant*_*ery的帖子

Redux Reducer不更新状态

我是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)

谢谢!

reactjs redux

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

reactjs ×1

redux ×1