Skip to content

Commit 8d951a7

Browse files
committed
Add Simple States And Composite States as an XState til
1 parent 09c4bb1 commit 8d951a7

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
1212

13-
_1140 TILs and counting..._
13+
_1141 TILs and counting..._
1414

1515
---
1616

@@ -1336,6 +1336,7 @@ _1140 TILs and counting..._
13361336

13371337
- [Define Event That Does Internal Self Transition](xstate/define-event-that-does-internal-self-transition.md)
13381338
- [Inline Actions vs Actions In Machine Options](xstate/inline-actions-vs-actions-in-machine-options.md)
1339+
- [Simple States And Composite States](xstate/simple-states-and-composite-states.md)
13391340
- [Use An XState Machine With React](xstate/use-an-xstate-machine-with-react.md)
13401341

13411342
### YAML
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Simple States And Composite States
2+
3+
XState allows you to build [hierarchical state
4+
machines](https://xstate.js.org/docs/guides/hierarchical.html). A hierarchical
5+
state machine is one where there are substates nested under states.
6+
7+
States with nothing nested under them are _simple states_. States that have
8+
substates are _composite states_.
9+
10+
```javascript
11+
const machine = createMachine({
12+
// Starting State
13+
initial: "active",
14+
// Starting Context
15+
context: { count: 0 },
16+
// States
17+
states: {
18+
inactive: {
19+
always: { target: "active" },
20+
},
21+
active: {
22+
on: {
23+
SWITCH_COUNT_DIRECTION: {
24+
actions: ["logSwitch", "consoleLogSwitch"],
25+
},
26+
COUNT: {
27+
actions: "changeCount",
28+
},
29+
},
30+
initial: "increment",
31+
states: {
32+
increment: {
33+
on: {
34+
SWITCH_COUNT_DIRECTION: "decrement",
35+
},
36+
},
37+
decrement: {
38+
on: {
39+
SWITCH_COUNT_DIRECTION: "increment",
40+
},
41+
},
42+
},
43+
},
44+
},
45+
});
46+
```
47+
48+
- The `inactive` state is a _simple state_. There is nothing nested under it.
49+
- The `active` state is a _composite state_. There are two substates
50+
(`increment` and `decrement`) nested under it.
51+
- The `increment` and `decrement` states are both _simple states_. Nothing is
52+
nested under them.

0 commit comments

Comments
 (0)