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

Feature request: Initialize StoreProvider with defaultState #27

Open
Christopher96u opened this issue Sep 13, 2024 · 4 comments
Open

Feature request: Initialize StoreProvider with defaultState #27

Christopher96u opened this issue Sep 13, 2024 · 4 comments

Comments

@Christopher96u
Copy link

Christopher96u commented Sep 13, 2024

I'm trying to implement something like this library: https://github.com/charkour/zustand-di

I'm using the utility helper from this library(zustand-slices) to create the Provider, but I'm still dealing with the TS Type inference to make it type-safe.

Could you provide a example of this feature?

It's really helpful for testing. I don't wanna use stores without Provider because it's really hard to test.

Any idea?

I want to get the Provider like this

<GlobalStoreProvider defaultState={initialState}>
where initialState infers the type based on my store, thanks!

@dai-shi
Copy link
Member

dai-shi commented Sep 14, 2024

If I understand it correctly, the question is about how to type React Context, which is not specific to Zustand.

How does zustand-di solve it?

Please try to create a very very small example to illustrate what you would like to accomplish with https://www.typescriptlang.org/play.

@Christopher96u
Copy link
Author

Hi, a bit busy with my little one today. I'll make a demo tonight and share it with you. Thanks

@Christopher96u
Copy link
Author

Christopher96u commented Sep 14, 2024

Hi @dai-shi , I've written an example and added some comments. example

I'm open to any suggestion. I feel adding the ability to define the zustand context with a prop initialState would be helpful for testing and test components in a isolated way. I'm doing this workaround because I have the same tradeoff than the blogger from here blog . Initialize the store with context will save lot of effors. If not, any advice? The Zustand official documentation for testing, doesn't explain it very well. Thanks

@dai-shi
Copy link
Member

dai-shi commented Sep 15, 2024

Unfortunately, the example doesn't help me understand the problem. Is it modified after you created?

I have a few comments:

  • What you request should be possible on your end. You can use Context and zustand-slices and create store using props.
  • If your question isn't tied to zustand-slices, you might want to open a discussion in the zustand repo. More people will look
  • In general, it's not certain if using Context only for testing is ideal. I would avoid adding code in production which is only required for testing.

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