Skip to main content

Slice

A Slice is an object that looks like:

{    addCaseReducers: (caseReducers: CaseReducers) => CaseReducerActions,    addExtraReducers: (reducers: Reducers) => void,    createAction: (actionKey: string) => PayloadActionCreator;    getActionType: (actionKey: string) => string,    name: string,    parentPath: string,    reducer: Reducer,    selector: Selector,    selectors: Selectors,}

Properties#

addCaseReducers(caseReducers)#

The caseReducers argument is an object with keys actionKey strings and values case reducer functions. addCaseReducers generates an actionType for each actionKey. The actionTypes are formatted based on the Slice's actionTypeFormat config option. Then addCaseReducers generates an action creator function for each actionType. The type of the payload will be based on the type expected in the case reducer function. addCaseReducers returns an "actions" object with keys ${actionKey}Action and values are the action creator function. The action creator functions are generated using the Redux Toolkit's createAction() function.

addExtraReducers(extraReducers)#

Like caseReducers, extraReducers should be an object containing Redux case reducer functions. However, the keys should be actionType string constants. addExtraReducers does not generate action creator functions and it has no return value.

createAction(actionKey)#

A utility function to create an action creator function for the given actionKey string.
It calls getActionType(actionKey) and then calls createAction function from Redux Toolkit.
The action creator function accepts a single argument, which will be included in the action object as a field called payload.

getActionType(actionKey)#

Given an actionKey string returns an actionType.
The actionType is formatted for uniqueness and readability in the Redux Dev Tools.
The format is based on the Slice's actionTypeFormat config option.

name#

The Slice's name.

parentPath#

The Slice's location in the redux state. The value is based on the Slice's name and the value of the Slice's parent config option.

reducer#

The Slice's reducer.
Useful in unit tests or when the Slice's parent config option was not set to a SliceParent but to the parent's path and therefore one must explicitly add the Slice's reducer to a parent.

selector#

The Slice's selector. Given the Store's state it returns the slice of state.
If in the Store's state the slice of state is undefined, then it returns the value defined in the Slice's initialState config option.
A Slice may exist under one or more nested SliceGroups. The Slice's selector uses the parent's path to walk up the ancestors SliceGroup names and return the slice of state or the initialState if undefined.

selectors#

When initialState is an object, selectors is an object with keys ${propertyName}Selector for each property name in the initialState object, and values Selector functions that return the property's value.