K
K
kamelot432021-01-22 18:53:09
React
kamelot43, 2021-01-22 18:53:09

How to correctly change the state of a React component?

The state of a component stores data. Their structure is different. How can you update different data in one call to setState: an object and a string? In the current implementation (just an example), you need to update the state twice to bring in the data. Is it possible to do everything in one setState call?

this.setState(prevState => {
      let current = Object.assign({}, prevState.current);
      current.params.from = curFrom;
      current.params.to = curTo;
     
      return { current };
    });

 this.setState({
    datePickerValueFrom: curFrom,
    datePickerValueTo: curTo,
});

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question