Comment by adzm

7 hours ago

An interesting feature is this:

> A current-state getter. useState and useReducer return [state, update, getState], so a delayed callback can read the latest value instead of a stale capture.

There are currently a few workarounds in React for this, and using other state management libs also give you non-reactive access to current state, but this is a big help in certain situations.

For example, a callback / event that you pass to all your child elements, that needs to access the current state when run, therefore normally needs to be recreated with the new state captured (in useCallback) whenever state changes, which then causes all child components to re-render. Non-reactive access to current state in callbacks avoids this entirely.

There are some hacks that fix this by wrapping the updated callback in a ref and updating it with an event (for example, https://usehooks-ts.com/react-hook/use-event-callback useEventCallback as implemented various places) and returning a stable reference to a function that calls that saved one, though I always wonder if there are downsides to this.