与对象反应挂钩useState()

在带有钩子的React中,更新状态的正确方法是嵌套对象是什么?

export Example = () => {

const [exampleState, setExampleState] = useState(

{masterField: {

fieldOne: "a",

fieldTwo: {

fieldTwoOne: "b"

fieldTwoTwo: "c"

}

}

})

一个人怎么会使用setExampleState到的更新exampleStatea(附加一个字段)?

const a = {

masterField: {

fieldOne: "a",

fieldTwo: {

fieldTwoOne: "b",

fieldTwoTwo: "c"

}

},

masterField2: {

fieldOne: "c",

fieldTwo: {

fieldTwoOne: "d",

fieldTwoTwo: "e"

}

},

}

}

b (改变价值)?

const b = {masterField: {

fieldOne: "e",

fieldTwo: {

fieldTwoOne: "f"

fieldTwoTwo: "g"

}

}

})

回答:

您可以像这样传递新值

  setExampleState({...exampleState,  masterField2: {

fieldOne: "c",

fieldTwo: {

fieldTwoOne: "d",

fieldTwoTwo: "e"

}

},

}})

以上是 与对象反应挂钩useState() 的全部内容, 来源链接: utcz.com/qa/403229.html

回到顶部