A utility library for creating Zustand stores with automatic reset functionality.
- NPM
npm install zustand-with-reset- PNPM
pnpm add zustand-with-reset- Yarn
yarn add zustand-with-reset- Bun
bun add zustand-with-reset- Create a store with automatic reset functionality
import { createWithReset } from "zustand-with-reset";
const store = createWithReset((set) => ({
count: 0,
secondaryCount: 10,
increment: () => set((state) => ({ count: state.count + 1 })),
}));- Use the store you normally would
const count = useStore((state) => state.count);
const increment = useStore((state) => state.increment);- To reset the entire store
const resetStore = useStore((state) => state.resetStore);
//now you can reset the store on unmount or when needed
useEffect(() => {
return () => {
resetStore();
};
}, []);- To reset specific state properties
const resetState = useStore((state) => state.resetState);
//now reset only the count property
resetState("count");
// or reset multiple properties
resetState("count", "secondaryCount");
// alternate syntax
resetState(["count", "secondaryCount"]);