-
Hi all! Following the docs I created the following Create a vanilla store: export const initialState = { foo: [] };
export const fooStore = createStore<FooState>((set, getState) => ({
...initialState,
update: (a) => set((state) => ({ foo: [...state.foo, a] }))
})); Create a hook to expose the vanilla store to the react component: export const useBoundStore: UseBoundStore<StoreApi<FooState>> = (
selector,
equals
) => useStore(fooStore, selector, equals); Now I receive a error coming from TypeScript, which I cannot get my head around:
You can find a running example of this in this codesandbox: https://codesandbox.io/s/zustandvanillaslashboundstore-rrvitm?file=/src/MyStore.ts What is going on there? What am I missing? Do I use the wrong types? Am I doing this too complicated/not idiomatic? Any pointer is greatly appreciated! Thanks for your awesome work! Cheers from Düsseldorf Holger |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 26 replies
-
You don't need to use Just typing your function should work, like this: export const useBoundStore = <T>(
selector: (state: FooState) => T,
equals?: (a: T, b: T) => boolean
) => useStore(fooStore, selector, equals); @sewera @chrisk-7777 @dbritto-dev This would be a good one for docs if we don't have it yet. |
Beta Was this translation helpful? Give feedback.
-
I have to reopen this again. While your answer works, there is a tiny issue for me left.
If I want to use the hook without providing a selector function, I get What I can do is provide a second hook, that does not require a selector, or provide a default in the
This does the job, but I get the feeling, that I assume too much of the inner workings of zustand. Please have a look at the sample here: https://codesandbox.io/s/zustandvanillaslashboundstore-rrvitm?file=/src/MyStore.ts:367-538 What do you think @dai-shi @chrisk-7777? Thank you! |
Beta Was this translation helpful? Give feedback.
-
I have tried to do a PR: |
Beta Was this translation helpful? Give feedback.
-
Thanks @dai-shi and @chrisk-7777 for your time, great answers and this outcome! 💪 |
Beta Was this translation helpful? Give feedback.
-
Related to this discussion, I was trying to follow documentation on creating an abstract However, the generated bounded hook in the example doesn't get typed correctly. const createBoundedUseStore = ((store) => (selector, equals) =>
useStore(store, selector as any, equals)) as <S extends StoreApi<unknown>>(
store: S
) => {
(): ExtractState<S>
<T>(
selector?: (state: ExtractState<S>) => T,
equals?: (a: T, b: T) => boolean
): T
}
type ExtractState<S> = S extends { get: () => infer X } ? X : never Has anyone actually gotten this to work as expected? It seems like |
Beta Was this translation helpful? Give feedback.
-
Ran into one more snag here. When memoizing selectors with custom bound hook, I noticed the selector doesn't get typed properly. For example: const memoizedStuff = useMyStore(useCallback(s => s.getStuff(), []));
// const memoizedStuff: any Any ideas on how to fix the useBoundStore types to have this working? |
Beta Was this translation helpful? Give feedback.
You don't need to use
UseBoundStore
type. It's for something else.Just typing your function should work, like this:
@sewera @chrisk-7777 @dbritto-dev This would be a good one for docs if we don't have it yet.