-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
docs: add migration example in v3 create context #1524
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit b09b385:
|
Size Change: 0 B Total Size: 40.1 kB ℹ️ View Unchanged
|
@chrisk-7777 @sewera @dbritto-dev @charkour |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clear and straightforward. Approved :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@dai-shi what would be your recommendation to initialize a zustand store from props? We've used
? |
Yeah, that's the way to go. Probably, useRef would be better. |
@TkDodo, here's the zustand guide for initializing a zustand store with props using useRef as Daishi mentioned. In preparation for the removal of @dai-shi, should the React/context migration example include a link to the guide "Initialize State With Props"? It has a nice walkthrough of how to use React Context to init stores. When I was trying to migrate our codebase, my team and I had trouble because the zustand + context documentation was separated in many different spots. What do you think? |
Thanks for the link. Just a small preference: I would choose - const storeRef = useRef<BearStore>()
- if (!storeRef.current) {
- storeRef.current = createBearStore(props)
- }
+ const [store] = useState() => createBearStore(props)) This has only upsides :) |
Would this also be equivalent in behavior? This is stretching my understanding of React's state behavior. - const storeRef = useRef<BearStore>()
- if (!storeRef.current) {
- storeRef.current = createBearStore(props)
- }
+ const store = useRef(createBearStore(props)).current |
no it's not strictly equivalent, because with |
Right, I always forget about lazy init. Thank you! |
I don't prefer using useState for lazy init. |
Is there an easy way to type the line below? I'd like export const useMyStore = (selector) => useStore(useContext(MyContext), selector); |
You don't need to replicate the type, instead: const useMyStore = <T>(selector: (state: MyState) => T) => useStore(useContext(MyContext), selector); |
#1403 (comment)