简体   繁体   English

如何使用setstate在react中更新对象数组中特定对象的属性

[英]how to update a property of a specific object in arrays of object in react using setstate

I want to update a specific value of specific object of array(array of object) using setState in react context !我想在反应上下文中使用 setState 更新数组(对象数组)的特定对象的特定值!

here is what i am doing这是我在做什么

getFieldData = (inputType, data, id) => {
       const index = this.state.sharedPeople.findIndex(a => a.id == id);
       this.setState({ sharedPeople: [this.state.sharedPeople[index], { email: data }] })}

am not able to update the value by doing this it malfunctions here !!我无法通过这样做来更新值,它在这里发生故障! Please guide me the better and correct way of doing this behaviour !请指导我做这种行为的更好和正确的方法!

You could你可以

    getFieldData = (inputType, email, id) => {

      let sharedPeople = this.state.sharedPeople.map((person => {
        if(person.id === id){
           person.email = email
      }
      return person
    })

setState({sharedPeople})

}

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

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