sentry-zustand-middleware is a specialized middleware for Zustand stores designed to automatically log state changes and actions to Sentry. This package is currently at version 4.3.0 and typically follows the release cycles of its peer dependencies, Zustand and Sentry, to ensure compatibility. Its core function is to capture the state snapshots and the actions that modify them, providing enhanced debugging capabilities and visibility into application state flow during error reporting. A key differentiator is its optional `stateTransformer` function, which allows developers to clean, filter, or obfuscate sensitive data from the Zustand state before it is sent to Sentry, preventing oversharing of private information. It provides a direct integration without manual Sentry calls within every action or state update.
npm install sentry-zustand-middlewareVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic integration of `sentry-zustand-middleware` with a Zustand store, logging state changes and actions to Sentry (assuming Sentry is initialized).
Upgrade `@sentry/browser` and `zustand` to their respective required versions as specified in the `peerDependencies`.
Ensure `Sentry.init()` is called and configured correctly at your application's entry point, prior to any Zustand store creation that uses this middleware.
Implement a `stateTransformer` function in the middleware options to filter, sanitize, or redact sensitive parts of your state before it is sent to Sentry. For example, remove `password` fields or large arrays.
Always provide a descriptive string as the third argument to Zustand's `set` function within your store actions (e.g., `set((state) => ({ count: state.count + 1 }), false, 'incrementCount')`).Call `Sentry.init({ dsn: 'YOUR_DSN' })` at the very beginning of your application's lifecycle, typically in `main.ts` or `App.tsx`.Verify that your `zustand` and `sentry-zustand-middleware` packages are compatible and that you are using the correct import syntax (ESM `import` statements) throughout your project.
Review your `BearState` interface and ensure all properties and methods are correctly defined and that the initial state and `set` calls conform to this interface.