简体   繁体   中英

when i update state in react, is this how i avoid mutating the state?

I read that I should be using functional setState and also not mutating my state.

Is this how that works?

onChangeTextInput = ({ name, value }) => {
    this.setState(state => ({
      ...state,
      form: {
        ...state.form,
        [name]: value
      }
    }));
  };
this.setState({ form: {...this.state.form, [name]: value }});

The above is the simpler form for what you trying to do. When it says don't mutate directly, what it means is dont do something like this.state.form = newObj

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM