useState

The React hook that adds a state variable to a component and returns the current value with a function to change it.

In more detail

const [count, setCount] = useState(0) gives you the value for this render and a setter. Calling the setter does not change count immediately; it asks React to render again with the new value. Passing a function to the setter, as in setCount((prev) => prev + 1), always works from the latest queued value.

Examples

const [open, setOpen] = useState(false);
const [count, setCount] = useState(0);

Related terms

Taught in

References