rsnext/examples/with-xstate/machines/counter.ts
Henrik Wenz b02b2f708c
[Docs] Migrate with-xstate to typescript (#39974)
## Changes

- Update packages
- Migrate to Typescript
- Refactor Component structure
- Normalize Code style

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm lint`
- [x] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
2022-08-26 12:51:01 +00:00

26 lines
581 B
TypeScript

import { createMachine, assign } from 'xstate'
type CounterContext = {
count: number
}
type CounterEvents = {
type: 'INC' | 'DEC' | 'RESET'
}
export const counterMachine = createMachine<CounterContext, CounterEvents>({
predictableActionArguments: true,
initial: 'active',
context: {
count: 0,
},
states: {
active: {
on: {
INC: { actions: assign({ count: (context) => context.count + 1 }) },
DEC: { actions: assign({ count: (context) => context.count - 1 }) },
RESET: { actions: assign({ count: 0 }) },
},
},
},
})