It's about synchronously invoking functions that are asynchronous and depending on each other. It's a situation that you must always avoid: let your logic to be dependent on the time some async function resolves. And setState
(or the setter function of useState
hook) is an example of such function.
To be more precise: a setState
function (in your case setTest
) is an async operation. If React happens to set the test
value before executing the first if-statement, your if executes the first clause, and if not (which you can't guess and depend on!), the else-block.
The times of React executing it's functions differ, depending on many things, hence the difference between "normal" refreshing and nodemon.
What I'd suggest in your case is to move this if-else code to it's own useEffect
with [test]
as a dependency.