Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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)
- Loading branch information
Showing
12 changed files
with
111 additions
and
85 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,18 @@ | ||
import { useMachine } from '@xstate/react' | ||
import { counterMachine } from '../machines/counter' | ||
|
||
export default function Counter({ counter = {} }) { | ||
const [current, send] = useMachine(counterMachine, { | ||
context: { count: 999 }, | ||
}) | ||
return ( | ||
<section> | ||
<h2> | ||
Count: <span>{current.context.count}</span> | ||
</h2> | ||
<button onClick={() => send('DEC')}>-1</button> | ||
<button onClick={() => send('INC')}>+1</button> | ||
<button onClick={() => send('RESET')}>Reset</button> | ||
</section> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
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,14 @@ | ||
import { useMachine } from '@xstate/react' | ||
import { toggleMachine } from '../machines/toggle' | ||
|
||
export default function Toggle() { | ||
const [current, send] = useMachine(toggleMachine) | ||
return ( | ||
<div> | ||
<h2> | ||
Toogle status: <span>{current.matches('active') ? 'On' : 'Off'}</span> | ||
</h2> | ||
<button onClick={() => send('TOGGLE')}>Toggle</button> | ||
</div> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
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,26 @@ | ||
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 }) }, | ||
}, | ||
}, | ||
}, | ||
}) |
This file was deleted.
Oops, something went wrong.
11 changes: 10 additions & 1 deletion
11
...les/with-xstate/machines/toggleMachine.js → examples/with-xstate/machines/toggle.ts
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
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
This file was deleted.
Oops, something went wrong.
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,13 @@ | ||
import Counter from '../components/Counter' | ||
import Toggle from '../components/Toggle' | ||
|
||
export default function IndexPage() { | ||
return ( | ||
<> | ||
<h1>XState Example</h1> | ||
<Counter /> | ||
<hr /> | ||
<Toggle /> | ||
</> | ||
) | ||
} |
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,20 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"lib": ["dom", "dom.iterable", "esnext"], | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"strict": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"noEmit": true, | ||
"incremental": true, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"jsx": "preserve" | ||
}, | ||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"], | ||
"exclude": ["node_modules"] | ||
} |