-
-
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: corrected example #2717
docs: corrected example #2717
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
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. |
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.
Thanks.
I found another issue.
The following is incorrect. We need createWithEqualityFn
to make it work.
For more control over re-rendering, you may provide any custom equality function.
const treats = useBearStore(
(state) => state.treats,
(oldTreats, newTreats) => compare(oldTreats, newTreats),
)
readme.md
Outdated
const useBearStore = create(set => ({ | ||
nuts: 0, | ||
honey: 0, | ||
treats: {}, | ||
increaseNuts: () => set(state => ({ nuts: state.nuts + 1 })), | ||
increaseHoney: () => set(state => ({ honey: state.honey + 1 })), | ||
increaseTreats: (treat: string, count?: number) => | ||
set(state => ({ treats: { ...state.treats, [treat]: (state.treats[treat] ?? 0) + 1 } })), | ||
})) |
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.
Hi, thanks for the suggestion. I didn't realize this.
We don't want to increase lines readme.md (and possibly move detail/complete examples to docs),
so can we make this simpler??
const useBearStore = create(set => ({ | |
nuts: 0, | |
honey: 0, | |
treats: {}, | |
increaseNuts: () => set(state => ({ nuts: state.nuts + 1 })), | |
increaseHoney: () => set(state => ({ honey: state.honey + 1 })), | |
increaseTreats: (treat: string, count?: number) => | |
set(state => ({ treats: { ...state.treats, [treat]: (state.treats[treat] ?? 0) + 1 } })), | |
})) | |
const useBearStore = create((set) => ({ nuts: 0, honey: 0, treats: {} /* ... */ })) |
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.
Thanks, and I agree. The update functions are unnecessary, so I removed them and added a ...
, which I think is clear enough. I also added a link to what documentation I could find for createWithEqualityFn
.
I'm afraid I don't have the bandwidth to go deeper and break this into its own file, etc. Hopefully this will be sufficient for now. Thanks for the feedback!
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.
Cool! Thanks for your contribution!
Summary
Corrected store shape in "Selecting multiple state slices" to be consistent with the example (which use
nuts
,honey
, andtreats
instead ofbears
).Check List
pnpm run prettier
for formatting code and docs