Skip to content

Commit 246d344

Browse files
Update info on how to use the "output" property (#236)
Co-authored-by: Laura Kalbag <[email protected]>
1 parent 0527627 commit 246d344

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

docs/final-states.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ const coffeeMachine = createMachine({
105105

106106
## Output
107107

108-
When a machine reaches its top-level final state, it can produce output data. You can specify this output data in the `.output` property of the machine:
108+
When a machine reaches its top-level final state, it can produce output data. You can specify this output data in the `.output` property of the machine config:
109109

110110
```ts
111111
import { createMachine, createActor } from 'xstate';

docs/migration.mdx

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,7 @@ Breaking change
12231223
12241224
:::
12251225
1226-
To produce output data from a machine which reached its final state, use the `output` property instead of `data`:
1226+
To produce output data from a machine which reached its final state, use the top-level `output` property instead of `data`:
12271227
12281228
<Tabs>
12291229
<TabItem value="v5" label="XState v5 beta">
@@ -1265,6 +1265,50 @@ const machine = createMachine({
12651265
</TabItem>
12661266
</Tabs>
12671267
1268+
1269+
To provide a dynamically generated output, replace `invoke.data` with `invoke.output` and add a top-level `output` property:
1270+
1271+
<Tabs>
1272+
<TabItem value="v5" label="XState v5 beta">
1273+
1274+
```ts
1275+
//
1276+
const machine = createMachine({
1277+
// ...
1278+
states: {
1279+
finished: {
1280+
type: 'final',
1281+
output: ({ event }) => ({
1282+
answer: event.someValue,
1283+
}),
1284+
},
1285+
},
1286+
output: ({ event }) => event.output,
1287+
});
1288+
```
1289+
1290+
</TabItem>
1291+
1292+
<TabItem value="v4" label="XState v4">
1293+
1294+
```ts
1295+
// ❌ DEPRECATED
1296+
const machine = createMachine({
1297+
// ...
1298+
states: {
1299+
finished: {
1300+
type: 'final',
1301+
data: (context, event) => {
1302+
answer: event.someValue,
1303+
},
1304+
},
1305+
},
1306+
});
1307+
```
1308+
1309+
</TabItem>
1310+
</Tabs>
1311+
12681312
### Don't use property mappers in `input` or `output`
12691313
12701314
:::breakingchange

0 commit comments

Comments
 (0)