-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated the example of with-xstate to utilize the App Router (#69960)
This PR updates the with-xstate example to use the App Router. Here are the changes that have been made: - Renamed the `pages` folder to the `app` folder. - Updated the routing for / files to align with the App Router. - moved the `components` and `machine` folder inside the app directory by renaming it to `_components` and `_machine` - Added the `layout.tsx` file as part of the App Router. - Updated the package.json file. CC: @samcx --------- Co-authored-by: samcx <sam@vercel.com>
- Loading branch information
1 parent
ee48ef7
commit 5348db4
Showing
12 changed files
with
102 additions
and
74 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,17 @@ | ||
"use client"; | ||
import { useMachine } from "@xstate/react"; | ||
import { counterMachine } from "../_machines/counter"; | ||
|
||
export default function Counter({ counter = {} }) { | ||
const [state, send] = useMachine(counterMachine); | ||
return ( | ||
<section> | ||
<h2> | ||
Count: <span>{state.context.count}</span> | ||
</h2> | ||
<button onClick={() => send({ type: "DEC" })}>-1</button> | ||
<button onClick={() => send({ type: "INC" })}>+1</button> | ||
<button onClick={() => send({ type: "RESET" })}>Reset</button> | ||
</section> | ||
); | ||
} |
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,15 @@ | ||
"use client"; | ||
import { useMachine } from "@xstate/react"; | ||
import { toggleMachine } from "../_machines/toggle"; | ||
|
||
export default function Toggle() { | ||
const [state, send] = useMachine(toggleMachine); | ||
return ( | ||
<div> | ||
<h2> | ||
Toggle status: <span>{state.matches("active") ? "On" : "Off"}</span> | ||
</h2> | ||
<button onClick={() => send({ type: "TOGGLE" })}>Toggle</button> | ||
</div> | ||
); | ||
} |
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" } | { type: "DEC" } | { type: "RESET" }; | ||
|
||
export const counterMachine = createMachine({ | ||
types: {} as { | ||
context: CounterContext; | ||
events: CounterEvents; | ||
}, | ||
id: "counter", | ||
initial: "active", | ||
context: { count: 999 }, | ||
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const metadata = { | ||
title: "Next.js", | ||
description: "Generated by Next.js", | ||
}; | ||
|
||
export default function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
return ( | ||
<html lang="en"> | ||
<body>{children}</body> | ||
</html> | ||
); | ||
} |
4 changes: 2 additions & 2 deletions
4
examples/with-xstate/pages/index.tsx → examples/with-xstate/app/page.tsx
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 was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,20 +1,21 @@ | ||
{ | ||
"private": true, | ||
"scripts": { | ||
"dev": "next", | ||
"dev": "next dev", | ||
"build": "next build", | ||
"start": "next start" | ||
}, | ||
"dependencies": { | ||
"@xstate/react": "^3.0.1", | ||
"next": "12.2.5", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"xstate": "^4.33.3" | ||
"@xstate/react": "^4.1.2", | ||
"next": "latest", | ||
"react": "^18.3.1", | ||
"react-dom": "^18.3.1", | ||
"xstate": "^5.18.1" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "18.7.13", | ||
"@types/react": "16.9.17", | ||
"typescript": "4.6.3" | ||
"@types/node": "^22.5.4", | ||
"@types/react": "^18.3.5", | ||
"@types/react-dom": "^18.3.0", | ||
"typescript": "^5.6.2" | ||
} | ||
} |
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