How do I setState for nested object?

后端 未结 5 645
鱼传尺愫
鱼传尺愫 2020-11-29 20:36

For a plugin I\'m using I have to have a state that looks like this:

getInitialState() {
  return {
    invalid: true,
    access: {
      access_code: \'\',         


        
5条回答
  •  囚心锁ツ
    2020-11-29 21:12

    Another way to do this would be

    const newAccess = {...this.state.access};
    newAccess.hospital_id = 1;
    setState({access: newAccess});
    

    Use of the spread operator creates a clone of this.state.access.

提交回复
热议问题