- useState is a React Hook used to stores the value and update state in functional components. It returns a state variable and a setter function. Whenever the state changes, React triggers a re-render to update the UI.
Syntax:
const[Variable,setVariable]=useState(initial value)
Example:
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
function increment() {
setCount(count + 1);
}
return (
<div>
<p>Current Count: {count}</p>
<button onClick={increment}>Increase</button>
</div>
);
}
export default Counter;
Top comments (0)