Here's my code:
const [val, setVal] = useState(0);
function onClick() {
setVal(val + 1);
}
I just wanted to check if this is the correct way to update a state based on a previous state? Why/why not?
This is a perfectly fine approach. Note that you could also get the value of your current state as a callback parameter on setVal, e.g.: setVal(prevState => prevState + 1).
Just be careful when manipulating arrays or objects since they shouldn't be directly mutated (with methods such as push or pop etc).