This is by design, the race conditions are caused by your implementation not complying with the constraints it was designed for you to stay within.
Use a local variable (not react state) if you need to store a value and read it back synchronously. Use a useEffect hook if you need side effects to run when react state changes, and finally useMemo if you need derived react state