setState

To mutate state, you use React's setState method available to you as part of the React.Component base class. The simplest usage is to provide it with a new object, as follows:

onUpdate = () => {
this.setState({
greet: 'State Updated',
});

};

Internally, setState merges the object you specify with the complete state object. Therefore, you can provide it with an object that includes only the updated portion of the entire state and not repeats of all the properties every time. More importantly, setState is usually asynchronous, meaning you shouldn't rely on having an updated state right after calling setState.

In this example, when the child component's button is clicked, onUpdate is executed in the parent and updates its state. Calls to setState trigger React's reconciliation process; therefore, the parent's component tree is re-rendered. The parent renders and passes the updated prop to the child, which should now display Hello State Updated.

setState is significant in React, and understanding its effects and usage is important. You can read more about props and state here:
https://reactjs.org/docs/components-and-props.html
https://reactjs.org/docs/state-and-lifecycle.html
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.30.236