简体   繁体   English

我是 redux 的新手并做出反应,我正在尝试更新 REDUX 商店中的嵌套 state,但无法对其进行整理

[英]i am new to redux and react i am trying to update the nested state in REDUX store, but unable to sort it out

where am i missing?我在哪里失踪? please help in sorting this out _/_ i have trouble in updating the redux store state correctly without breaking请帮助解决这个问题_/_ 我在更新 redux 商店 state 时遇到问题而不会损坏

This is my current state in redux store state before posting the data这是我在发布数据之前在 redux 商店 state 中的当前 state

next state Object {
  "highlights": Object {
    "errMess": null,
    "highlights": Array [],
    "isLoading": true,
  },
  "posts": Object {
    "errMess": null,
    "isLoading": false,
    "posts": Array [
      Object {
        "__v": 0,
        "_id": "5f37c3ebc8cb6a46c89bc33c",
        "by": Object {
          "_id": "5ed8d7fcfd3da42bc426992a",
          "name": "mad",
          "username": "mad",
        },
        "content": "this is first post",
        "contentWarn": false,
        "createdAt": "2020-08-15T11:15:55.986Z",
        "flaged": false,
        "heading": "first pot",
        "report": 0,
        "updatedAt": "2020-08-15T11:15:55.986Z",
      },
    ],
  },
}

then add new post action is performed using reducer====>然后使用reducer执行添加新的post操作====>

export const posts = (
  state = { isLoading: true, errMess: null, posts: [] },
  action
) => {
  switch (action.type) {
 
   case ActionTypes.UPDATE_POSTS:
      return {
        posts: state.posts.posts.concat(action.payload),
      };
    default:
      return state;
  }
};

this is the error returned这是返回的错误

 action     Object {
  "payload": [TypeError: undefined is not an object (evaluating 'state.posts.posts.concat')],
  "type": "UPDATE_POSTS_FAILED",
}

undefined is not an object (evaluating 'state.posts.posts.concat')
* Redux\posts.js:24:13 in posts

this is the result i wanted to happen这是我想要发生的结果

next state Object {
  "highlights": Object {
    "errMess": null,
    "highlights": Array [],
    "isLoading": true,
  },
  "posts": Object {
    "errMess": null,
    "isLoading": false,
    "posts": Array [
      Object {
        "__v": 0,
        "_id": "5f37c3ebc8cb6a46c89bc33c",
        "by": Object {
          "_id": "5ed8d7fcfd3da42bc426992a",
          "name": "mad",
          "username": "mad",
        },
        "content": "this is first post",
        "contentWarn": false,
        "createdAt": "2020-08-15T11:15:55.986Z",
        "flaged": false,
        "heading": "first pot",
        "report": 0,
        "updatedAt": "2020-08-15T11:15:55.986Z",
      },
     Object {
        "__v": 0,
        "_id": "5f37c3ebc8cb6a46c89bc33c",
        "by": Object {
          "_id": "5ed8d7fcfd3da42bc426992a",
          "name": "mad",
          "username": "mad",
        },
        "content": "this is second post",
        "contentWarn": false,
        "createdAt": "2020-08-15T11:15:55.986Z",
        "flaged": false,
        "heading": "second post",
        "report": 0,
        "updatedAt": "2020-08-15T11:15:55.986Z",
      }
    ],
  },
}

Your reducer should have this structures with two level posts:你的 reducer 应该有这样的结构,有两个级别的帖子:

export const posts = (
  state = {
    highlights: {
      errMess: null,
      highlights: [],
      isLoading: true,
    },
    posts: { isLoading: true, errMess: null, posts: [] },
  },
  action
) => {
  switch (action.type) {
    case ActionTypes.UPDATE_POSTS:
      return {
        ...state,
        posts: state.posts.posts.concat(action.payload),
      };
    default:
      return state;
  }
};

and try to keep the same data structure in the state, like highlights !并尽量在 state 中保持相同的数据结构,就像亮点一样!

      return {
        ...state,
        posts: state.posts.concat(action.payload),
      }

this reducer worked Note: I am using highlight reducer in separate file这个reducer工作注意:我在单独的文件中使用highlight reducer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Redux,我在这里改变状态吗? - React Redux, Am I mutating state here? 我没有正确地改变我的 redux 商店的 state - I am not properly mutate the state of my redux store 我正在尝试使用 react-redux 存储我的数据,但它没有。 我该如何解决这个问题? - I am trying to store my data using react-redux, but it doesnt. How can I solve this? 为什么我没有从反应 redux state 获得更新的值 - Why I am not getting the updated value from react redux state 为什么我在 React 和 redux 上收到此错误“TypeError: state is not iterable” - Why am i getting this error 'TypeError: state is not iterable' on react and redux 我正在尝试使用 redux,但在我设置 redux 商店时它返回空白页 - I am trying to use redux but it returns blank page when I set up the redux store 我正在尝试在单击时从 redux 商店加载数据并更新 state,但来自 state 的数据在单击时未得到更新 - I am trying to load data from redux store on click and updating the state, but the data from state is not getting updated on the click 我是否在减速器中改变了redux状态? - Am I mutating the redux state in reducer? 如果我将redux用于状态管理,是否应该在本地存储状态? - Should I store state locally if I am using redux for state management? 我尝试在商店中恢复 state (react, redux) - I try to recover a state in store (react, redux)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM