Skip to content

Commit 5f211cc

Browse files
authored
Merge pull request #257 from statelyai/davidkpiano/update-params
Update action and guard params
2 parents a9dbc86 + ae704da commit 5f211cc

File tree

4 files changed

+54
-19
lines changed

4 files changed

+54
-19
lines changed

docs/actions.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ const feedbackMachine = createMachine({
2727
}
2828
}, {
2929
actions: {
30-
track: ({ action }) => {
31-
track(action.params);
30+
track: (_, params) => {
31+
track(params);
3232
// tracks { response: 'good' }
3333
},
3434
showConfetti: () => {
@@ -249,7 +249,7 @@ const feedbackMachine = createMachine(
249249
{
250250
// highlight-start
251251
actions: {
252-
track: ({ context, event }) => {
252+
track: ({ context, event }, params) => {
253253
// Action implementation
254254
// ...
255255
},
@@ -266,7 +266,7 @@ const feedbackActor = createActor(
266266
// highlight-start
267267
feedbackMachine.provide({
268268
actions: {
269-
track: ({ context, event }) => {
269+
track: ({ context, event }, params) => {
270270
// Overridden action implementation
271271
// ...
272272
},

docs/cheatsheet.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ const machine = createMachine({
266266
const actor = createActor(
267267
machine.provide({
268268
actions: {
269-
notify: ({ action }) => {
270-
console.log(action.params.message ?? 'Default message');
269+
notify: (_, params) => {
270+
console.log(params.message ?? 'Default message');
271271
},
272272
activate: () => {
273273
console.log('Activating');
@@ -337,8 +337,8 @@ const actor = createActor(
337337
// highlight-start
338338
guards: {
339339
canBeToggled: ({ context }) => context.canActivate,
340-
isAfterTime: ({ guard }) => {
341-
const { time } = guard.params;
340+
isAfterTime: (_, params) => {
341+
const { time } = params;
342342
const [hour, minute] = time.split(':');
343343
const now = new Date();
344344
return now.getHours() > hour && now.getMinutes() > minute;

docs/guards.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ const feedbackMachine = createMachine(
143143
{
144144
// highlight-start
145145
guards: {
146-
isValid: ({ context, guard }) => {
146+
isValid: ({ context }, params) => {
147147
return (
148148
context.feedback.length > 0 &&
149-
context.feedback.length <= guard.maxLength
149+
context.feedback.length <= params.maxLength
150150
);
151151
},
152152
},
@@ -162,10 +162,10 @@ const feedbackActor = createActor(
162162
// highlight-start
163163
feedbackMachine.provide({
164164
guards: {
165-
isValid: ({ context, guard }) => {
165+
isValid: ({ context }, params) => {
166166
return (
167167
context.feedback.length > 0 &&
168-
context.feedback.length <= guard.maxLength &&
168+
context.feedback.length <= params.maxLength &&
169169
isNotSpam(context.feedback)
170170
);
171171
},

docs/migration.mdx

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -456,15 +456,15 @@ const machine = createMachine({
456456
</TabItem>
457457
</Tabs>
458458

459-
### Use `params` to pass custom event data
459+
### Use `params` to pass params to actions & guards
460460

461461
:::breakingchange
462462

463463
Breaking change
464464

465465
:::
466466

467-
Properties other than `type` on action objects and guard objects should be nested under a `params` property; `{ type: 'someType', message: 'hello' }` becomes `{ type: 'someType', params: { message: 'hello' }}`:
467+
Properties other than `type` on action objects and guard objects should be nested under a `params` property; `{ type: 'someType', message: 'hello' }` becomes `{ type: 'someType', params: { message: 'hello' }}`. These `params` are then passed to the 2nd argument of the action or guard implementation:
468468

469469
<Tabs>
470470
<TabItem value="v5" label="XState v5 beta">
@@ -478,6 +478,22 @@ const machine = createMachine({
478478
message: 'Hello world',
479479
},
480480
},
481+
on: {
482+
someEvent: {
483+
guard: { type: 'isGreaterThan', params: { value: 42 } },
484+
},
485+
},
486+
}).provide({
487+
actions: {
488+
greet: ({ context, event }, params) => {
489+
console.log(params.message); // 'Hello world'
490+
},
491+
},
492+
guards: {
493+
isGreaterThan: ({ context, event }, params) => {
494+
return event.value > params.value;
495+
},
496+
},
481497
});
482498
```
483499

@@ -487,12 +503,31 @@ const machine = createMachine({
487503

488504
```ts
489505
// ❌ DEPRECATED
490-
const machine = createMachine({
491-
entry: {
492-
type: 'greet',
493-
message: 'Hello world',
506+
const machine = createMachine(
507+
{
508+
entry: {
509+
type: 'greet',
510+
message: 'Hello world',
511+
},
512+
on: {
513+
someEvent: {
514+
cond: { type: 'isGreaterThan', value: 42 },
515+
},
516+
},
494517
},
495-
});
518+
{
519+
actions: {
520+
greet: (context, event, { action }) => {
521+
console.log(action.message); // 'Hello world'
522+
},
523+
},
524+
guards: {
525+
isGreaterThan: (context, event, { guard }) => {
526+
return event.value > guard.value;
527+
},
528+
},
529+
},
530+
);
496531
```
497532

498533
</TabItem>

0 commit comments

Comments
 (0)