Replies: 3 comments 3 replies
-
This is my current solution, still having a small problem with Object.fromEntries typing const createTestStore = create<TestState>()(
persist(
immer((set) => ({
...initialTestStoreState,
dispatch: (args: TestStoreActions) =>
set((state: TestStoreState) => testStoreReducer(state, args)),
})),
{
name: "test-store",
}
)
);
export function useTestStore<T extends TestStateKeys>(
keys: T[]
): {
[K in T]: TestState[K];
} {
return createTestStore((state) => {
const entries = keys.map((key) => [key, state[key]]);
return Object.fromEntries(entries);
}, shallow);
} Usageconst { count, dispatch } = useTestStore(["count", "dispatch"]); |
Beta Was this translation helpful? Give feedback.
-
Related issue: #685 In short, it's open for 3rd party library. But, there are many ways without middleware, depending on requirements. |
Beta Was this translation helpful? Give feedback.
-
Out of curiosity, what are the tradeoffs to consider here? Just want to make sure I understand the implications before I default to To me, the convenience of being able to destructure multiple values like
is great, and it seems to me like this convenience outweighs the slight performance gain of picking values one-by-one and using Am I missing some pros/cons, or underestimating the performance difference here? |
Beta Was this translation helpful? Give feedback.
-
Is it possible to create a store what is shallow by default?
Beta Was this translation helpful? Give feedback.
All reactions