import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
const increment = () => {
setCount(count + 1);
};
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
}
export default Counter;
In this example, the Counter
component has a state variable count
. When you click the "Increment" button, it triggers a state change using setCount
, leading to a re-render of the Counter
component to reflect the updated count value in the UI.