@@ -6,14 +6,24 @@ Use this cheatsheet to quickly look up the syntax for XState v5 beta.
6
6
7
7
## Installing XState
8
8
9
- ``` bash title="using yarn"
9
+ <Tabs >
10
+ <TabItem value = " yarn" label = " yarn" >
11
+
12
+ ``` bash
10
13
yarn add xstate
11
14
```
12
15
13
- ``` bash title="using npm"
16
+ </TabItem >
17
+
18
+ <TabItem value = " npm" label = " npm" >
19
+
20
+ ``` bash
14
21
npm install xstate
15
22
```
16
23
24
+ </TabItem >
25
+ </Tabs >
26
+
17
27
[ Read more on installing XState] ( installation.mdx ) .
18
28
19
29
## Creating a state machine
@@ -31,12 +41,12 @@ const machine = createMachine({
31
41
count : ({ context }) => context .count + 1 ,
32
42
}),
33
43
on: {
34
- toggle: ' active' ,
44
+ toggle: { target: ' active' } ,
35
45
},
36
46
},
37
47
inactive: {
38
48
on: {
39
- toggle: ' inactive' ,
49
+ toggle: { target: ' inactive' } ,
40
50
},
41
51
},
42
52
},
@@ -79,7 +89,7 @@ actor.subscribe((snapshot) => {
79
89
80
90
actor .start ();
81
91
// logs: {
82
- // message: "https://images.dog.ceo/breeds/kuvasz/n02104029_110.jpg",
92
+ // message: "https://images.dog.ceo/breeds/kuvasz/n02104029_110.jpg",
83
93
// status: "success"
84
94
// }
85
95
```
@@ -93,17 +103,20 @@ A transition function is just like a reducer.
93
103
``` ts
94
104
import { fromTransition , createActor } from ' xstate' ;
95
105
96
- const transitionLogic = fromTransition ((state , event ) => {
97
- switch (event .type ) {
98
- case ' inc' :
99
- return {
100
- ... state ,
101
- count: state .count + 1 ,
102
- };
103
- default :
104
- return state ;
105
- }
106
- }, 0 ); // <- initial state
106
+ const transitionLogic = fromTransition (
107
+ (state , event ) => {
108
+ switch (event .type ) {
109
+ case ' inc' :
110
+ return {
111
+ ... state ,
112
+ count: state .count + 1 ,
113
+ };
114
+ default :
115
+ return state ;
116
+ }
117
+ },
118
+ { count: 0 }, // initial state
119
+ );
107
120
108
121
const actor = createActor (transitionLogic );
109
122
@@ -218,23 +231,24 @@ const machine = createMachine({
218
231
states: {
219
232
active: {
220
233
// highlight-next-line
221
- entry: ' activate' ,
234
+ entry: { type: ' activate' } ,
222
235
// highlight-next-line
223
- exit: ' deactivate' ,
236
+ exit: { type: ' deactivate' } ,
224
237
on: {
225
238
toggle: {
226
239
target: ' inactive' ,
227
240
// highlight-next-line
228
- actions: [' notify' ],
241
+ actions: [{ type: ' notify' } ],
229
242
},
230
243
},
231
244
},
232
245
inactive: {
233
246
on: {
234
247
toggle: {
235
- type : ' active' ,
248
+ target : ' active' ,
236
249
// highlight-start
237
250
actions: [
251
+ // action with params
238
252
{
239
253
type: ' notify' ,
240
254
params: {
@@ -306,6 +320,13 @@ const machine = createMachine({
306
320
},
307
321
},
308
322
active: {
323
+ on: {
324
+ toggle: {
325
+ // Guard with params
326
+ guard: { type: ' isAfterTime' , params: { time: ' 16:00' } },
327
+ target: ' inactive' ,
328
+ },
329
+ },
309
330
// ...
310
331
},
311
332
},
@@ -316,6 +337,12 @@ const actor = createActor(
316
337
// highlight-start
317
338
guards: {
318
339
canBeToggled : ({ context }) => context .canActivate ,
340
+ isAfterTime : ({ guard }) => {
341
+ const { time } = guard .params ;
342
+ const [hour, minute] = time .split (' :' );
343
+ const now = new Date ();
344
+ return now .getHours () > hour && now .getMinutes () > minute ;
345
+ },
319
346
},
320
347
// highlight-end
321
348
actions: {
@@ -500,3 +527,38 @@ actor.start();
500
527
```
501
528
502
529
[ Read more about invoking actors with input] ( input.mdx#invoking-actors-with-input ) .
530
+
531
+ ## Types
532
+
533
+ ``` ts
534
+ const promiseLogic = fromPromise (async () => {
535
+ /* ... */
536
+ });
537
+
538
+ const machine = createMachine ({
539
+ types: {} as {
540
+ context: {
541
+ count: number ;
542
+ };
543
+ events:
544
+ | {
545
+ type: ' inc' ;
546
+ }
547
+ | { type: ' dec' }
548
+ | { type: ' incBy' ; amount: number };
549
+ actions:
550
+ | { type: ' notify' ; params: { message: string } }
551
+ | { type: ' handleChange' };
552
+ guards:
553
+ | { type: ' canBeToggled' }
554
+ | { type: ' isAfterTime' ; params: { time: string } };
555
+ actors: {
556
+ logic: typeof promiseLogic ;
557
+ src: ' someSrc' ;
558
+ id: ' promise1' | ' promise2' ;
559
+ };
560
+ delays: ' shortTimeout' | ' longTimeout' ;
561
+ tags: ' tag1' | ' tag2' ;
562
+ },
563
+ });
564
+ ```
0 commit comments