Allow enqueued actions to access params
#4926
-
I initially wanted to submit a bug report but figured it was probably working as intended, but it definitely caught me off guard: import {createActor, setup, log, enqueueActions} from 'xstate';
const s = setup({
actions: {
foo: log((_, params) => params), // LOG: {x: 1}
bar: enqueueActions(({enqueue}, p) => {
enqueue(log((_, params) => params)); // LOG: undefined (expected {x: 2})
enqueue((_, params) => console.log(params)); // LOG: undefined (expected {x: 2})
enqueue({type: 'foo', params: p}); // LOG: {x: 2}
})
}
});
const m = s.createMachine({
on: {
log: {
actions: [
{type: 'foo', params: {x: 1}},
{type: 'bar', params: {x: 2}}
]
}
}
});
const a = createActor(m);
a.start();
a.send({type: 'log'}); Considering that What do you think? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I think this makes sense for what you want to do, but on the other hand, explicitly passing the parameters would be more clear 🤔 |
Beta Was this translation helpful? Give feedback.
-
This post came about after hours of debugging until I figured out what was going on: enqueueActions(({enqueue, context, event}, params) => {
// ^^^^^^^^^^^^^^ ^^^^^^
// (same thing) | | (different)
// +-------------+ +-------------+
// | |
// vvvvvvvvvvvvvv vvvvvv
enqueue(({context, event}, params) => {
});
}); XState does implicitly (?) pass on things like After sleeping on it, I think it all makes sense now. This: {
entry: {
type: 'foo',
params: {
x: 1
}
}
} Is the same as: enqueue({
type: 'foo',
params: {
x: 1
}
}) But these So yeah, being explicit about passing Anyway thanks a lot for XState, I absolutely love it <3 |
Beta Was this translation helpful? Give feedback.
I think this makes sense for what you want to do, but on the other hand, explicitly passing the parameters would be more clear 🤔