我想在react上下文中使用setState更新array(object array)的特定对象的特定值!
这是我在做什么
getFieldData = (inputType, data, id) => {
const index = this.state.sharedPeople.findIndex(a => a.id == id);
this.setState({ sharedPeople: [this.state.sharedPeople[index], { email: data }] })}
我无法通过执行此操作来更新值,这将导致故障! 请指导我做这种行为的更好和正确的方法!
答案 0 :(得分:0)
您可以
getFieldData = (inputType, email, id) => {
let sharedPeople = this.state.sharedPeople.map((person => {
if(person.id === id){
person.email = email
}
return person
})
setState({sharedPeople})
}