useState
It declares a “state variable”.
useState is a new way to use the same capabilities that this.state provides in a class.
Normally, variables disappear when the function exits but state variables are preserved by React.
This is how you initiate useState:
const [count, setCount] = useState(0);
The destructured array as shown above provides two things:
countrefers to the variable that is being updated.setCountis the function that we call to update the variable.
We pass in the initial state in the component.
Best practice is to have the variable named as 'something', then the function to be called 'setSomething'.
This can then be called within your component or page.
Here's an example of this:
<button onClick={() => setCount(count + 1)}>Click me</button>
Example
You clicked 0 times