Created
May 5, 2023 18:58
-
-
Save wiredprairie/65d6f30e23e9995b79c27ad1d62dcc83 to your computer and use it in GitHub Desktop.
Experiment with Zustand initializer
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
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