This is a great question because it challenges the best practice of having a setState
for each slice of state.
The best way is to create a POJO with two keys (to be explicit), one for running, one for jumping. Then, the setter will have 3 permutations.
- setting just jumping
- setting just running
- setting both
const [actions, setActions] = useState({running: false, jumping: false});
const { jumping, running } = actions;
I don't think this is a best practice, you should split them up whenever you can to avoid this pattern. However, this is one instance where it may be worth merging them to save a render (which can be desirable).