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

Migrate docs from wiki: auto generating selectors #1057

Merged
merged 7 commits into from
Jul 11, 2022

Conversation

Carnageous
Copy link
Contributor

@Carnageous Carnageous commented Jul 5, 2022

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:

  • I removed some semicolons from the wiki page in the last commit (aabaaad). These semicolons are added back in by prettier for some reason
  • The prettier and eslint scripts do not run on windows for me, but they ran fine in a ubuntu wsl.
  • I did not test a lot of the code provided in the examples. Should I do so or is it save to assume that it's still valid?

Looking forward to your suggestions

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 5, 2022

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:

Sandbox Source
React Configuration
React Typescript Configuration
React Browserify Configuration
React Snowpack Configuration
React Parcel Configuration
Next.js Configuration

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.

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.

Carnageous and others added 2 commits July 6, 2022 11:35
Accepted suggestion to remove hint about auto-zustand-selectors-hook

Co-authored-by: Daishi Kato <[email protected]>
@Carnageous Carnageous force-pushed the docs/auto-generating-selectors branch from 7c57b5f to 4e03a4b Compare July 10, 2022 12:50
@Carnageous Carnageous requested a review from dai-shi July 10, 2022 14:32
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.

This looks good. Thanks for working on this.

Added third-party library

Co-authored-by: Daishi Kato <[email protected]>
@dai-shi
Copy link
Member

dai-shi commented Jul 11, 2022

  • I removed some semicolons from the wiki page in the last commit (aabaaad). These semicolons are added back in by prettier for some reason

Those semicolons at the head are correct (with "semi": false). Let me run prettier.

@dai-shi dai-shi merged commit 8e2445c into pmndrs:main Jul 11, 2022
Comment on lines +11 to +36
## 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>
}
```
Copy link
Contributor

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) => ({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)
Copy link
Contributor

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.

@dai-shi
Copy link
Member

dai-shi commented Jul 11, 2022

@Carnageous Hi, can you take @devanshj comments into account?

@Carnageous
Copy link
Contributor Author

Yep, will do! I wrote the docs for the 3.x version, will adjust it in another PR!

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.

3 participants