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

Lazy Initialization doesn't work in useContextState #53

Closed
renatorib opened this issue Dec 2, 2018 · 3 comments
Closed

Lazy Initialization doesn't work in useContextState #53

renatorib opened this issue Dec 2, 2018 · 3 comments
Labels
bug Something isn't working

Comments

@renatorib
Copy link

renatorib commented Dec 2, 2018

Note: this issue refers to v1 alpha

I saw that you wraps useContextState inside a useContextReducer, but this has a problem. `React.useState and React.useReducer handle lazy initialization differently.

React.useState accept initialState as a function to be computed once, but React.useReducer doesn't. It uses a initialAction argument instead for perform lazy initialization.

Because of this, it's currently not working, since initialState argument is passed to React.useReducer that does not handle initialState as function.

useContextState('context-state', () => someExpensiveComputation(props))
@renatorib renatorib changed the title Lazy Initialization don't working Lazy Initialization doesn't work in useContextState Dec 2, 2018
@diegohaz diegohaz added the bug Something isn't working label Dec 2, 2018
@diegohaz
Copy link
Owner

diegohaz commented Dec 2, 2018

Good catch. I thought that, since React.useState calls React.useReducer underneath (in React codebase) it would work out of the box, but it seems that it handles only a special case that only works inside React code.

@diegohaz
Copy link
Owner

diegohaz commented Dec 3, 2018

Should be fixed in v1.0.0-alpha.5

@renatorib
Copy link
Author

😍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants