Question about memoize selector for many components
See original GitHub issueHello,
type AppStore = {
settings: Partial<GlobalStoryblok>
page: Partial<PageStoryblok>
drawerVariant: DrawerProps['variant']
}
export const useAppStore = create<AppStore>(() => ({
settings: {},
page: {},
drawerVariant: 'temporary'
}))
// memoize the selectors for component-reuse
export const settingsSelector = (state: AppStore) => state.settings
export const pageSelector = (state: AppStore) => state.page
export const pageSettingsSelector = (state: AppStore) => ({
settings: state.settings,
page: state.page
})
// sample component
function Component(){
const settings = useAppStore(settingsSelector)
}
Is this the recommended way of using Zustand with TypeScript? I am migrating away from react-hooks-global-state
and several React context approaches to keep the code more clean. The selectors are re-used over many files in the codebase. Thanks for feedback!
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
How does memoization break on Redux createSelector ...
There are many cases where a selector function needs to be reused across multiple components. If the components will all be calling the...
Read more >Memoization with Selectors in NgRx | by Keerti Kotaru - Medium
NgRx selectors help retrieve subset of the state, specific to the feature or the component in question. They are pure functions that ...
Read more >React re-reselect: Better memoization and cache management
In these cases, we have to use memoization, which will run the selector only when the value of todoList changes in our store....
Read more >Deriving Data with Selectors - Redux
Reselect provides a function called createSelector to generate memoized selectors. createSelector accepts one or more "input selector" functions ...
Read more >Optimizing Selectors - NGXS
Selectors are memoized functions. Memoized functions are calculated when their arguments change and the results are cached. Regardless of how many components or ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
closing as resolved. see also #375.
@dai-shi I was currently using the
shallow
function as second argument. But actually you are right its not necessary and probably with just 3 values in the Store kind of an overhead. Thanks for this lib !