diegohaz/constate

Lazy Initialization doesn't work in useContextState

renatorib opened this issue · 3 comments

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))

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.

Should be fixed in v1.0.0-alpha.5

😍