# Redux

> Browse and edit your Redux state, review every action with a diff, time travel, and dispatch actions from the desktop app. It is off until you pass your store.

The Redux panel has three tabs. **State** shows your store’s current state and lets you edit its values. **Actions** lists every action with a diff, and can unwind the store to any of them. **Dispatch** sends an action with a payload you enter.

[Using Redux in the desktop app](/docs/desktop/redux/)

## Setup

**Accepts** `false | ReduxPluginConfig` Opt-in

| Option           | Type                 | Default | Description                                                                                                                                        |
| ---------------- | -------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
| `store` required | `ReduxStoreLike`     | —       | The store to inspect. It must be created with `xerlockEnhancer()`.                                                                                 |
| `actions`        | `ReduxActionsLike[]` | `[]`    | Action types to offer in the Dispatch tab: a slice's `.actions`, action creators, `{ type }` objects, type strings, or `dispatchable()` templates. |

Create your store with `xerlockEnhancer()`, then pass it to `init()`:

* Redux Toolkit

  *store.ts*

  ```ts
  import { configureStore, createSlice, type PayloadAction } from '@reduxjs/toolkit';
  import { xerlockEnhancer } from '@xerlock/client';

  export const caseSlice = createSlice({
    name: 'case',
    initialState: { caseNumber: 221, solved: false },
    reducers: {
      opened: (state, action: PayloadAction<number>) => {
        state.caseNumber = action.payload;
        state.solved = false;
      },
      closed: (state) => {
        state.solved = true;
      },
    },
  });

  export const suspectsSlice = createSlice({
    name: 'suspects',
    initialState: { names: [] as string[] },
    reducers: {
      added: (state, action: PayloadAction<string>) => {
        state.names.push(action.payload);
      },
      cleared: (state) => {
        state.names = [];
      },
    },
  });

  export const store = configureStore({
    reducer: {
      case: caseSlice.reducer,
      suspects: suspectsSlice.reducer,
    },
    enhancers: (getDefaultEnhancers) => getDefaultEnhancers().concat(xerlockEnhancer()),
  });
  ```

  Show the full example

* Plain Redux

  *store.ts*

  ```ts
  import { xerlockEnhancer } from '@xerlock/client';
  import { legacy_createStore as createStore, type Reducer } from 'redux';

  type EvidenceState = { items: string[] };

  const evidence: Reducer<EvidenceState> = (state = { items: [] }, action) =>
    action.type === 'evidence/logged' && typeof action.payload === 'string'
      ? { items: [...state.items, action.payload] }
      : state;

  export const store = createStore(evidence, xerlockEnhancer());
  ```

*index.ts*

```ts
import { init } from '@xerlock/client';

import { store } from './store';

init({
  redux: { store },
});
```

Without `xerlockEnhancer()`, the Redux panel stays hidden. With other enhancers, `xerlockEnhancer()` must come after `applyMiddleware`, as `getDefaultEnhancers().concat()` does. Otherwise actions that middleware dispatches, such as those from a thunk, aren’t logged.

### Offering actions to dispatch

The Dispatch tab offers every action type it has seen in the log. To offer types before they’re dispatched, list them in `actions`:

```ts
import { createAction } from '@reduxjs/toolkit';
import { init } from '@xerlock/client';

import { caseSlice, store, suspectsSlice } from './store';

const caseReopened = createAction('case/reopened');

init({
  redux: {
    store,
    actions: [
      caseSlice.actions,
      suspectsSlice.actions.added,
      caseReopened,
      { type: 'evidence/cleared' },
      'app/reset',
    ],
  },
});
```

Show the full example

Each entry can be a slice’s `.actions`, an action creator, a `{ type }` object or a type string. An entry without a string `type`, such as a thunk or a helper that dispatches, is skipped with a warning in the console. List the action creators it dispatches instead.

To prefill an action’s payload, wrap its creator with `dispatchable()`:

```ts
import { dispatchable, init } from '@xerlock/client';

import { store, suspectsSlice } from './store';

const { added } = suspectsSlice.actions;

init({
  redux: {
    store,
    actions: [dispatchable(added, { payload: 'Irene Adler' })],
  },
});
```

The template is checked against the action the creator returns, without its `type`.

`xerlockEnhancer()` does nothing in production builds.

Caution

Update state immutably, as Redux expects. Redux Toolkit does this for you. The SDK keeps each state you replace for diffs and time travel, so changing an object in place changes the history too.

### Shapes

The SDK doesn’t depend on Redux. Any store with this shape works, as long as it was created with `xerlockEnhancer()`:

| Member                                        | Description                                                          |
| --------------------------------------------- | -------------------------------------------------------------------- |
| `getState(): unknown`                         | Returns the current state.                                           |
| `subscribe(listener: () => void): () => void` | Calls the listener after every dispatch, and returns an unsubscribe. |
| `dispatch(action: { type: string }): unknown` | Dispatches an action. Used by the desktop app's Dispatch tab.        |

### Turning it off

Leave out `redux`, or pass `false`, to hide the Redux panel for this app.

## Limits and Troubleshooting

* **The Redux panel is missing**: pass `redux` to `init()`, and create the store with `xerlockEnhancer()`.
* **Actions from thunks or middleware are missing**: put `xerlockEnhancer()` after `applyMiddleware`.
* **A type isn’t offered on the Dispatch tab**: check the console for a warning that its entry in `actions` has no `type`.
