React Hooks useState() with Object

前端 未结 9 1667
猫巷女王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 07:02

    You can pass new value like this

      setExampleState({...exampleState,  masterField2: {
            fieldOne: "c",
            fieldTwo: {
               fieldTwoOne: "d",
               fieldTwoTwo: "e"
               }
            },
       }})
    

提交回复
热议问题