question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question about memoize selector for many components

See original GitHub issue

Hello,

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:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, May 5, 2021

closing as resolved. see also #375.

1reaction
dohomicommented, Jan 8, 2021

@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 !

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found