My First Steps with React
A counter has two pieces: the value it stores and the number it displays. In React, a component can keep that value in useState. Clicking a button calls the setter, and React renders the component with the new value.
What changes on a click?
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(n => n + 1)}>
{count}
</button>
);The function passed to setCount receives the pending value. If two updates are queued, each adds one to the result of the previous update. The example below illustrates the same interaction using plain JavaScript.
State counter
Change a counter and see how the value on screen follows its state.
count