How do I update states onchange in an array of object in React Hooks

前端 未结 3 393
醉话见心
醉话见心 2020-12-03 00:41

I have retrieved datas stored using useState in an array of object, the datas was then outputted into form fields. And now I want to be able to update the fields (state) as

3条回答
  •  猫巷女王i
    2020-12-03 01:35

    You can do this without mutation by mapping your old array into a new one, swapping what you want to change for an updated item along the way.

    setDatas(
        datas.map(item => 
            item.id === index 
            ? {...item, someProp : "changed"} 
            : item 
    ))
    

提交回复
热议问题