zustand-mutative is a middleware for Zustand that integrates the Mutative library, enabling efficient and convenient immutable state updates using a mutable syntax. It allows developers to modify Zustand state directly, similar to libraries like Immer, but claims significantly higher performance (2-6x faster than spread operations, over 10x faster than `zustand/middleware/immer`). The current stable version is 1.3.1. Releases appear to be driven by dependency updates and minor fixes, with new versions roughly every few months, often coinciding with updates to `mutative` or `zustand` itself. Its key differentiator is its performance advantage over other immutable update helpers within the Zustand ecosystem while providing a familiar mutable-style API for state management.
npm install zustand-mutativeVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a Zustand store using `zustand-mutative` middleware, enabling direct, mutable-style updates to an immutable state object, including primitive values and array/object mutations.
Ensure your project's `zustand` and `mutative` versions meet the peer dependency requirements specified in `zustand-mutative`'s `package.json` (e.g., `zustand: ^4.0 || ^5.0`, `mutative: ^1.3.0`).
Within the `set` callback provided to `mutative`, ensure all state changes are done by directly mutating the `state` parameter, e.g., `state.count += 1;`.
Evaluate the complexity of your state updates. For simple, shallow changes, direct `set` with spread syntax is an option. For nested, complex, or performance-critical immutable updates, `zustand-mutative` is recommended.
If encountering peer dependency issues with React, ensure your React version is within the supported range (`^17.0 || ^18.0 || ^19.0`). Consider using `legacy-peer-deps` or similar options with your package manager temporarily if you are unable to upgrade React immediately.
Ensure you are importing the `mutative` middleware from `zustand-mutative` like so: `import { mutative } from 'zustand-mutative';`.Ensure the `mutative` middleware is correctly wrapped around your store definition. If you've enabled `autoFreeze` in Mutative options, all mutations must occur within the `set` callback provided by `zustand-mutative`. Check Mutative's strict mode documentation for more advanced debugging.
Ensure `zustand-mutative` is correctly installed. For `NodeNext` resolution issues, verify your `tsconfig.json` and `package.json` (`exports` field) are configured correctly, or update to `zustand-mutative@^1.2.1` or later which includes fixes for these types of import issues.