简体   繁体   English

无法在REACT / REDUX中获得状态以正确更新对象内部的数组

[英]Can't get state to update array inside of an object correctly in REACT/REDUX

I am going to break this down step by step for what I want to happen so hopefully people can understand what I am wanting. 我将针对我想发生的事情逐步分解,希望人们能够理解我想要的。

Using React/Redux, Lodash 使用React / Redux,Lodash

  1. I have many post that are sent from a back end api as an array. 我有很多帖子是从后端api作为数组发送的。 Each post has an _id . 每个帖子都有一个_id When I call on the action getAllPost() it gives me back that array with all the post. 当我调用动作getAllPost()它会将所有帖子都带回给我。 This is working just fine. 这很好。

  2. I then dispatch type GET_ALL_POSTS and it triggers the reducer reducer_posts to change/update the state. 然后,我分派类型GET_ALL_POSTS ,它触发reducer_posts更改/更新状态。

reducer: 减速器:

export default function(state = {}, action) {
  switch(action.type) {
    case GET_ALL_POSTS:
    const postsState =  _.mapKeys(action.payload.data, '_id');
    //const newPostsState = _.map(postsState, post => {
      //const newComments = _.mapKeys(post.comments, '_id');
    //});
      return postsState;
    break;
    default:
      return state;
    break;
  }
}
  1. As you can see I change the array into one giant object that contains many post as objects with keys that are equal to their '_id'. 如您所见,我将数组更改为一个巨型对象,该对象包含许多帖子,这些帖子的键等于其“ _id”。 This works just fine and returning this part of the state also works fine. 这很好,返回该状态的一部分也很好。
  2. As I mentioned each of these posts has a comments value that is an array. 正如我提到的,这些帖子中的每个帖子都有一个数组的comment值。 I would like to change the comments array into one large object that holds each comment as an object with a key that is equal to their '_id' just like I did in the post. 我想将注释数组更改为一个大对象,该对象将每个注释作为一个对象,其键等于其“ _id”,就像我在帖子中所做的那样。
  3. Now I need to do this all at once and return the newly created state with One large object that contains all the post as objects and on each of those post there should be a comments object that contains all the comments as objects. 现在,我需要立即执行所有操作,并使用一个包含所有帖子作为对象的大对象返回新创建的状态,并且在每个帖子上都应该有一个注释对象,该对象将所有注释作为对象。 I will try to write some example code to show what I am trying to do. 我将尝试编写一些示例代码来显示我要执行的操作。

Example: 例:

BigPostsObject { 
1: SinglePostObject{}, 
2: SinglePostObject{}, 
3: SinglePostObject {
  _id: '3',
  author: 'Mike',
  comments: BigCommentObject{1: SingleCommentObject{}, 2: SingleCommentObject{}}
 }
}

I hope that the example kind of clears up what I am trying to do. 我希望这个例子能弄清楚我要做什么。 If it still is confusing as to what I am doing then please ask and also please do not say things like use an array instead. 如果仍然对我在做什么感到困惑,请询问,也不要说使用数组代替之类的事情。 I know I can use an array, but that is not helpful to this post as if others want to do it this way that is not helpful information. 我知道我可以使用数组,但是这对这篇文章没有帮助,就好像其他人希望以此方式做也是有用的信息。

I believe nowadays JS builtin function can do this without requiring external libraries. 我相信如今,JS内置函数无需外部库即可完成此操作。 Anyway this should be the way to go. 无论如何,这应该是要走的路。 I will really encourage you getting back to js builtin functions. 我真的会鼓励您回到js内置函数。

var data = [
 {
  _id: '3',
  title: 'Going on vaccation',
  comments:[ 
    {_id: 1, comment: 'hello'},
    {_id: 2, comment: 'world'}           
  ] 
 }, 

 {
  _id: '2',
  title: 'Going to dinner',
  comments:[ 
    {_id: 1, comment: 'hello'},
    {_id: 2, comment: 'world'}           
  ] 
 } 

]

//you can use JS builtin reduce for this
var transformedPost= _.reduce(data, function(posts, post) {
  var newPost = Object.assign({}, post)
  newPost._id=post._id
  //you can use js builtin map for this 
  newPost.comments = _.mapKeys(post.comments, '_id')

  // if you are using es6, replace the last three line with this 
  //return Object.assign({}, posts, {[newPost._id]: newPost})

  var item = {}
  item[newPost._id]=newPost
  return Object.assign({}, posts, item)
},{});

console.log(transformedPost)

https://jsbin.com/suzifudiya/edit?js,console https://jsbin.com/suzifudiya/edit?js,console

Write a function that processes all the comments from the comments array for each post you have in the posts array: 编写一个函数,为您在posts数组中的每个帖子处理来自评论数组的所有评论:

function processComment(post) {
   post.bigCommentsObject = _.mapKeys(post.comments, '_id');
   // now the comments array is no longer needed
   return _.omit(post, ['comments']);

}

Now use that function to turn each comments array into a big object with all the comments WHILE it still is in the array. 现在,使用该函数将每个注释数组变成具有所有注释的大对象,尽管它仍在数组中。 Then afterwards turn the array itself in a big object: 然后,将数组本身放在一个大对象中:

const commentsProcessed =  _.map(action.payload.data, procesComment);
const postsState =  _.mapKeys(commentsProcessed, '_id');   

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

相关问题 如何使用 Redux 更新对象数组中的状态? - How can I update a state inside an array of object with Redux? 如果我在对象中有一个数组并且该对象在一个大数组中,我该如何更新 React Native 状态? - How can I update the React Native state if I have an array inside an object and the object is inside in a big array? 在 React redux 对象的状态数组中,我无法更新它创建的状态错误在调度之间检测到状态突变 - In React redux state array of objects i can't update the state it creates error A state mutation was detected between dispatches React / Redux:无法映射状态[object Object] - React/Redux: Can't map over state [object Object] 更新React Redux中的对象数组 - update the array of object in the react redux 无法更新 React state - Can't get React state to update 我如何从 React-Redux 中的 reducer 中的状态更新对象数组中单个元素的名称字段? - How can i update a name field from single element in array of object from a state in reducer in React-Redux? 无法在Redux中更新特定数组项内的输入值 - Can't update an input value inside specific array item in redux 无法在 React 中获取数组状态值 - Can't get the array state value in React 如何使用反应正确更新阵列中的 state? - How to correctly update state in array using react?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM