Closed gauravkumar37 closed 3 years ago
Nice!
If we add CustomSetState
in combine
, can we create withImmer
with combine
?
If we add CustomSetState
in combine
, we would still have to actually call produce
function in combine
in https://github.com/react-spring/zustand/blob/7b2391b74ab19730d10f2709bb91528b838045f8/src/middleware.ts#L111
Is there a way in TS which can help us do that?
Yeah, I mean my question is if we can create withImmer
having combine
in it.
@gauravkumar37 I finally made it.
It was not combine
but StateCreator
that we need to modify.
Check this: https://codesandbox.io/s/beautiful-wilbur-ftvdt
type StateCreator<
T extends State,
CustomSetState = SetState<T>,
U extends State = T
> = (set: CustomSetState, get: GetState<T>, api: StoreApi<T>) => U;
const immer = <T extends State, U extends State>(
config: StateCreator<T, (fn: (draft: T) => void) => void, U>
): StateCreator<T, SetState<T>, U> => (set, get, api) =>
config((fn) => set(produce(fn) as (state: T) => T), get, api);
const combineAndImmer = <
PrimaryState extends State,
SecondaryState extends State
>(
initialState: PrimaryState,
config: StateCreator<
PrimaryState,
(fn: (draft: PrimaryState) => void) => void,
SecondaryState
>
): StateCreator<PrimaryState & SecondaryState> => {
return combine(initialState, immer(config));
};
I have explicitly used the original form of producer with 2 arguments so that no casting is required.
This doesn't seem to work in the codesandbox above. Not sure.
@gauravkumar37 Would you like to continue work on it? I think it's going to produce something good.
@dai-shi Unfortunately I'm not working on React for the time being, so it would be difficult for me to take this forward.
@gauravkumar37 No problem!
Let's leave this issue open for a while and then close.
Has this been solved already?
I'm not sure. We changed types in newer versions, so there might not be any issues.
Nah, the combine with immer still can't infer types(which is reasonable) https://stackblitz.com/edit/react-ts-hnrmpk?file=useCounter.tsx
Basically, I suppose typing issue with combine and other middleware is not solved.
anything I can help with? Testing, example, docs super interested in Poimandres group work
@always-maap sure!
This typing issue would require decent TS knowledge. You can challenge it, if you like.
For other tasks, we want to have more basic and advanced examples in https://github.com/pmndrs/zustand#best-practices (contents are in wiki pages), and
https://github.com/pmndrs/zustand/issues have help wanted
label, as well as Type: Question
and documentation
. They are all waiting for contributors.
@dai-shi was this issue resolved, and how? I'm facing this same issue on latest version of zustand with immer with combine API
This is pretty old. https://github.com/pmndrs/zustand/issues/191#issuecomment-691585896 is the last status. If that doesn't help, please open a new discussion.
I would like to add a TypeScript example which combines
combine
andimmer
usages so that everything is typed, automatically inferred and is intended for use withimmer
. Typing is inferred in state actions as well as when using it in the components.withImmer
should be the innermostmiddleware
and othermiddleware
s can be added on top of it such asdevools
.set
function (replace
) is omitted here, since it doesn't make sense with Immer because the entire object (original/proxied) is returned.shallow
inuseStore(..., shallow)
is also not needed because if the object is not modified, Immer returns the original object and strict equality===
will hold.immer
example, explicit type casting is used because currying is used for theproducer
syntax. I have explicitly used the original form of producer with 2 arguments so that no casting is required.Usage:
Please let me know your thoughts on the same.