Skip to main content

React states

In React, we can store data in a component's state. The state is an object that contains the data we want to store. We can access the state using the useState hook.

When the state of the component changes, it will trigger a re-render of the component. This means that the component will be rendered again, and the new state will be used to render the component.

Let's see an example. Say we want to create a counter. One could design a component that looks like this.

Live Editor
Result
Loading...

This won't work in React. Instead, you'll need to declare the count variable as a state. This can be done using the useState hook. You will also need to use the setCount function to update the state.

Live Editor
Result
Loading...
tip

Exercise that muscle memory. Try to change the first example so that it now uses the useState hook.