A basic Zustand store inherently requires devs to repeat themselves. Workaround? #3201
Unanswered
Johnrobmiller
asked this question in
Q&A
Replies: 1 comment
-
|
I'm not sure if you need to repeat it. import { create } from "zustand";
interface CounterState {
doSomething: (someArg: {
example: number | string;
anotherExample: string;
}) => { example: number; anotherExample: string };
}
export const useCounterStore = create<CounterState>()((set) => ({
doSomething: (someArg) => {
// ...something is done
return { example: 5, anotherExample: "6" };
},
})); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This issue is not a huge deal, and I still love Zustand regardless for simplifying state management. However, with this said, below is an example Zustand store. Notice how the developer has to "repeat themselves" by explictly telling Zustand what the type of the "doSomething" function is even though typescript already implicitly knows what the type of this function is. Is there any way for Zustand to just automagically "know" what the types are without needing to explictly say what they are?
EDIT: I'm seeing that this "repeating yourself" is standard React practice for useState() even without Zustand. So I'm beginning to this that is is more fundamentally a problem with React itself.
Beta Was this translation helpful? Give feedback.
All reactions