#sending payload to transition

1 messages · Page 1 of 1 (latest)

obsidian citrus
#

how to send payload to transition?

mighty lion
#

if it's a separate event from the originating event you would want to use raise.

drifting bronze
#

Just add extra props to the send object

send({
  type: "transition-name",
  otherProp: "foo",
  otherProp2: { nested: "bar" },
});
jolly violet
#

And then? What receives these values?

jolly violet
#

I suppose, but in your example it feels more like the event "contains" the payload props. Which in turn is "received" by the transition. I wasn't aware of the existence of "Transition Actors".

#

I guess the Transition Actor documentation always confuses me a little because it uses the word "state" quite differently to the "states" in a finite state machine. For Transition Actors, they appear to have their own "state" which is sort of like the "context" of a FSM (it contains immutable data), and "returning the next state" means returning the data structure (possibly modified) rather than returning "the next state which the FSM will transition to".

#

Unless I'm reading this incorrectly.

#

The above (mis)understanding is why I'm confused often about where "data" is meant to belong in an Xstate system, especially when there are multiple actors involved. I get that "context" is global to the state machine (although, unfortunately, it's not directly accessible by any invoked/spawned child actors). And I see how these special Transition Actors seem to have their own "state" which is actually like another context/redux-store place to store data, local only to that actor. What I'm confused about is that the payloads/props/data that can be added to "events" appear to only be accessible by these special Transition Actors. They are not passed on to the "next state" of the FSM; nor are these props applied to the "context" data (although I suppose one could programmatically add this in a convoluted way). So what is the point of adding props to events when this data is only usable by Transition Actors?

true marsh
jolly violet
#

But that's my point: this is not what it does. It emits a "state" in the sense of "some data", not "state" in the sense of "the next state in the state machine".

true marsh
jolly violet
#

I have found the the "event Object" (including any props) is also available in certain other cases, not just Transition Actors. For example, it is available when passing input to a spawned/invoked actor, e.g.

#
input: ({ context, event }) => {
// Can use properties on 'event' here
#

It can also be accessed when sending events to an actor using sendTo, e.g:

#
"tracking.update": {
          actions: sendTo("engagementZoneActor", ({ event }) => event),
          // if there are props defined on "tracking.update" event, these will be accessible here
        },
#

So that's been useful.

true marsh
jolly violet
#

Where in the docs does it say this?

true marsh
jolly violet
#

Oh you mean for Transition Actors, yes. But I mean giving input to a spawned or invoked actor.

true marsh
#

I thought that's what we were talking about?

#

What are you talking about? The event object?

jolly violet
#

Initially, yes. But I then brought up examples I had found of other ways to pass event props (the Event object) elsewhere, that didn't involve Transition Actions.

#

Yes, the event object.

true marsh
#

The event prop is available on regular send events also. I'm sure you knew that already.

#

Or have you never used it before?

#

I'm confused. That seems like such a normal thing in XState. The event object.

jolly violet
#

The event object is indeed a normal object. It's easy to see how to set props on that object. What's less obvious is how to read and use those props anywhere.

#

There are plenty of examples of emitting, sending events, etc. with props on the event object. There are very few examples of actually accessing those props from the object, or even getting access to the object at all.

true marsh
#

You access them from the event you sent them to. Like an event handler handles events.

jolly violet
#

Apologies, that doesn't make much sense to me.

#

"Access them from the event you sent them to". Where?

true marsh
#

In an action.

jolly violet
#

Indeed, yes.

#

But I must say, it took a long time for me to see the connection between Events and Actions.

#

In the docs for "Events and Transitions" , there is not even any mention of how one would access the Event object or do anything with any props set on it. So far, it seems to me that the only places were you can access this data is in Action functions and invoke/spawn functions.

true marsh
#

I can't remeber how I learned about that.