You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Just wanted to share the setup that I landed on combining the Flux Inspired example and seperate slices. Only because I couldn't find many other examples online. It leverages a cool package I found https://github.com/dhmk083/dhmk-zustand-lens. One of the good thing is under the hood it still uses immer and if you want you can still create global slices. It pretty much just makes it easier to set and get within the slice, not a mega difference but helps if you have some deeper nested states.
The end result I came to was something like.
Wondering if anyone else has some alternative solutions.
// Seperated return type to allow for a different// eg a slice with no lens would be StateSlice<StoreState, INoLensSlice>// since it receives a StoreState get and set but does not have to return the entire store stateexporttypeStateSlice<Textendsobject,Rextendsobject>=(set: SetState<T>,get: GetState<T>)=>R;interfaceIFishSlice{fishes: number;increment: ()=>void;}exportconstfishSlice: StateSlice<IFishSlice,IFishSlice>=(set,get)=>({fishes: 0,increment: ()=>{set({fishes: get().fishes++});},});interfaceIBearSlice{bears: number;decrement: ()=>void;}exportconstbearSlice: StateSlice<IBearSlice,IBearSlice>=(set,get)=>({bears: 0,decrement: ()=>{set({bears: get().bears--});},});// Turn the set method into an immer proxyconstimmer=<TextendsStoreState,CustomSetStateextendsSetState<T>=SetState<T>,CustomGetStateextendsGetState<T>=GetState<T>,CustomStoreApiextendsStoreApi<T>=StoreApi<T>,>(config: StateCreator<T,(partial: ((draft: Draft<T>)=>void)|T,replace?: boolean)=>void,CustomGetState,CustomStoreApi>,): StateCreator<T,CustomSetState,CustomGetState,CustomStoreApi>=>(set,get,api)=>config((partial,replace)=>{constnextState=typeofpartial==='function' ? produce(partialas(state: Draft<T>)=>T) : (partialasT);returnset(nextState,replace);},get,api,);// can lens each entry or leave as blankexportconstuseStore=create<StoreState>(immer(withLenses(()=>({bearSlice: lens<IBearSlice>(createBearSlice),fishSlice: lens<IFishSlice>(createFishSlice),// can add global dispatches here})),),);
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Just wanted to share the setup that I landed on combining the Flux Inspired example and seperate slices. Only because I couldn't find many other examples online. It leverages a cool package I found https://github.com/dhmk083/dhmk-zustand-lens. One of the good thing is under the hood it still uses immer and if you want you can still create global slices. It pretty much just makes it easier to set and get within the slice, not a mega difference but helps if you have some deeper nested states.
The end result I came to was something like.
Wondering if anyone else has some alternative solutions.
Beta Was this translation helpful? Give feedback.
All reactions