Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hooks with lazy loading #53

Closed
mikesol opened this issue Nov 18, 2021 · 7 comments
Closed

Hooks with lazy loading #53

mikesol opened this issue Nov 18, 2021 · 7 comments

Comments

@mikesol
Copy link

mikesol commented Nov 18, 2021

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?

@megamaddu
Copy link
Member

Good point, this is probably a bug right now! I'll take a look at it soon

@megamaddu
Copy link
Member

Fixed in 7.0.1

@mikesol
Copy link
Author

mikesol commented Nov 25, 2021

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?

@megamaddu
Copy link
Member

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 initialState

I'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.

@mikesol
Copy link
Author

mikesol commented Nov 25, 2021 via email

@megamaddu
Copy link
Member

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.

@mikesol
Copy link
Author

mikesol commented Nov 25, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants