useState
is a built-in React hook that allows us to use state in a functional component. It returns an array of two values: the first value is the current state, and the second value is a function to update that state.
Here's an example of how to use useState
in a React functional component:
280 chars17 lines
In this example, we have a component called Counter
that displays a count and a button to increment that count. We use useState
to declare a state variable called count
and a function called setCount
to update that state. We initialize count
to 0.
When the button is clicked, the increment
function is called, which uses setCount
to update the count
state by adding 1.
Note that you need to import useState
from the react
library before you can use it.
gistlibby LogSnag