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

docs: corrected example #2717

Merged
merged 4 commits into from
Sep 2, 2024
Merged

docs: corrected example #2717

merged 4 commits into from
Sep 2, 2024

Conversation

EthanRBrown
Copy link
Contributor

Summary

Corrected store shape in "Selecting multiple state slices" to be consistent with the example (which use nuts, honey, and treats instead of bears).

Check List

  • pnpm run prettier for formatting code and docs

Copy link

vercel bot commented Sep 1, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
zustand-demo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 2, 2024 11:48pm

Copy link

codesandbox-ci bot commented Sep 1, 2024

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.

Copy link
Member

@dai-shi dai-shi left a 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
Comment on lines 93 to 101
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 } })),
}))
Copy link
Member

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??

Suggested change
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: {} /* ... */ }))

Copy link
Contributor Author

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!

Copy link
Member

@dai-shi dai-shi left a 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!

readme.md Outdated Show resolved Hide resolved
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

Successfully merging this pull request may close these issues.

2 participants