Unverified Commit b02b2f70 authored by Henrik Wenz's avatar Henrik Wenz Committed by GitHub

[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)
parent 060812a1
export default function Counter({ counter = {} }) {
return (
<div>
<h1>
Count: <span>{counter.count}</span>
</h1>
<button onClick={() => counter.decrement()}>-1</button>
<button onClick={() => counter.increment()}>+1</button>
<button onClick={() => counter.reset()}>Reset</button>
</div>
)
}
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>
)
}
const Toggle = ({ onToggle, active }) => {
return (
<div>
<h1>
Toogle status: <span>{active ? 'On' : 'Off'}</span>
</h1>
<button onClick={onToggle}>Toggle</button>
</div>
)
}
export default Toggle
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>
)
}
import Counter from './Counter'
import Toggle from './Toggle'
export { Counter, Toggle }
import { createMachine, assign } from 'xstate' import { createMachine, assign } from 'xstate'
const increment = (context) => context.count + 1 type CounterContext = {
const decrement = (context) => context.count - 1 count: number
}
export const counterMachine = createMachine({ type CounterEvents = {
type: 'INC' | 'DEC' | 'RESET'
}
export const counterMachine = createMachine<CounterContext, CounterEvents>({
predictableActionArguments: true,
initial: 'active', initial: 'active',
context: { context: {
count: 0, count: 0,
...@@ -11,8 +17,8 @@ export const counterMachine = createMachine({ ...@@ -11,8 +17,8 @@ export const counterMachine = createMachine({
states: { states: {
active: { active: {
on: { on: {
INC: { actions: assign({ count: increment }) }, INC: { actions: assign({ count: (context) => context.count + 1 }) },
DEC: { actions: assign({ count: decrement }) }, DEC: { actions: assign({ count: (context) => context.count - 1 }) },
RESET: { actions: assign({ count: 0 }) }, RESET: { actions: assign({ count: 0 }) },
}, },
}, },
......
import { createMachine } from 'xstate' import { createMachine } from 'xstate'
export const toggleMachine = createMachine({ type ToggleContext = {
value: 'inactive' | 'active'
}
type ToggleEvents = {
type: 'TOGGLE'
}
export const toggleMachine = createMachine<ToggleContext, ToggleEvents>({
predictableActionArguments: true,
id: 'toggle', id: 'toggle',
initial: 'inactive', initial: 'inactive',
states: { states: {
......
...@@ -6,10 +6,15 @@ ...@@ -6,10 +6,15 @@
"start": "next start" "start": "next start"
}, },
"dependencies": { "dependencies": {
"@xstate/react": "^1.3.1", "@xstate/react": "^3.0.1",
"next": "latest", "next": "12.2.5",
"react": "^17.0.2", "react": "^18.2.0",
"react-dom": "^17.0.2", "react-dom": "^18.2.0",
"xstate": "^4.17.0" "xstate": "^4.33.3"
},
"devDependencies": {
"@types/node": "18.7.13",
"@types/react": "16.9.17",
"typescript": "4.6.3"
} }
} }
import { useMachine } from '@xstate/react'
import { Counter, Toggle } from '../components'
import { toggleMachine } from '../machines/toggleMachine'
import { counterMachine } from '../machines/counterMachine'
const IndexPage = () => {
const [toggleCurrent, toggleSend] = useMachine(toggleMachine)
const [counterCurrent, counterSend] = useMachine(counterMachine, {
context: { count: 999 },
})
return (
<div>
<Counter
counter={{
count: counterCurrent.context.count,
increment: () => counterSend('INC'),
decrement: () => counterSend('DEC'),
reset: () => counterSend('RESET'),
}}
/>
<hr />
<Toggle
onToggle={() => toggleSend('TOGGLE')}
active={toggleCurrent.matches('active')}
/>
</div>
)
}
export default IndexPage
import Counter from '../components/Counter'
import Toggle from '../components/Toggle'
export default function IndexPage() {
return (
<>
<h1>XState Example</h1>
<Counter />
<hr />
<Toggle />
</>
)
}
{
"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"]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment