Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wiredprairie/65d6f30e23e9995b79c27ad1d62dcc83 to your computer and use it in GitHub Desktop.
Save wiredprairie/65d6f30e23e9995b79c27ad1d62dcc83 to your computer and use it in GitHub Desktop.
Experiment with Zustand initializer
function initStore<StoreTypes, Store extends UseBoundStore<StoreApi<unknown>>>(
store: Store,
compare?: (a: StoreTypes, b: StoreTypes) => boolean
) {
return (newValue: StoreTypes, defaultValue: StoreTypes) => {
if (compare && compare(newValue, store.getState() as unknown as StoreTypes)) {
return
}
store.setState(newValue as any)
}
}
const pageStoreInit = initStore<PageState, typeof usePageStore>(usePageStore, (a, b) => {
return a.design.design_id === b.design.design_id
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment