mweststrate / use-st8

Single-function alternative for React.useState
MIT License
234 stars 7 forks source link

Question: What about prevent stale state by using ref #3

Closed edygarOliveira closed 4 years ago

edygarOliveira commented 5 years ago

What if the returned version was bound to a ref instead of the state directly? So whenever a new state was assigned, the ref.current would be updated and the function could always return the latest state.

I know, the array of diffs could help to solve the staling, but it implies that the consumer always use that state inside hooks callbacks, but it isn’t the case for asynchronous callbacks

mweststrate commented 5 years ago

it is quite a convenient idea imho, although it goes against the general React philosophy, so kinda torn by the idea :sweat_smile: