
Zustand Has a Free API You're Not Using
Zustand is the simplest React state manager — just a hook. But most developers miss the powerful middleware and patterns that make it a full Redux replacement. The Free APIs You're Missing 1. Slices Pattern — Modular Store import { create , StateCreator } from " zustand " ; interface AuthSlice { user : User | null ; login : ( email : string , password : string ) => Promise < void > ; logout : () => void ; } interface CartSlice { items : CartItem []; addItem : ( item : CartItem ) => void ; total : () => number ; } const createAuthSlice : StateCreator < AuthSlice & CartSlice , [], [], AuthSlice > = ( set ) => ({ user : null , login : async ( email , password ) => { const user = await api . login ( email , password ); set ({ user }); }, logout : () => set ({ user : null }), }); const createCartSlice : StateCreator < AuthSlice & CartSlice , [], [], CartSlice > = ( set , get ) => ({ items : [], addItem : ( item ) => set (( s ) => ({ items : [... s . items , item ] })), total : () => get ().
Continue reading on Dev.to React
Opens in a new tab

