Skip to content
English

Redux

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

Accepts false | ReduxPluginConfig Opt-in
ReduxPluginConfig
OptionTypeDefaultDescription
store requiredReduxStoreLike—The store to inspect. It must be created with xerlockEnhancer().
actionsReduxActionsLike[][]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():

store.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()),
});
index.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.

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

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',
],
},
});

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():

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.

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

ReduxStoreLike
MemberDescription
getState(): unknownReturns the current state.
subscribe(listener: () => void): () => voidCalls the listener after every dispatch, and returns an unsubscribe.
dispatch(action: { type: string }): unknownDispatches an action. Used by the desktop app's Dispatch tab.

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

  • 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.