【问题标题】:How to properly update the reducer如何正确更新reducer
【发布时间】:2020-09-03 15:07:41
【问题描述】:

在我最初的状态下,我有板,将它们视为聊天室中的组。 setboard 是一个变量,用于在房间之间切换使用

activeBoard. =>

activeBoard: state.boards[initialState.setBoard],

I map the content of debates easily. The problem comes when I try to update the reducer.

const initialState = {
    setBoard: 'Feed',
    boards : {
        Feed: {
            id: 1,
            debates: [
                {
                    id: 1,
                    text: 'This is the most amazing website on the internet.",
                    Images: 'soul.jpg',
                },
                {
                    id: 2,
                    topic: 'Somebody tell him to shut up',
                    text: "This is the most amazing website on the internet.",
                    Images: 'salt.jpg',
                },
            ],
            invitations: [
                {
                    id: 1,
                    nickname: "imhotep",
                    inviteText: ' BLOCKING People block or unfriend their parents on Facebook 
                },
            ],
     }
export const BoardProvider = ({ children}) =>{
    const [state, dispatch] = useReducer(BoardReducer, initialState) 

    function AddDebates(debates){
        dispatch({
            type: 'Add-debates',
            payload: debates
        })
    }
return ( <BoardContext.Provider value={{
        boards: state.boards,
        
        activeBoard: state.boards[initialState.setBoard],
        debates: activeBoard.debates,
        AddDebates
    }}>
        {children}
    </BoardContext.Provider>)
}

This is my reducer.

export default ( state, action, activeBoard, debates,invitations) =>{
    switch(action.type) {
       case 'Add-debates':
            return { 
                ...state, 
                debates: [action.payload, ...debates]
            }
  default: 
            return state
          
    }
}

我收到一个错误:TypeError:debates is not iterable 我可以通过简单地映射它来呈现辩论,但可以通过这种方式更新减速器。一些帮助请...

【问题讨论】:

  • 看来你只需要将reducer中的...debates替换为...state.debates
  • 这个我试过了,也没用

标签: reactjs redux react-hooks


【解决方案1】:

您需要将 所有 级别的状态从根目录浅复制到您正在更新的 debates,因为它嵌套了几个级别。正确的引用还将包括该属性的完整路径,即state.debates 未定义且不可可迭代。

case 'Add-debates':
  return { 
    ...state, 
    boards: {
      ...state.boards,
      Feed: {
        ...state.boards.Feed,
        debates: [action.payload, ...state.boards.Feed.debates],
      },
    },
  }

【讨论】:

  • 感谢您的回复,我没有收到任何错误,但它没有工作。另外,我正在尝试使用activeBoard在房间/董事会之间切换,因为有几个,我只是复制了其中一个。
  • @Imhotep 你能澄清什么不起作用吗?在您的AddDebates 动作创建者中,您将debates 传递给payload,但在reducer 中它被视为单个“辩论”元素。传递给动作创建者的debates 是什么?这个名字暗示了不止一个“辩论”,比如它们的数组,在这种情况下,它们也需要在减速器中传播到状态,即debates: [...action.payload, ...state.boards.Feed.debates]。 IMO action.payload 不是一个好名字,因为它没有传达值是什么或如何与之交互,我建议使用参数名称“debates”。
  • action.payload 是一个包含一些字符串和图像的对象
  • 我对 reducer 很陌生,我只是假设 action.payload 是一个约定
  • 当我在动作上使用扩展符号时。有效载荷,我得到一个错误,不可迭代
猜你喜欢
  • 2019-07-05
  • 1970-01-01
  • 2016-07-02
  • 2018-03-12
  • 2017-12-20
  • 2013-02-07
  • 2018-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多