• Construct a new StateNodeConfig (suitable for passing to createMachine) by walking an existing StateMachine (e.g. the output of createMachine) and replacing each state with the output of the supplied mapper applied to that state.

    Example

    Update state metadata:

    import { createMachine } from "xstate";
    import { mapStates } from "@simplystated/f-of-xstate";
    const machine = createMachine(...);
    const config = mapStatesFromDefinition(
    machine,
    (state) => ({
    ...state,
    meta: {
    ...state.meta,
    stateId: state.id
    }
    })
    );
    const updatedMachine = createMachine(config);

    See

    MapStatesMapper.

    Returns

    The new MachineConfig resulting from applying mapper to each state.

    Type Parameters

    • TContext

    • TStateSchema extends StateSchema<any, TStateSchema>

    • TEvent extends EventObject

    Parameters

    • root: StateMachine<TContext, TStateSchema, TEvent, {
          context: TContext;
          value: any;
      }, BaseActionObject, ServiceMap, ResolveTypegenMeta<TypegenDisabled, NoInfer<TEvent>, BaseActionObject, ServiceMap>>

      StateMachine or StateNode to map over.

    • mapper: MapStatesMapper<TContext, TStateSchema, TEvent>

      Function that maps the existing node and a state path to a possibly-updated new state.

      Generally, mapper should return a modified copy of the provided state. E.g. with (state) => ({ ...state, modifications: "here" }) You should likely have lots of ...s and concats to ensure that you are preserving the parts of the source state that you aren't direclty modifying.

    Returns MachineConfig<TContext, TStateSchema, TEvent, BaseActionObject, ServiceMap, TypegenDisabled>

Generated using TypeDoc