Intermediate
How does state management work in React?
State management in React allows components to maintain and manage their own data. State is a built-in object that stores dynamic data about the component.
To manage state, you can use the useState
hook in functional components. Here's a step-by-step guide on how to do this:
- Import
useState
from React. - Initialize state using the
useState
hook. - Update state using the function returned by
useState
.
Here's an example:
import React, { useState } from 'react';
const Counter = () => {
const [count, setCount] = useState(0);
return (
Count: {count}
);
};