【问题标题】:how to update a property of a specific object in arrays of object in react using setstate如何使用usestate更新对象数组中特定对象的属性
【发布时间】:2020-03-29 07:06:28
【问题描述】:

我想在反应上下文中使用 setState 更新数组的特定对象(对象数组)的特定值!

这就是我正在做的事情

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

我无法通过这样做来更新值,它在这里出现故障! 请指导我更好、更正确地执行此行为!

【问题讨论】:

    标签: arrays reactjs setstate


    【解决方案1】:

    你可以

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-16
      • 2019-12-31
      • 2020-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多