-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from ShinyLeee/main
[Feat] add tracked hooks selector (big thanks for react-tracked)
- Loading branch information
Showing
8 changed files
with
166 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
"@udecode/zustood": minor | ||
--- | ||
|
||
`react-tracked` support | ||
|
||
Use the tracked hooks in React components, no providers needed. Select your | ||
state and the component will trigger re-renders only if the **accessed property** is changed. Use the `useTracked` method: | ||
|
||
```tsx | ||
// Global tracked hook selectors | ||
export const useTrackedStore = () => mapValuesKey('useTracked', rootStore); | ||
|
||
// with useTrackStore UserEmail Component will only re-render when accessed property owner.email changed | ||
const UserEmail = () => { | ||
const owner = useTrackedStore().repo.owner() | ||
return ( | ||
<div> | ||
<span>User Email: {owner.email}</span> | ||
</div> | ||
); | ||
}; | ||
// with useStore UserEmail Component re-render when owner changed, but you can pass equalityFn to avoid it. | ||
const UserEmail = () => { | ||
const owner = useStore().repo.owner() | ||
// const owner = useStore().repo.owner((prev, next) => prev.owner.email === next.owner.email) | ||
return ( | ||
<div> | ||
<span>User Email: {owner.email}</span> | ||
</div> | ||
); | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ API. | |
- Derived actions | ||
- `immer`, `devtools` and `persist` middlewares | ||
- Full typescript support | ||
- `react-tracked` support | ||
|
||
## Create a store | ||
|
||
|
@@ -40,6 +41,10 @@ import { createStore } from '@udecode/zustood' | |
const repoStore = createStore('repo')({ | ||
name: 'zustood', | ||
stars: 0, | ||
owner: { | ||
name: 'someone', | ||
email: '[email protected]', | ||
}, | ||
}) | ||
``` | ||
|
||
|
@@ -75,6 +80,17 @@ repoStore.use.stars() | |
We recommend using the global hooks (see below) to support ESLint hook | ||
linting. | ||
|
||
### Tracked Hooks | ||
|
||
> Big thanks for [react-tracked](https://github.com/dai-shi/react-tracked) | ||
Use the tracked hooks in React components, no providers needed. Select your | ||
state and the component will trigger re-renders only if the **accessed property** is changed. Use the `useTracked` method: | ||
|
||
```ts | ||
repoStore.useTracked.owner() | ||
``` | ||
|
||
### Getters | ||
|
||
Don't overuse hooks. If you don't need to subscribe to the state, use | ||
|
@@ -182,6 +198,9 @@ export const rootStore = { | |
// Global hook selectors | ||
export const useStore = () => mapValuesKey('use', rootStore); | ||
|
||
// Global tracked hook selectors | ||
export const useTrackedStore = () => mapValuesKey('useTracked', rootStore); | ||
|
||
// Global getter selectors | ||
export const store = mapValuesKey('get', rootStore); | ||
|
||
|
@@ -202,7 +221,32 @@ useStore().modal.isOpen() | |
useStore().repo.middlewares(shallow) | ||
``` | ||
|
||
By using `useStore()`, ESLint will correctly lint hook errors. | ||
### Global tracked hook selectors | ||
|
||
```tsx | ||
// with useTrackStore UserEmail Component will only re-render when accessed property owner.email changed | ||
const UserEmail = () => { | ||
const owner = useTrackedStore().repo.owner() | ||
return ( | ||
<div> | ||
<span>User Email: {owner.email}</span> | ||
</div> | ||
); | ||
}; | ||
|
||
// with useStore UserEmail Component re-render when owner changed, but you can pass equalityFn to avoid it. | ||
const UserEmail = () => { | ||
const owner = useStore().repo.owner() | ||
// const owner = useStore().repo.owner((prev, next) => prev.owner.email === next.owner.email) | ||
return ( | ||
<div> | ||
<span>User Email: {owner.email}</span> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
By using `useStore() or useTrackStore()`, ESLint will correctly lint hook errors. | ||
|
||
### Global getter selectors | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/zustood/src/utils/generateStateTrackedHooksSelectors.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { State } from 'zustand'; | ||
import { GetRecord, UseImmerStore } from '../types'; | ||
|
||
export const generateStateTrackedHooksSelectors = <T extends State>( | ||
store: UseImmerStore<T>, | ||
trackedStore: () => T | ||
) => { | ||
const selectors: GetRecord<T> = {} as any; | ||
|
||
Object.keys(store.getState()).forEach((key) => { | ||
selectors[key] = () => { | ||
return trackedStore()[key as keyof T]; | ||
}; | ||
}); | ||
|
||
return selectors; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters