Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem Passing params in XState v5 Transitions #5180

Open
dforer opened this issue Jan 28, 2025 · 0 comments
Open

Problem Passing params in XState v5 Transitions #5180

dforer opened this issue Jan 28, 2025 · 0 comments

Comments

@dforer
Copy link

dforer commented Jan 28, 2025

Discussed in #5179

Originally posted by dforer January 28, 2025
Hi, XState community!

I’ve been working on a project using XState v5 to handle state transitions in a simple state machine. I’m encountering a persistent issue where the params object sent with an event doesn’t seem to be passed to the actions during transitions.

Here’s what’s happening:

  1. I send an event to the machine via actor.send() with a params property.
    Example:
    actor.send({ type: 'START', params: { additionalInfo: 'Logging START event' } });
  2. The transition to the next state occurs correctly, and the action tied to the transition executes.
  3. However, inside the action, the second argument (params) is consistently undefined.

Here’s a minimal example of the state machine:

const testMachine = createMachine({
  id: 'simple',
  initial: 'idle',
  states: {
    idle: {
      on: {
        START: {
          target: 'running',
          actions: (context, params) => {
            console.log('Transitioning from idle to running with params:', params || 'No params received');
          },
        },
      },
    },
    running: {
      on: {
        STOP: {
          target: 'idle',
          actions: (context, params) => {
            console.log('Transitioning from running to idle with params:', params || 'No params received');
          },
        },
      },
    },
  },
});

Here’s the output I’m seeing when I run this example:
Starting the state machine...
Actor received state: idle
Transitioned to state: idle
About to send START event...
Sending START event with: { type: 'START', params: { additionalInfo: 'Logging START event' } }
Transitioning from idle to running with params: No params received
Actor received state: running
Transitioned to state: running
Sent START event.
About to send STOP event...
Sending STOP event with: { type: 'STOP', params: { additionalInfo: 'Logging STOP event' } }
Transitioning from running to idle with params: No params received
Actor received state: idle
Transitioned to state: idle
Sent STOP event.

Expected Behavior

The params object sent with the event should be accessible in the second argument of the transition actions. For example:
Transitioning from idle to running with params: { "additionalInfo": "Logging START event" }

Questions
1. Is there something specific I need to do in XState v5 to ensure the params are passed to the actions during transitions?
2. Are there breaking changes in v5 related to how events and params are handled compared to v4?

Any guidance or suggestions would be greatly appreciated!

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant