zustand-slices is a utility library designed to introduce an opinionated, TypeScript-friendly slice pattern for Zustand, a minimalist global state management library. As of version 0.4.0, it provides `createSlice` and `withSlices` helpers to structure Zustand stores into modular, reusable "slices." This addresses the complexities often encountered when attempting to implement such patterns with strong TypeScript typing, particularly when following the official Zustand documentation's manual approach. The library is actively developed, with its primary maintainer frequently tweeting about updates and examples, suggesting a consistent, though not strictly scheduled, release cadence focusing on refinement and new features. Its key differentiator is its explicit support for type inference and clean separation of concerns within a Zustand store, leveraging immutable updates via Immer, which is a peer dependency.
npm install zustand-slicesVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define individual state slices using `createSlice`, combine them into a single Zustand store with `withSlices`, and consume both state and actions within a React component. It also highlights distinct reset actions for each slice to avoid naming conflicts.
Explicitly install Immer in your project: `npm install immer` or `yarn add immer`.
Prefix action names (e.g., `resetCount`, `resetText`) or implement a single, combined action that dispatches to specific slice actions if a global reset is desired.
Always use Immer's `draft` parameter if available, or return a new state object (e.g., `(prev) => ({ ...prev, value: newValue })`) for updates to ensure immutability.Consult the official Zustand TypeScript guide and examples for best practices. Ensure middleware is applied at the combined store level, not within individual slices, for predictable typing.
Install Immer: `npm install immer` or `yarn add immer`.
Carefully review your `StateCreator` generics and ensure consistency across slices and the main store. Explicitly typing `createSlice<T>()(...)` can help guide inference.
Verify that all slices are correctly passed to `withSlices` and that the property name matches the slice's defined state. Ensure your TypeScript configuration is strict enough to catch such issues.