Skip to content

Commit 517e1c7

Browse files
authored
Doc updates (#285)
* Improve installation * Updates * Update context
1 parent f7416cf commit 517e1c7

File tree

11 files changed

+429
-177
lines changed

11 files changed

+429
-177
lines changed

docs/cheatsheet.mdx

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,26 @@ Use this cheatsheet to quickly look up the syntax for XState v5 beta.
77
## Installing XState
88

99
<Tabs>
10-
<TabItem value="yarn" label="yarn">
10+
<TabItem value="npm" label="npm">
1111

1212
```bash
13-
yarn add xstate
13+
npm install xstate@beta
1414
```
1515

1616
</TabItem>
1717

18-
<TabItem value="npm" label="npm">
18+
<TabItem value="pnpm" label="pnpm">
19+
20+
```bash
21+
pnpm install xstate@beta
22+
```
23+
24+
</TabItem>
25+
26+
<TabItem value="yarn" label="yarn">
1927

2028
```bash
21-
npm install xstate
29+
yarn add xstate@beta
2230
```
2331

2432
</TabItem>
@@ -200,9 +208,16 @@ const machine = createMachine({
200208
active: {
201209
initial: 'one',
202210
states: {
203-
one: {},
211+
one: {
212+
on: {
213+
NEXT: { target: 'two' }
214+
}
215+
},
204216
two: {},
205217
},
218+
on: {
219+
NEXT: { target: 'inactive' }
220+
}
206221
},
207222
inactive: {},
208223
},
@@ -216,6 +231,12 @@ actor.subscribe((snapshot) => {
216231

217232
actor.start();
218233
// logs { active: 'one' }
234+
235+
actor.send({ type: 'NEXT' });
236+
// logs { active: 'two' }
237+
238+
actor.send({ type: 'NEXT' });
239+
// logs 'inactive'
219240
```
220241

221242
[Read more about parent states](parent-states.mdx).
@@ -323,6 +344,7 @@ const machine = createMachine({
323344
on: {
324345
toggle: {
325346
// Guard with params
347+
// highlight-next-line
326348
guard: { type: 'isAfterTime', params: { time: '16:00' } },
327349
target: 'inactive',
328350
},
@@ -362,15 +384,18 @@ actor.start();
362384
## Invoking actors
363385

364386
```ts
365-
import { createMachine, fromPromise, createActor, assign } from 'xstate';
387+
import { setup, fromPromise, createActor, assign } from 'xstate';
366388

367389
const loadUserLogic = fromPromise(async () => {
368390
const response = await fetch('https://jsonplaceholder.typicode.com/users/1');
369391
const user = await response.json();
370392
return user;
371393
});
372394

373-
const machine = createMachine({
395+
const machine = setup({
396+
// highlight-next-line
397+
actors: { loadUserLogic }
398+
}).createMachine({
374399
id: 'toggle',
375400
initial: 'loading',
376401
context: {
@@ -381,7 +406,7 @@ const machine = createMachine({
381406
// highlight-start
382407
invoke: {
383408
id: 'loadUser',
384-
src: loadUserLogic,
409+
src: 'loadUserLogic',
385410
onDone: {
386411
target: 'doSomethingWithUser',
387412
actions: assign({
@@ -531,36 +556,40 @@ actor.start();
531556
## Types
532557

533558
```ts
559+
import { setup, fromPromise } from 'xstate';
560+
534561
const promiseLogic = fromPromise(async () => {
535562
/* ... */
536563
});
537564

538-
const machine = createMachine({
539-
types: {} as {
540-
context: {
565+
const machine = setup({
566+
types: {
567+
context: {} as {
541568
count: number;
542569
};
543-
events:
544-
| {
545-
type: 'inc';
546-
}
570+
events: {} as
571+
| { type: 'inc'; }
547572
| { type: 'dec' }
548573
| { type: 'incBy'; amount: number };
549-
actions:
574+
actions: {} as
550575
| { type: 'notify'; params: { message: string } }
551576
| { type: 'handleChange' };
552-
guards:
577+
guards: {} as
553578
| { type: 'canBeToggled' }
554579
| { type: 'isAfterTime'; params: { time: string } };
555-
actors: {
556-
logic: typeof promiseLogic;
557-
src: 'someSrc';
558-
id: 'promise1' | 'promise2';
580+
children: {} as {
581+
promise1: 'someSrc';
582+
promise2: 'someSrc';
559583
};
560584
delays: 'shortTimeout' | 'longTimeout';
561585
tags: 'tag1' | 'tag2';
562586
input: number;
563587
output: string;
564588
},
589+
actors: {
590+
promiseLogic
591+
}
592+
}).createMachine({
593+
// ...
565594
});
566595
```

docs/context.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import SkipDownLink from '@site/src/components/SkipDownLink';
66

77
In XState, `context` is how you store data in a state machine [actor](actors.mdx).
88

9-
`context` is a special property available in all states and used to store data relevant to the state machine. The `context` object is immutable, so you cannot directly modify it. Instead, use the `assign(...)` action to update `context`.
9+
The `context` property is available in all states and used to store data relevant to an actor. The `context` object is immutable, so you cannot directly modify it. Instead, for state machine logic, you can use the `assign(...)` action to update `context`.
1010

11-
The `context` property is _optional_; if the state machine only specifies [finite states](finite-states.mdx), it may not need `context`.
11+
The `context` property is _optional_; if the state machine only specifies [finite states](finite-states.mdx) and no external contextual data, it may not need `context`.
1212

1313
```ts
1414
import { createMachine } from 'xstate';
@@ -179,7 +179,7 @@ const machine = createMachine({
179179

180180
Use our XState context cheatsheet below to get started quickly.
181181

182-
**Initial context**
182+
### Initial context
183183

184184
```ts
185185
const machine = createMachine({
@@ -189,7 +189,7 @@ const machine = createMachine({
189189
});
190190
```
191191

192-
**Lazy initial context**
192+
### Lazy initial context
193193

194194
```ts
195195
const machine = createMachine({
@@ -200,7 +200,7 @@ const machine = createMachine({
200200
});
201201
```
202202

203-
**Updating context with `assign(...)`**
203+
### Updating context with `assign(...)`
204204

205205
```ts
206206
const machine = createMachine({
@@ -217,7 +217,7 @@ const machine = createMachine({
217217
});
218218
```
219219

220-
**Input**
220+
### Input
221221

222222
```ts
223223
const machine = createMachine({

docs/installation.mdx

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,52 @@ title: Installation
44

55
# Installation
66

7-
You can install XState using your favorite package manager or embed the `<script>` directly from a CDN.
7+
XState has **zero dependencies** and runs anywhere that JavaScript runs. You can install XState using your favorite package manager, like [npm](https://www.npmjs.com/), [pnpm](https://pnpm.js.org/), or [yarn](https://yarnpkg.com/).
88

99
<Tabs>
10-
<TabItem value="yarn" label="yarn">
10+
<TabItem value="npm" label="npm">
1111

1212
```bash
13-
yarn add xstate@beta
13+
npm install xstate@beta
1414
```
1515

1616
</TabItem>
1717

18-
<TabItem value="npm" label="npm">
18+
<TabItem value="pnpm" label="pnpm">
1919

2020
```bash
21-
npm install xstate@beta
21+
pnpm install xstate@beta
22+
```
23+
24+
</TabItem>
25+
26+
<TabItem value="yarn" label="yarn">
27+
28+
```bash
29+
yarn add xstate@beta
2230
```
2331

2432
</TabItem>
2533
</Tabs>
2634

2735
## CDN
2836

29-
You can import XState from the [Skypack CDN](https://www.skypack.dev/xstate@beta):
37+
You can also import XState from various CDNs:
38+
39+
- [esm.run](https://esm.run):
40+
41+
```ts
42+
import { createMachine, createActor } from 'https://esm.run/xstate';
43+
```
44+
45+
- [esm.sh](https://esm.sh):
46+
47+
```ts
48+
import { createMachine, createActor } from 'https://esm.sh/xstate';
49+
```
50+
51+
- [Skypack](https://www.skypack.dev):
3052

3153
```ts
32-
import {
33-
createMachine,
34-
createActor,
35-
} from 'https://cdn.skypack.dev/xstate@beta';
36-
37-
const machine = createMachine({
38-
initial: 'a',
39-
states: {
40-
a: {},
41-
},
42-
});
43-
44-
const actor = createActor(machine);
45-
actor.subscribe((state) => {
46-
console.log(state);
47-
});
48-
actor.start();
54+
import { createMachine, createActor } from 'https://cdn.skypack.dev/xstate';
4955
```

0 commit comments

Comments
 (0)