Hooks with lazy loading
mikesol opened this issue · 7 comments
The current signature for useState is polymorphic over any state:
useState ::
forall state.
state ->
Hook (UseState state) (state /\ ((state -> state) -> Effect Unit))However, if the state is a function in react, it tries to lazily evaluate the function: https://reactjs.org/docs/hooks-reference.html#lazy-initial-state. Should this case be encapsulated somehow in the library?
Good point, this is probably a bug right now! I'll take a look at it soon
Fixed in 7.0.1
Thanks!
I checked out the fix and it looks great, but I'm not sure if it preserves the lazy loading mechanism. Would it be possible to represent this as a fundep from Lazy x to x and otherwise from x to x?
Correct. I didn't think it made sense to complicate the types and docs with a third version of useState when you can currently achieve the same thing with useMemo:
initialState <- useMemo unit \_ -> slowInitState props
state /\ setState <- useState initialStateI'm not entirely opposed though, if that doesn't work for some reason. Actually, if I were building it from scratch today I'd probably just force the lazy useState always, but that's an unnecessarily large breaking change now, probably.
Re: Lazy fundeps, that would most likely be a breaking API change, so probably not worth it unless we're already making v8 for some other reason. Is that right? My fundep knowledge is rusty.