zustand-immer-store
v0.9.6
Published
A ~268B library to create type-safe redux-style stores with Zustand and Immer
Downloads
33
Maintainers
Readme
Zustand Immer Store
A ~268B library to create type-safe redux-style stores with Zustand and Immer
Installation
yarn add zustand-immer-store
Usage
// counter-store.ts
import { createStore } from "zustand-immer-store";
const useCounterStore = createStore(
{ counter: 0 },
{
createActions: (set) => ({
increment: () =>
set((draft) => {
draft.state.counter++;
}),
decrement: () =>
set((draft) => {
draft.state.counter--;
}),
}),
}
);
export default useCounterStore;
// App.tsx
import useCounterStore from "./counter-store";
export default function App() {
const { state, actions } = useCounterStore();
return (
<main>
<div>
<button onClick={actions.decrement}> - </button>
<div>{state.count}</div>
<button onClick={actions.increment}> + </button>
</div>
</main>
);
}