Skip to content

How to determine if a state has been entered using an interpreter? #3567

Answered by davidkpiano
lo1tuma asked this question in General
Discussion options

You must be logged in to vote

The way you would do it, following the algorithm that XState itself uses, is that you would compare the previous state.configuration to the current state.configuration:

let prevStateNodes;

service.subscribe(state => {
  if (state.configuration.find(stateNode => stateNode.id === 'some.state.id' && !prevStateNodes?.includes(stateNode))) {
    // state was entered
  }

  prevStateNodes = state.configuration;
});

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@lo1tuma
Comment options

@Andarist
Comment options

Answer selected by lo1tuma
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants