1) Counter App
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0); // Initialize state with 0
const increment = () => {
setCount(count + 1); // Update state
};
return (
<div>
<h1>Count: {count}</h1>
<button onClick={increment}>Increment</button>
</div>
);
}
export default Counter;
2) Bulb ON/OFF
import { useState } from 'react';
function Lightbulb() {
const [isOn, setIsOn] = useState(false); // Initial state is 'off'
const toggleLight = () => {
setIsOn(!isOn); // Toggle between true and false
};
return (
<div>
<h1>The light is {isOn ? "ON" : "OFF"}</h1>
<button onClick={toggleLight}>
{isOn ? "Turn Off" : "Turn On"}
</button>
</div>
);
}
export default Lightbulb;