-
Hi, I'm in the process of upgrading from zustand v3 to v4. In v3 I had types working and I could optionally add (devtools) middleware like so: type StoreState = {
foo: string
}
const middleware = subscribeWithSelector(
persist<
StoreState,
SetState<StoreState>,
GetState<StoreState>
>(
(set, get) => ({
foo: 'bar'
}),
{
// persist options
}
)
)
// Optionally include devtools middleware
const createWithMiddleware = () => {
if (process.env.NODE_ENV === 'test') {
return middleware;
} else {
return devtools(middleware);
}
};
export const useStore = create(createWithMiddleware()); However in v4 I can only get the types to pass by following this example: I've tried using ramda pipe, like mentioned in this discussion: #224 (reply in thread) but it seems a bit outdated already and I can't get the typing right. If someone could give me a pointer in the right direction that would be very helpful 🙏🏻 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I also tried with optional |
Beta Was this translation helpful? Give feedback.
-
How about just this... import create from 'zustand'
import { devtools, persist } from 'zustand/middleware'
const envAwareDevtools =
( process.env.NODE_ENV === "production"
? f => f
: devtools
) as typeof devtools
interface BearState {
bears: number
increase: (by: number) => void
}
const useBearStore = create<BearState>()(
envAwareDevtools(
persist((set) => ({
bears: 0,
increase: (by) => set((state) => ({ bears: state.bears + by })),
}))
)
) Now instead of writing |
Beta Was this translation helpful? Give feedback.
How about just this...
Now instead of writing
devtools
you can just writeenvAwareDevtools
instead of doing the unnecessary hassle to type themiddleware
andcreateWithMiddleware
in the original code. Let me know if this doesn'…