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 ...

Get Hands-On Full-Stack Web Development with ASP.NET Core now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.