Skip to content

initial store state from props #552

Closed Answered by dai-shi
TkDodo asked this question in Q&A
Sep 2, 2021 · 1 comments · 5 replies
Discussion options

You must be logged in to vote

(I recently recommend use-constant for lazy stable one-time memoization, instead of useState.)

If you create a store with in a component (because of depending on props), and want to propagate it, then react context is the right solution.
However, passing a hook in context value has a pitfall. Unless it's not stable, we can violate rules of hooks.
To avoid the pitfall, we provide a special createContext in zustand/context. You can't violate rules of hooks with the special one.

The second approach would also work. It's a global store with 2nd pass initialization, so to say.

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@TkDodo
Comment options

TkDodo Sep 2, 2021
Collaborator Author

@dai-shi
Comment options

@TkDodo
Comment options

TkDodo Sep 2, 2021
Collaborator Author

@dai-shi
Comment options

@TkDodo
Comment options

TkDodo Sep 2, 2021
Collaborator Author

Answer selected by TkDodo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants