Skip to content

Commit 475f504

Browse files
committed
Add types to cheatsheet
1 parent 565ffac commit 475f504

File tree

1 file changed

+82
-20
lines changed

1 file changed

+82
-20
lines changed

versioned_docs/version-5/cheatsheet.mdx

Lines changed: 82 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,24 @@ Use this cheatsheet to quickly look up the syntax for XState v5 beta.
66

77
## Installing XState
88

9-
```bash title="using yarn"
9+
<Tabs>
10+
<TabItem value="yarn" label="yarn">
11+
12+
```bash
1013
yarn add xstate
1114
```
1215

13-
```bash title="using npm"
16+
</TabItem>
17+
18+
<TabItem value="npm" label="npm">
19+
20+
```bash
1421
npm install xstate
1522
```
1623

24+
</TabItem>
25+
</Tabs>
26+
1727
[Read more on installing XState](installation.mdx).
1828

1929
## Creating a state machine
@@ -31,12 +41,12 @@ const machine = createMachine({
3141
count: ({ context }) => context.count + 1,
3242
}),
3343
on: {
34-
toggle: 'active',
44+
toggle: { target: 'active' },
3545
},
3646
},
3747
inactive: {
3848
on: {
39-
toggle: 'inactive',
49+
toggle: { target: 'inactive' },
4050
},
4151
},
4252
},
@@ -79,7 +89,7 @@ actor.subscribe((snapshot) => {
7989

8090
actor.start();
8191
// logs: {
82-
// message: "https://images.dog.ceo/breeds/kuvasz/n02104029_110.jpg",
92+
// message: "https://images.dog.ceo/breeds/kuvasz/n02104029_110.jpg",
8393
// status: "success"
8494
// }
8595
```
@@ -93,17 +103,20 @@ A transition function is just like a reducer.
93103
```ts
94104
import { fromTransition, createActor } from 'xstate';
95105

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+
);
107120

108121
const actor = createActor(transitionLogic);
109122

@@ -218,23 +231,24 @@ const machine = createMachine({
218231
states: {
219232
active: {
220233
// highlight-next-line
221-
entry: 'activate',
234+
entry: { type: 'activate' },
222235
// highlight-next-line
223-
exit: 'deactivate',
236+
exit: { type: 'deactivate' },
224237
on: {
225238
toggle: {
226239
target: 'inactive',
227240
// highlight-next-line
228-
actions: ['notify'],
241+
actions: [{ type: 'notify' }],
229242
},
230243
},
231244
},
232245
inactive: {
233246
on: {
234247
toggle: {
235-
type: 'active',
248+
target: 'active',
236249
// highlight-start
237250
actions: [
251+
// action with params
238252
{
239253
type: 'notify',
240254
params: {
@@ -306,6 +320,13 @@ const machine = createMachine({
306320
},
307321
},
308322
active: {
323+
on: {
324+
toggle: {
325+
// Guard with params
326+
guard: { type: 'isAfterTime', params: { time: '16:00' } },
327+
target: 'inactive',
328+
},
329+
},
309330
// ...
310331
},
311332
},
@@ -316,6 +337,12 @@ const actor = createActor(
316337
// highlight-start
317338
guards: {
318339
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+
},
319346
},
320347
// highlight-end
321348
actions: {
@@ -500,3 +527,38 @@ actor.start();
500527
```
501528

502529
[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

Comments
 (0)