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(core): risks of omitting useStore selector #1126

Merged
merged 1 commit into from
Feb 5, 2025

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


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 highlighted this potential pitfall.

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

Screenshot 2025-01-22 at 15 07 07

@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. 🤟

@crutchcorn crutchcorn merged commit 2bd0696 into TanStack:main Feb 5, 2025
1 check passed
@harry-whorlow harry-whorlow changed the title feat(docs): risks of omitting useStore selector docs(core): risks of omitting useStore selector Mar 5, 2025
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