UseState Example
UseState Example
function Counter() {
const [count, setCount] = useState(0); // Initialize state with 0
return (
<div>
<h1>Count: {count}</h1>
<button onClick={increment}>Increment</button>
</div>
);
}
2) Bulb ON/OFF
function Lightbulb() {
const [isOn, setIsOn] = useState(false); // Initial state is 'off'
return (
<div>
<h1>The light is {isOn ? "ON" : "OFF"}</h1>
<button onClick={toggleLight}>
{isOn ? "Turn Off" : "Turn On"}
</button>
</div>
);
}