Always pass a function that takes the previous state as an argument when setting the new state. Otherwise, it will use the initial state when the component was initialized.
function MyComponent() {
const [counter, setCounter] = useState(0);
...
setCounter(counter + 1);
}
function MyComponent() {
const [counter, setCounter] = useState(0);
...
setCounter((prevCounter) => prevCounter + 1);
}