xstate: Assign action in entry get a wrong state
Description Action in entry will get different state for assign action and non-assign action;
const machine = createMachine<ToggleContext, ToggleEvent>(
{
id: "machine",
initial: "inactive",
context: {
count: 0
},
states: {
inactive: {
on: { TOGGLE: "active" },
entry: ["assignEntry", "nonAssignEntry"]
},
active: {
on: { TOGGLE: "inactive" },
entry: ["assignEntry", "nonAssignEntry"]
}
}
},
{
actions: {
assignEntry: assign({
count: (context, event, { state }) => {
console.log("assign", state);
return ++context.count;
}
}),
nonAssignEntry: (context, event, { state }) => {
console.log("non assign", state);
}
}
}
);
Expected Result They should have same state value, I think the state should be the target state, not previous state;
Actual Result
They have different state value:

Reproduction
Additional context xstate version: 4.22.0
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 17 (10 by maintainers)
Commits related to this issue
- Added a failing test for #2415 — committed to mattpocock/xstate by mattpocock 3 years ago
I think that this one was largely addressed in v5. For starters, we definitely have access to the same data in different action types since we no longer prioritize any of them and all of them are just executed “in order”. The
lightNode+entryactions solve @Sczlog’s issue (see the answer to Q2 here). The same mitigates @pke’s problem.You can now add
.preserveActionOrder: truein the root machine config to resolve this.