React Hooks useState() with Object

前端 未结 9 1693
猫巷女王i
猫巷女王i 2020-12-02 06:04

What is the correct way of updating state, is a nested object, in React with Hooks?

export Example = () => {
  const [exampleState, setExampleState] = use         


        
9条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 06:38

    I think best solution is Immer. It allows you to update object like you are directly modifying fields (masterField.fieldOne.fieldx = 'abc'). But it will not change actual object of course. It collects all updates on a draft object and gives you a final object at the end which you can use to replace original object.

提交回复
热议问题