Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs] Migrate with-xstate to typescript #39974

Merged
merged 2 commits into from Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 0 additions & 12 deletions examples/with-xstate/components/Counter.js

This file was deleted.

18 changes: 18 additions & 0 deletions examples/with-xstate/components/Counter.tsx
@@ -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>
)
}
12 changes: 0 additions & 12 deletions examples/with-xstate/components/Toggle.js

This file was deleted.

14 changes: 14 additions & 0 deletions examples/with-xstate/components/Toggle.tsx
@@ -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>
)
}
4 changes: 0 additions & 4 deletions examples/with-xstate/components/index.js

This file was deleted.

26 changes: 26 additions & 0 deletions examples/with-xstate/machines/counter.ts
@@ -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 }) },
},
},
},
})
20 changes: 0 additions & 20 deletions examples/with-xstate/machines/counterMachine.js

This file was deleted.

@@ -1,6 +1,15 @@
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',
initial: 'inactive',
states: {
Expand Down
15 changes: 10 additions & 5 deletions examples/with-xstate/package.json
Expand Up @@ -6,10 +6,15 @@
"start": "next start"
},
"dependencies": {
"@xstate/react": "^1.3.1",
"next": "latest",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"xstate": "^4.17.0"
"@xstate/react": "^3.0.1",
"next": "12.2.5",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"xstate": "^4.33.3"
},
"devDependencies": {
"@types/node": "18.7.13",
"@types/react": "16.9.17",
"typescript": "4.6.3"
}
}
31 changes: 0 additions & 31 deletions examples/with-xstate/pages/index.js

This file was deleted.

13 changes: 13 additions & 0 deletions examples/with-xstate/pages/index.tsx
@@ -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 />
</>
)
}
20 changes: 20 additions & 0 deletions examples/with-xstate/tsconfig.json
@@ -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"]
}