useState Hook
useState
is a Hook that allows functional components to hold state.
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
useState
returns the current state and a function to update it.- Calling the setter re-renders the component.