Can the array obtained by react from state be modified directly?

the following code does not feel reasonable. How to write

var items = this.state.items;  
items[i].status = "doing";  
this.setState({  
      items: items  
});  

the environmental background of the problems and what methods you have tried

related codes

/ / Please paste the code text below (do not replace the code with pictures)

what result do you expect? What is the error message actually seen?

Aug.25,2021

see which one you inherit. Inheriting component can be modified directly. It is not possible to inherit purecomponent


first of all, this method of writing is not recommended, but more recommended is the way of writing immutable, for example:

var items = this.state.items;  

this.setState({  
  items: items.map(item => ({...item, ...{status: 'doing'}}))  
});  

Why is it not recommended to modify it directly?

There are two reasons for

:

1. Performance issues

this way of directly changing the original object makes it impossible for react to optimize it, so
has potential performance problems

.

2. It is difficult to locate the problem

if you use purecomponent, you will find that the status cannot be updated directly. The reason for
is that purecomponent rewrites the SCU,SCU by directly judging the reference difference between state and props, so it will
return false, so that render cannot run

.

this method is not officially recommended, but many people use
if you are worried, you can use immutable, or directly get a copy of the object array and change it

.
  setState ()  

Menu