React: can you use setState with existing state object?

后端 未结 3 539
盖世英雄少女心
盖世英雄少女心 2021-01-28 23:09

Lets say I have a React component that has a \"state\" with 10 fields:

this.state = {
    field1: 1,
    field2: 2,
    ... other fields
    something: \'a\'
};
         


        
3条回答
  •  忘掉有多难
    2021-01-28 23:48

    To update single field you need to pass object with this field. React will merge it for you.

    this.setState({something: 'b'})
    

提交回复
热议问题