Don't want to re-render when value.current is rebound to a new value - then use useRef() (Aside: another use of useRef() that doesn't reference a DOM element: handleEvent & React )
If value can change (e.g. via useEffect()) requiring a re-render then useState() has to be used.
Don't use the closure value for initialization but initialize both states from the same fresh value instead.
Once asynchronous updates get involved things get even more interesting in terms of the number of renders:
Two renders:
Four renders:
Three renders:
Back to two renders:
Picking
useRef()
overuseState()
comes down to:value.current
is rebound to a new value - then useuseRef()
(Aside: another use ofuseRef()
that doesn't reference a DOM element: handleEvent & React )useEffect()
) requiring a re-render thenuseState()
has to be used.How React isn't reactive, and why you shouldn't care
Ryan Carniato for This is Learning ・ Mar 18 '21 ・ 6 min read
Deep dive: How do React hooks really work?
Major edit: Made implied fetch explicit by emulating it with a promise.
Thanks for explanation