[英]How to edit a property of a object on an array?
I have an array in a useState.我在 useState 中有一个数组。 and I want to edit a property.
我想编辑一个属性。
const [equipe_montador, setEquipe_Montador] = useState([]);
<Card title="Adicionar Mão de Obra">
{equipe_montador.map((item, key) =>
<div key={key}>
<InputBox type="number" maxLength={6} title={item.nome} value={item.quantidade} onChange={e => setEquipe_Montador([...equipe_montador, { ...item, quantidade: e.target.value }])} />
</div>
)}
</Card>
But, when I edit the value, the item duplicate.但是,当我编辑该值时,该项目重复。
I've discovered the solution.我发现了解决方案。 It's working, but I don't know if is the best approach.
它正在工作,但我不知道是否是最好的方法。 I edit the object and create a new list with the edited object.
我编辑对象并使用编辑的对象创建一个新列表。
<Card title="Adicionar Mão de Obra">
{equipe_montador.map((item, key) =>
<div key={key}>
<InputBox type="number" maxLength={6} title={item.nome} value={item.quantidade} onChange={e => {
var new_array = equipe_montador.map((new_obj) => new_obj.id_objeto === item.id_objeto ?
{
...new_obj,
quantidade: e.target.value
} : new_obj);
setEquipe_Montador(new_array);
}
} />
</div>
)}
</Card>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.