Hacker News new | ask | show | jobs
by jy14898 323 days ago
> The other option is to misuse refs to also maintain a current state there in addition to the useState. So always use the ref, but set the state in the end to trigger a re render.

Arugably this isn't a misuse, but just accepting that Reacts state management isn't powerful enough for what you need. Something akin to useRef + useSyncExternalStore is often what developers want.

For example, there's no guarantee that your useEffect will be invoked with the intermediate state you wanted to observe.