-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9aeb9e0
commit 5febfe8
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
'xstate': minor | ||
--- | ||
|
||
Event creators can now be modeled inside of the 2nd argument of `createModel()`, and types for both `context` and `events` will be inferred properly in `createMachine()` when given the `typeof model` as the first generic parameter. | ||
|
||
```ts | ||
import { createModel } from 'xstate/lib/model'; | ||
|
||
const userModel = createModel( | ||
// initial context | ||
{ | ||
name: 'David', | ||
age: 30 | ||
}, | ||
// creators (just events for now) | ||
{ | ||
events: { | ||
updateName: (value: string) => ({ value }), | ||
updateAge: (value: number) => ({ value }), | ||
anotherEvent: () => ({}) // no payload | ||
} | ||
} | ||
); | ||
|
||
const machine = createMachine<typeof userModel>({ | ||
context: userModel.initialContext, | ||
initial: 'active', | ||
states: { | ||
active: { | ||
on: { | ||
updateName: { | ||
/* ... */ | ||
}, | ||
updateAge: { | ||
/* ... */ | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
const nextState = machine.transition( | ||
undefined, | ||
userModel.events.updateName('David') | ||
); | ||
``` |