useState
provides a value that will persist across renders
and an API to update that value and trigger a re-render.
Use whenever you set the current state based on the previous state.
const increment = () => setCount((count) => count + 1)
Use if the initial value for state is the result of an expensive calculation.
const [count, setCount] = React.useState(() => getExpensiveCount())
The effect runs AFTER React updates the DOM and AFTER the browser paints those updates.
Invoked right before invoking the new effect on a re-render AND right before removing the component from the DOM.
React.useEffect(() => {
return function cleanup() { ... }
})
The effect runs AFTER React updates the DOM and BEFORE the browser paints those updates.