Updating state - why creating a new copy of state when calling setState?

六月ゝ 毕业季﹏ 提交于 2021-02-11 01:12:16

问题


React docs:

Never mutate this.state directly, as calling setState() afterwards may replace the mutation you made. Treat this.state as if it were immutable.

That's clear.

class App extends React.Component {
  state = {
   data: []
  } 

the following I understand

  updateState(event) {
   const {name, value} = event.target;
   let user = this.state.user; // this is a reference, not a copy...
   user[name] = value; // 
   return this.setState({user}); // so this could replace the previous mutation
  }

this following I don't understand

  updateState(event) {
  const {name, value} = event.target;
  let user = {...this.state.user, [name]: value};
  this.setState({user});
  }

I understand (as in previous example), that I should not either only:

  1. mutate state directly without calling setState; or
  2. mutate it and then use setState afterwards.

However, why can't I just (without direct mutation) call setState without creating a new copy of state (no spread operator/Object.assign)? What would be wrong with the following:

  getData = () => {
   axios.get("example.com") ...
    this.setState({
     data:response.data
    })
  } 

Why should it be:

  getData = () => {
   axios.get("example.com") ...
    this.setState({
     data:[...data, response.data]
    })
  } 

 render (){ 
  ...
 }  
}

回答1:


What would be wrong with the following:

this.setState({
   data: response.data,
});

Absolutely nothing, unless you don't want to replace the contents of this.state.data with response.data.

Why should it be:

this.setState({
   data: [...data, response.data],
});

Because with spread you are not loosing the contents of this.state.data - you are basically pushing new response into the data array.

Note: You should use callback inside setState to get access to current data from this.state.

this.setState((prevState) => ({
   data: [...prevState.data, response.data],
}));


来源:https://stackoverflow.com/questions/58901446/updating-state-why-creating-a-new-copy-of-state-when-calling-setstate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!