Sometimes you only want to consume a feature in a specific state, or states. This can be in an other feature or in a UI component. In these scenarios you should avoid using the hook and rather get the state of the feature as a prop from the outside.
There are situations when the action handlers in the reducer needs access to state from other features or props. To avoid duplicating state you can make the reducer dynamic.
Sometimes you might have one or multiple action handlers across states. You can lift them up and compose them back into your transitions.
There are three parts to this patterns:
Only type what you need from an action, not the actions themselves
If you are consuming the current state, type it as any context (State) and optionally restrict it with properties and values you want to be available on that state
Always give Transition as the return type
You can define a single event handler:
import { createReducer } from 'react-states';
const handleChangeDescription = (
// Expressing that we allow any context, as long as it has "description" on it
state: State & { description: string },
// Expressing what we want from the event
{ description }: { description: string },
// Allowing us to move into any state
): Transition => ({
...state,
description,
});
const reducer = createReducer<Feature>({
FOO: {
DESCRIPTION_CHANGED: handleChangeDescription,
},
BAR: {
DESCRIPTION_CHANGED: handleChangeDescription,
},
});
This expresses the simplest states first, then indents the states using the base state. This ensures that you see these states related to their base and with their indentation they have "additional meaning".
Nested states
You do not have to express the whole state at the root, you can split it up into nested states.