Skip to content

How to display the current state (string) #5268

Answered by davidkpiano
RemyMachado asked this question in Q&A
Discussion options

You must be logged in to vote

The state value can be a string or an object. If you have a flat state machine (no nested states, then state.value is a string. Otherwise, it's an object and it's up to you to display that as a string however you see fit.

One such way is this: https://stately.ai/docs/migration#the-statetostrings-method-has-been-removed

import { type StateValue } from 'xstate';

export function getStateValueStrings(stateValue: StateValue): string[] {
  if (typeof stateValue === 'string') {
    return [stateValue];
  }
  const valueKeys = Object.keys(stateValue);

  return valueKeys.concat(
    ...valueKeys.map((key) =>
      getStateValueStrings(stateValue[key]!).map((s) => key + '.' + s),
    ),
  );
}

/…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by RemyMachado
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants