4.3.0 • Published 8 months ago

sentry-zustand-middleware v4.3.0

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months ago

sentry-zustand-middleware

NPM GitHub contributors npm npm

A Zustand middleware to log state on Sentry

How to use

npm install sentry-zustand-middleware

Basic usage

import create from 'zustand';
import sentryMiddleware from 'sentry-zustand-middleware';

interface BearState {
  bears: number;
  increase: (by: number) => void;
}

const useStore = create<BearState>()(
  sentryMiddleware((set) => ({
    bears: 0,
    increase: (by) => set((state) => ({ bears: state.bears + by })),
  })),
);

export default useStore;

With a state transformer

import create from 'zustand';
import sentryMiddleware from 'sentry-zustand-middleware';

interface BearState {
  bears: number;
  increase: (by: number) => void;
}

const stateTransformer = (state: BearState) => {
  const cleanedState = {
    ...state,
    bears:
      state.bears > 0 ? "There are some bears, but I won't tell you how many!" : 'No bears here',
  };

  // In zustand, actions are accessible from the store's state
  // We might want to remove them before sending the state to Sentry
  return Object.fromEntries(
    Object.entries(cleanedState).filter(
      ([key]) => typeof cleanedState[key as keyof typeof cleanedState] !== 'function',
    ),
  );
};

const useStore = create<BearState>()(
  sentryMiddleware(
    (set) => ({
      bears: 0,
      increase: (by) => set((state) => ({ bears: state.bears + by })),
    }),
    { stateTransformer: stateTransformer },
  ),
);

export default useStore;
4.1.0

8 months ago

4.0.1

8 months ago

4.3.0

8 months ago

4.2.0

8 months ago

4.0.0

1 year ago

3.0.4

2 years ago

3.0.3

2 years ago

3.0.5

2 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.0.1

3 years ago

1.0.0

3 years ago