Redux Integration

Learn about Sentry's Redux integration.

(Available in version 1.8.0 and above)

Redux support is included in the @sentry/react-native package, version 1.8.0 and up. To apply Sentry to Redux, use Sentry.createReduxEnhancer in the same place you'd initialize in your Redux store.

Copied
import { configureStore, createStore, compose } from "redux";
import * as Sentry from "@sentry/react-native";

// ...

const sentryReduxEnhancer = Sentry.createReduxEnhancer({
  // Optionally pass options listed below
});

// If you are using the `configureStore` API, pass the enhancer as follows:
const store = configureStore({
  reducer,
  enhancers: (getDefaultEnhancers) => {
    return getDefaultEnhancers().concat(sentryReduxEnhancer);
  },
});

// If you are using the deprecated `createStore` API, pass the enhancer as follows:
const store = createStore(reducer, sentryReduxEnhancer);

If you are using the deprecated createStore API and have other enhancers or middleware such as thunk:

Copied
const store = createStore(
  rootReducer,
  compose(applyMiddleware(thunk), sentryReduxEnhancer),
);

By default, Sentry SDKs normalize any context to a depth of 3. You may want to increase this for sending Redux states by passing normalizeDepth to the Sentry.init call:

Copied
Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  normalizeDepth: 10, // Or however deep you want your state context to be.
});

To configure enhancer options, pass an options object as the first parameter to Sentry.createReduxEnhancer.

actionTransformer (Function)

This can be used to remove sensitive information from actions. The first parameter passed to the function is the Redux action. We send all actions by default, if you don't want an action sent to Sentry, use return null.

Copied
const sentryReduxEnhancer = Sentry.createReduxEnhancer({
  actionTransformer: (action) => {
    if (action.type === "GOVERNMENT_SECRETS") {
      // Return null to not log the action to Sentry
      return null;
    }
    if (action.type === "SET_PASSWORD") {
      // Return a transformed action to remove sensitive information
      return {
        ...action,
        password: null,
      };
    }

    return action;
  },
});

stateTransformer (Function)

This can be used to remove sensitive information from state. The first parameter passed to the function is the Redux state. We attach all state changes by default. If you don't want to attach state changes to events being sent to Sentry, use return null. Note, that if you choose not to send state to Sentry, your errors might not have the latest version attached.

Copied
const sentryReduxEnhancer = Sentry.createReduxEnhancer({
  stateTransformer: (state) => {
    if (state.topSecret.doNotSend) {
      // Return null to not send this version of the state.
      return null;
    }

    // Transform the state to remove sensitive information
    const transformedState = {
      ...state,
      topSecret: {
        ...state.topSecret,
        // Replace sensitive information with something else
        nuclearLaunchCodes: "I love pizza",
        // or just remove it entirely
        hiddenTreasureLocation: null,
      },
      // You should also remove large data that is irrelevant to debugging to not clutter your Sentry issues
      giganticState: null,
    };

    return transformedState;
  },
});

configureScopeWithState (Function)

This is called on every state update. To use it, configure the Sentry Scope with the Redux state. The first parameter is the scope, which is the same scope instance that you would get when you call Sentry.configureScope. The second parameter is the latest Redux state.

Copied
const sentryReduxEnhancer = Sentry.createReduxEnhancer({
  configureScopeWithState: (scope, state) => {
    // Set tag if the user is using imperial units.
    if (state.settings.useImperialUnits) {
      scope.setTag("user.usesImperialUnits", true);
    }
  },
});

attachReduxState (Boolean)

(Available in version 7.69.0 and above)

This is true by default. It attaches a file redux_state.json with Redux state to all error events sent to Sentry. It is beneficial where Redux states are too big. If the stateTransformer function is provided, it will attach the transformed state. If you don't want to attach the state to error events, set this to false.

Copied
const sentryReduxEnhancer = Sentry.createReduxEnhancer({
  attachReduxState: false,
});
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").