-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Migrate docs from wiki: auto generating selectors #1057
Migrate docs from wiki: auto generating selectors #1057
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 1c41c4e:
|
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.
I find it tough to add unmaintained wiki page to docs, but that's the point. We want to maintain it right? So, it requires some efforts.
Please create a codesandbox in TypeScript and try if the description really works.
Accepted suggestion to remove hint about auto-zustand-selectors-hook Co-authored-by: Daishi Kato <[email protected]>
7c57b5f
to
4e03a4b
Compare
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.
This looks good. Thanks for working on this.
Added third-party library Co-authored-by: Daishi Kato <[email protected]>
Those semicolons at the head are correct (with |
## create the following function: `createSelectors` | ||
|
||
```typescript | ||
import { State, StoreApi, UseBoundStore } from 'zustand' | ||
|
||
interface Selectors<StoreType> { | ||
use: { | ||
[key in keyof StoreType]: () => StoreType[key] | ||
} | ||
} | ||
|
||
export default function createSelectors<StoreType extends State>( | ||
store: UseBoundStore<StoreType, StoreApi<StoreType>> | ||
) { | ||
// Casting to any to allow adding a new property | ||
;(store as any).use = {} | ||
|
||
Object.keys(store.getState()).forEach((key) => { | ||
const selector = (state: StoreType) => state[key as keyof StoreType] | ||
;(store as any).use[key] = () => store(selector) | ||
}) | ||
|
||
return store as UseBoundStore<StoreType, StoreApi<StoreType>> & | ||
Selectors<StoreType> | ||
} | ||
``` |
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.
This code should look like this instead.
increment: () => void | ||
} | ||
|
||
const useStoreBase = create<BearState>((set) => ({ |
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.
const useStoreBase = create<BearState>((set) => ({ | |
const useStoreBase = create<BearState>()((set) => ({ |
|
||
## Live Demo | ||
|
||
for a working example of this, see the [Code Sandbox](https://codesandbox.io/s/zustand-auto-generate-selectors-9i0ob3?file=/src/store.ts:396-408) |
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.
Probably will have to update the sandbox based on the review.
@Carnageous Hi, can you take @devanshj comments into account? |
Yep, will do! I wrote the docs for the 3.x version, will adjust it in another PR! |
Like you said in #1008 you want to migrate the docs from Github's wiki to the docs folder as markdown files and I'd like to help! This one is for Auto generating selectors.
I will work on all the pages in my fork and open PRs if that's fine?
A few things:
Looking forward to your suggestions