[英]How to update complex nested object in setState?
How can I access the index
inside array
and update it without removing property and value inside the object?如何访问array
内的index
并更新它而不删除对象内的属性和值?
const [data, setData] = useState({
commission: [{ commissionFor: "", commissionPercentage: "" }],
salary: [{ salaryFor: "", salaryPrice: "" }],
off: [{ offFor: "", offPercentage: "" }],
});
<Autocomplete
onChange={(e, value) =>
setData((data) => ({
...data,
["commission"]: {
...data[0],
["commissionFor"]: value || e.target.value,
},
}))
}
renderInput={(params) => <TextField {...params} label="Commission" />}
/>
Can be done like this可以这样做
setData((data) => ({
...data,
commission: data.commission.map((c, index) => index === 0 ? {...c, commissionFor: value || e.target.value}: c)
}))
如果您正在映射这些数据,则在单击数据后使用 0 的索引位置发送该项目的索引
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.