Redux

Adding the @sophonz/react-native-redux middleware to a Redux Toolkit store, with or without hooks, and exactly what each dispatched action records.

@sophonz/react-native-redux is a Redux middleware that records a span for every dispatched action: its type, how long the reducers and the rest of the middleware chain took, whether it threw, and the size of its payload — not the payload itself. The package is pure JavaScript and records through the tracer provider.

Install

npm install @sophonz/react-native-redux@0.1.0 @sophonz/react-native-tracer-provider@0.1.0

@reduxjs/toolkit is a peer dependency; the middleware is typed with its Middleware type and added through configureStore.

With hooks

The middleware can only be created once the tracer provider exists, which is after the SDK has started. Create the store when middleware becomes available:

App.tsx
import {useEffect, useState} from "react";
import {Provider} from "react-redux";
import {configureStore, type EnhancedStore} from "@reduxjs/toolkit";
import {useSophonz} from "@sophonz/react-native";
import {useSophonzNativeTracerProvider} from "@sophonz/react-native-tracer-provider";
import {useSophonzMiddleware} from "@sophonz/react-native-redux";
 
import {rootReducer} from "./store/rootReducer";
 
const SOPHONZ_CONFIG = {trackUnhandledRejections: true};
 
export default function App() {
  const {isStarted} = useSophonz(SOPHONZ_CONFIG);
  const {tracerProvider} = useSophonzNativeTracerProvider(undefined, isStarted);
  const {middleware} = useSophonzMiddleware(tracerProvider);
  const [store, setStore] = useState<EnhancedStore>();
 
  useEffect(() => {
    if (middleware && !store) {
      setStore(
        configureStore({
          reducer: rootReducer,
          middleware: getDefaultMiddleware => getDefaultMiddleware().concat(middleware),
        }),
      );
    }
  }, [middleware, store]);
 
  if (!store) {
    return null; // or a splash screen
  }
 
  return <Provider store={store}>{/* ... */}</Provider>;
}

Put the Sophonz middleware last in the chain, as above, so its span covers the reducers and nothing before it. Middleware placed after it — for example one that turns a thunk into several actions — is timed as part of the outer action.

Without hooks

If the store is created outside React, start the SDK first and create the provider and the middleware directly:

store.ts
import {configureStore} from "@reduxjs/toolkit";
import {initialize} from "@sophonz/react-native";
import {SophonzNativeTracerProvider} from "@sophonz/react-native-tracer-provider";
import {createSophonzMiddleware} from "@sophonz/react-native-redux";
 
import {rootReducer} from "./rootReducer";
 
export const createStore = async () => {
  await initialize({sdkConfig: {trackUnhandledRejections: true}});
 
  const tracerProvider = new SophonzNativeTracerProvider();
 
  return configureStore({
    reducer: rootReducer,
    middleware: getDefaultMiddleware =>
      getDefaultMiddleware().concat(createSophonzMiddleware(tracerProvider)),
  });
};

new SophonzNativeTracerProvider() does not check that the SDK is running. Creating it before initialize resolves gives spans the native side cannot record.

What is recorded

One span per action that has a type. Anything else passes through untouched.

FieldValue
Span nameaction
spz.typesys.rn_action
nameThe action type, such as cart/itemAdded
payload_sizeSize in bytes of the gzip-compressed JSON of the action without its type
outcomeincomplete at the start; success when next(action) returned, fail when it threw
action.stateAppState.currentState when the action finished: active, background or inactive
DurationFrom dispatch until next(action) returns

When a reducer or later middleware throws, the span ends with outcome: fail and the error is rethrown unchanged. The span status is not set to ERROR.

The action payload never leaves the device. Only its compressed size is recorded, which is enough to spot an action that suddenly carries far more data than usual.

Async work

The span covers the synchronous dispatch only. For createAsyncThunk, that means three short spans — pending, fulfilled or rejected — rather than one span for the whole request. The request itself is a network span. To time the whole operation, wrap the thunk body in a custom span.

Volume

Every dispatch makes three native calls: start the span, set the outcome, end it. An app that dispatches on every keystroke or animation frame produces a matching stream of spans. If that is your app, record only the store in which actions are meaningful, or use custom spans for the flows that matter instead.

API

APIDescription
useSophonzMiddleware(tracerProvider?)Returns {middleware}: undefined until tracerProvider is set, then a middleware that stays the same across renders
createSophonzMiddleware(tracerProvider)The middleware, created immediately

The tracer is named @sophonz/react-native-redux with version 0.1.0.