K
K
Kappy2018-12-07 13:26:59
React
Kappy, 2018-12-07 13:26:59

React, How to change the state?

There is a form and a result that communicate through the parent.
Data is formed in the form, sent to the parent, where the data is processed, and the data is sent to the result component. But they go there as properties, and the state of the result does not change. How to change the state of the result? The data got into the props, changed, and the old ones remained in the state. Already the head floats simply, but it is necessary to finish
<Result data = {this.state.data}/>
5c0a4ad814b15583357386.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-12-07
@KappyJS

static getDerivedStateFromProps(props, state) {
  if (props.data !== state.data) {
    return {
      data: props.data,
    };
  }

  return null;
}

But, in a good way, if the data is stored in the state of the parent and changes there, then in most cases they do not need to be written in the state of the child component, you can simply use props.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question