Skip to content

Method to check if transition is traversable #200

@tacomanator

Description

@tacomanator

Hello and thank you for this library.

Have you considered providing a method to check if a transition may be traversed (i.e. exists and would pass the guards) without changing the current state, such as the XState can method?

I found that I do something like:

const machine = createMachine(
  { idle: state(transition("start", "started", isReady)},
  // ...
);

function Widget() {
  const [current, send] = useMachine(machine);
  const disabled = current.value.transitions.get("start")[0].guards(current.context)
  return <button onClick={() => send('start")} disabled={disabled} >Start</button>
}

but a little awkward. Maybe something like

current.value.transitions.get("start").may() // => boolean

would be helpful (assumption: guards are pure functions). Maybe there is already a better way?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions