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

feat(docs): risks of omitting useStore selector #1126

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

harry-whorlow
Copy link
Contributor

@harry-whorlow harry-whorlow commented Jan 22, 2025

From issue #1122 and a conversation with @crutchcorn, omitting the selector prop of the useStore hook will result in unnecessary re-renders.

Screenshot 2025-01-22 at 15 06 57

Screenshot 2025-01-22 at 15 07 07


The interface of useStore shows it as an optional prop, and while the hook will work without the prop, it is not the recommended approach.

export declare function useStore<TState, TSelected = NoInfer<TState>>(store: Store<TState, any>, selector?: (state: NoInfer<TState>) => TSelected): TSelected;

I've searched the Docs for references to this, and the only documentation relating to this hook was under Reactivity of basic concepts. However, it does not highlight the risks of omitting selector.

Screenshot 2025-01-22 at 15 13 36

The only other references to this hook I found in the documentation were examples demonstrating the use of useStore however, none of them highlight this potential pitfall.

This PR adds clarification of useStore and examples of correct use.

@harry-whorlow
Copy link
Contributor Author

Personally I feel it might be good to have a section for the library's hooks in the docs, with documentation on their use and explanation of their props, kind of like the api-reference but a bit more human friendly.

But, for now this is a good stop gap. 🤟

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.

1 participant