xstate: Can't transition back in history
Bug or feature request?
Bug, I believe.
Description:
When transitioning to a history state, the machine remains in the same state. It seems like it might be resolving the state by going forward to the history state and then back one in history to the current state.
// machine
{
initial: "step1",
states: {
step1: { on: { A: "step2" } },
step2: { on: { B: "step3" } },
step3: {},
history: { history: true }
},
on: { BACK: "history" }
}
send("A"); // 'step2'
send("B"); // 'step3'
send("BACK"); // --> 'step3'
(Bug) Expected result:
The machine’s state reverts back to the previous state in history.
(Bug) Actual result:
The machine’s state remains in the same state.
Link to reproduction or proof-of-concept:
BACK on the machine: https://codesandbox.io/s/j31v21xvyv
BACK in a step: https://codesandbox.io/s/1zqzv81l97
In xstate@next, the value ends up as {"history":{}}:
BACK on the machine in next: https://codesandbox.io/s/pwr4k2rmv7
BACK in a step in next: https://codesandbox.io/s/88mx737n5l
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 21 (11 by maintainers)
What if I’m not interested in doing the following: some step can skip a step, so when I’m in the target step and I want to go back, I’d like to go back to the original previous step, considering if it was skipped or not:
I just ran into this scenario and solved it by using guarded transitions.
Then define the
previousStepin themetaobject when sending theBACKtransition.Hopefully this helps someone else!
https://codesandbox.io/s/xstate-state-machine-with-history-stack-6iq32?file=/src/index.js ^Demo of how to implement history with a back button. My example use case revolves around navigation through “screens” of a user flow through a products screens, like in https://overflow.io/examples/#wireframe-user-flows.
Thanks for this awesome library @davidkpiano, it’s a pleasure working with something this well considered. Hope this demo makes sense!
@gpietro That is possible if those created/ready/in_progress states are in a nested state:
I assumed history states where used to fix the use case @diegopamio described. Why would you want to transition into a history state to revert back to the place you came from? I can’t get it to work that way though.
I’ll reopen it because I still want to verify that the behavior is correct for these cases:
I have similar behavior, but I am not sure if it is possible to achieve it.
I have a State Machine which can go from multiple states to another state (for example the settings page, which one can make some changes and then go back). So when going back from the settings page, I want to go back to the last accessed state.
Is it possible to do?
@carloslfu yea that’s what I’m trying to avoid, because you don’t always know where to go back to. That was a simple example but say step1 or step2 both took me to step3 – I just want to go back to where it was previously. Think of a dynamic wizard flow. Maybe this isn’t expected to be possible with the library.