简体   繁体   English

更新reducer中对象的状态

[英]Update the state of object in reducer

在此处输入图片说明 I am new to react js.我是反应 js 的新手。 Here, I do have a functionality where there are three diff rows that can be added or removed.在这里,我确实有一个功能,其中可以添加或删除三个差异行。 I have a reducer which is like,我有一个减速器,就像,

const initialState = {
    Low: [
        {
            id: 'L0',
            technologyId: 0,
            technology: '',
            type: '',
            count: '',
            level: 'EASY'
        }
    ],
    Medium: [
        {
            id: 'M0',
            technologyId: 0,
            technology: '',
            type: '',
            count: '',
            level: 'Medium'
        }
    ],
    High: [
        {
            id: 'H0',
            technologyId: 0,
            technology: '',
            type: '',
            count: '',
            level: 'Tough'
        }
    ],

SO, Here, I want to have this as a generic,so, I am trying to add objects on click of the plus and remove on - .所以,在这里,我想把它作为一个通用的,所以,我试图在点击加号时添加对象并删除 - 。 So,所以,

Here, I am adding using ,在这里,我添加使用,

const tempObj = {
      id: tempvar + id,
      technologyId: 0,
      technology: temp[0].technology,
      type: type,
      count: '',
      level: addtype
    }

I create this object now depend upon the addType I add it in that particular array.我现在创建这个对象取决于我将它添加到该特定数组中的 addType。

I do it using this way,我是用这种方式做的,

const addData = [...this.props.lowData[addtype], tempObj];

    this.props.addNewTech(addData);

So, Here this ...this.props.lowData[addtype] returns an object not an array, is this the problem ?所以,这里这个...this.props.lowData[addtype]返回一个对象而不是一个数组,这是问题吗? If yes, It should return an array not the object but how is this returning the object and not an array.如果是,它应该返回一个数组而不是对象,但是如何返回对象而不是数组。 My props.lowData is like ,我的 props.lowData 就像,

an object with three different arrays, I mean same as that of the reducer .,But every time I add it adds that element in the Low array only and not in the medium or high.一个具有三个不同数组的对象,我的意思是与 reducer 的对象相同。,但是每次我添加它时,它只会在Low数组中添加该元素,而不是在中或高数组中。 Can any one help me with this ?谁能帮我这个 ?

The problem is in the reducer you are only updating the Low property.问题出在减速器中,您只更新Low属性。 You need to check what property needs to update.您需要检查哪些属性需要更新。 For that you can pass addtype to your action creator like so为此,您可以像这样将addtype传递给您的动作创建者

this.props.addNewTech({ addData, addtype });

And then in action creator然后在行动创造者

export function addNewTech(data) { 
  return {
     type: ADD_NEW, 
     data //is equavalent to data: data (ES6)
  } 
} 

And then in reducer dynamically update the object然后在reducer中动态更新对象

    case ADD_NEW: 
      return { 
        ...state, 
        //here choose the property to be updated dynamically
        //replace Low: action.data with below line
        [action.data.addtype]: action.data.addData, 
        error: false, 
      } 

**Note here [action.data.addtype]: action.data.addData we are using computed property names in ES6. **注意这里[action.data.addtype]: action.data.addData我们在 ES6 中使用计算属性名称。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM