Skip to content

Refactoring Context to Zustand - How can I initialize a store property with a hook call? #1027

Closed Answered by dai-shi
jestrickler asked this question in Q&A
Discussion options

You must be logged in to vote

I would also like to hear from others how they use zustand store for auth state.

From the lib perspective, what you are trying to has a mismatch.
useOauth2 provides a state and AuthContext provides a dependency injection.
So you can't use zustand store only to replace AuthContext. You want to replace both useOauth2 and AuthContext altogether. oauth2 should have a vanilla (non-react) api.

Is there another way to get this initialized on the store?

You could, however, create a zustand store in a component (instead of at module level): https://github.com/pmndrs/zustand#react-context

but it would render uninitialized the first time through

It's probably unavoidable if you need to use useOa…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
4 replies
@igortas
Comment options

@dai-shi
Comment options

@igortas
Comment options

@dai-shi
Comment options

Answer selected by jestrickler
Comment options

You must be logged in to vote
1 reply
@jestrickler
Comment options

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