Skip to content

Commit b75ce3f

Browse files
authored
Update output data property in final state of (#249)
currencyMachine
1 parent 38a0559 commit b75ce3f

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

docs/final-states.mdx

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ const coffeeMachine = createMachine({
139139

140140
## Output
141141

142-
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 top-level final state:
142+
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:
143143

144144
```ts
145145
import { createMachine, createActor } from 'xstate';
@@ -152,14 +152,14 @@ const currencyMachine = createMachine({
152152
},
153153
converted: {
154154
type: 'final',
155-
// highlight-start
156-
output: ({ context }) => ({
157-
amount: context.amount,
158-
currency: context.currency,
159-
}),
160-
// highlight-end
161155
},
162156
},
157+
// highlight-start
158+
output: ({ context }) => ({
159+
amount: context.amount,
160+
currency: context.currency,
161+
}),
162+
// highlight-end
163163
});
164164

165165
const currencyActor = createActor(currencyMachine, {
@@ -170,13 +170,26 @@ const currencyActor = createActor(currencyMachine, {
170170
},
171171
});
172172

173-
currencyActor.onDone(() => {
174-
console.log(currencyActor.getSnapshot().output);
175-
// logs e.g. { amount: 12, currency: 'EUR' }
173+
currencyActor.subscribe({
174+
complete() {
175+
console.log(currencyActor.getSnapshot().output);
176+
// logs e.g. { amount: 12, currency: 'EUR' }
177+
},
176178
});
177179
```
178180

179-
- `.output` can also be a static value
181+
The `.output` property can also be a static value:
182+
183+
```ts
184+
import { createMachine, createActor } from 'xstate';
185+
186+
const processMachine = createMachine({
187+
// ...
188+
output: {
189+
message: 'Process completed.',
190+
},
191+
});
192+
```
180193

181194
## TypeScript
182195

0 commit comments

Comments
 (0)