javascript / beginner
Snippet
Managing Numbers with useState
The useState hook can track numeric values. You provide an initial number (0) and update it using the setter function when an event occurs.
snippet.js
javascript
1
2
3
4
5
6
7
8
9
const ClickCounter = () => {const [count, setCount] = React.useState(0);return (<button onClick={() => setCount(count + 1)}>Clicked {count} times</button>);};
react
Breakdown
1
const [count, setCount] = React.useState(0);
Initializes a state variable 'count' starting at 0 and a function 'setCount' to change it.