Skip to content

Commit 01f3203

Browse files
committed
Add info about re-entering child state nodes to the migration guide
1 parent 40d3de5 commit 01f3203

File tree

1 file changed

+121
-62
lines changed

1 file changed

+121
-62
lines changed

versioned_docs/version-5/migration.mdx

Lines changed: 121 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -657,33 +657,31 @@ Breaking change
657657

658658
:::
659659

660-
All transitions are implicitly internal. This change is relevant for transitions defined on compound state nodes with `entry` or `exit` actions, invoked actors or delayed transitions (`after`). If you relied on implicit re-entering of a compound state node, use `reenter: true`:
660+
All transitions are implicitly internal. This change is relevant for transitions defined on compound state nodes with `entry` or `exit` actions, invoked actors, or delayed transitions (`after`). If you relied on implicit re-entering of a compound state node, use `reenter: true`:
661661

662662
<Tabs>
663663
<TabItem value="v5" label="XState v5 beta">
664664

665665
```ts
666666
//
667-
const machine = createMachine(
668-
{
669-
// ...
670-
states: {
671-
compoundState: {
672-
entry: 'someAction',
673-
on: {
674-
someEvent: {
675-
target: 'compoundState.childState',
676-
reenter: true, // make it external explicitly!
677-
},
678-
},
679-
initial: 'childState',
680-
states: {
681-
childState: {},
667+
const machine = createMachine({
668+
// ...
669+
states: {
670+
compoundState: {
671+
entry: 'someAction',
672+
on: {
673+
someEvent: {
674+
target: 'compoundState.childState',
675+
reenter: true, // make it external explicitly!
682676
},
683677
},
678+
initial: 'childState',
679+
states: {
680+
childState: {},
681+
},
684682
},
685683
},
686-
)
684+
})
687685
```
688686

689687
</TabItem>
@@ -692,26 +690,24 @@ const machine = createMachine(
692690

693691
```ts
694692
// ❌ DEPRECATED
695-
const machine = createMachine(
696-
{
697-
// ...
698-
states: {
699-
compoundState: {
700-
entry: 'someAction',
701-
on: {
702-
someEvent: {
703-
// implicitly external
704-
target: 'compoundState.childState', // non-relative target
705-
},
706-
},
707-
initial: 'childState',
708-
states: {
709-
childState: {},
693+
const machine = createMachine({
694+
// ...
695+
states: {
696+
compoundState: {
697+
entry: 'someAction',
698+
on: {
699+
someEvent: {
700+
// implicitly external
701+
target: 'compoundState.childState', // non-relative target
710702
},
711703
},
704+
initial: 'childState',
705+
states: {
706+
childState: {},
707+
},
712708
},
713709
},
714-
)
710+
})
715711
```
716712

717713
</TabItem>
@@ -722,25 +718,88 @@ const machine = createMachine(
722718

723719
```ts
724720
//
725-
const machine = createMachine(
726-
{
727-
// ...
728-
states: {
729-
compoundState: {
730-
after: {
731-
1000: {
732-
target: 'compoundState.childState',
733-
reenter: true, // make it external explicitly!
734-
}
721+
const machine = createMachine({
722+
// ...
723+
states: {
724+
compoundState: {
725+
after: {
726+
1000: {
727+
target: 'compoundState.childState',
728+
reenter: true, // make it external explicitly!
729+
}
730+
},
731+
initial: 'childState',
732+
states: {
733+
childState: {},
734+
},
735+
},
736+
},
737+
})
738+
```
739+
740+
</TabItem>
741+
742+
<TabItem value="v4" label="XState v4">
743+
744+
```ts
745+
// ❌ DEPRECATED
746+
const machine = createMachine({
747+
// ...
748+
states: {
749+
compoundState: {
750+
after: {
751+
1000: {
752+
// implicitly external
753+
target: 'compoundState.childState', // non-relative target
754+
}
755+
},
756+
initial: 'childState',
757+
states: {
758+
childState: {},
759+
},
760+
},
761+
},
762+
})
763+
```
764+
765+
</TabItem>
766+
</Tabs>
767+
768+
### Child state nodes are always re-entered
769+
770+
:::breakingchange
771+
772+
Breaking change
773+
774+
:::
775+
776+
Child state nodes are always re-entered when they are targeted by transitions (both external and **internal**) defined on compound state nodes. This change is relevant only if a child state node has `entry` or `exit` actions, invoked actors, or delayed transitions (`after`). Add a `stateIn` guard to prevent undesirable re-entry of the child state:
777+
778+
<Tabs>
779+
<TabItem value="v5" label="XState v5 beta">
780+
781+
```ts
782+
//
783+
784+
const machine = createMachine({
785+
// ...
786+
states: {
787+
compoundState: {
788+
on: {
789+
someEvent: {
790+
guard: not(stateIn('compoundState.childState')),
791+
target: '.childState',
735792
},
736-
initial: 'childState',
737-
states: {
738-
childState: {},
793+
},
794+
initial: 'childState',
795+
states: {
796+
childState: {
797+
entry: 'someAction',
739798
},
740799
},
741800
},
742801
},
743-
)
802+
})
744803
```
745804

746805
</TabItem>
@@ -749,30 +808,30 @@ const machine = createMachine(
749808

750809
```ts
751810
// ❌ DEPRECATED
752-
const machine = createMachine(
753-
{
754-
// ...
755-
states: {
756-
compoundState: {
757-
after: {
758-
1000: {
759-
// implicitly external
760-
target: 'compoundState.childState', // non-relative target
761-
}
811+
812+
const machine = createMachine({
813+
// ...
814+
states: {
815+
compoundState: {
816+
on: {
817+
someEvent: {
818+
target: '.childState', // implicitly internal, childState not re-entered
762819
},
763-
initial: 'childState',
764-
states: {
765-
childState: {},
820+
},
821+
initial: 'childState',
822+
states: {
823+
childState: {
824+
entry: 'someAction',
766825
},
767826
},
768827
},
769828
},
770-
)
829+
})
771830
```
772831

773832
</TabItem>
774-
</Tabs>
775833

834+
</Tabs>
776835

777836
### Use `stateIn()` to validate state transitions, not `in`
778837

0 commit comments

Comments
 (0)