React — State in React
State
is a built-in React object used to store dynamic data in a component. When the state changes, the component re-renders automatically.
State is mostly used in class components, but with Hooks, you can also use it in functional components.
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
);
}