Unverified Commit 5348db44 authored by SamPhillemon's avatar SamPhillemon Committed by GitHub

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: 's avatarsamcx <sam@vercel.com>
parent ee48ef70
"use client";
import { useMachine } from "@xstate/react";
import { counterMachine } from "../machines/counter";
import { counterMachine } from "../_machines/counter";
export default function Counter({ counter = {} }) {
const [current, send] = useMachine(counterMachine, {
context: { count: 999 },
});
const [state, send] = useMachine(counterMachine);
return (
<section>
<h2>
Count: <span>{current.context.count}</span>
Count: <span>{state.context.count}</span>
</h2>
<button onClick={() => send("DEC")}>-1</button>
<button onClick={() => send("INC")}>+1</button>
<button onClick={() => send("RESET")}>Reset</button>
<button onClick={() => send({ type: "DEC" })}>-1</button>
<button onClick={() => send({ type: "INC" })}>+1</button>
<button onClick={() => send({ type: "RESET" })}>Reset</button>
</section>
);
}
"use client";
import { useMachine } from "@xstate/react";
import { toggleMachine } from "../machines/toggle";
import { toggleMachine } from "../_machines/toggle";
export default function Toggle() {
const [current, send] = useMachine(toggleMachine);
const [state, send] = useMachine(toggleMachine);
return (
<div>
<h2>
Toogle status: <span>{current.matches("active") ? "On" : "Off"}</span>
Toggle status: <span>{state.matches("active") ? "On" : "Off"}</span>
</h2>
<button onClick={() => send("TOGGLE")}>Toggle</button>
<button onClick={() => send({ type: "TOGGLE" })}>Toggle</button>
</div>
);
}
......@@ -4,21 +4,21 @@ type CounterContext = {
count: number;
};
type CounterEvents = {
type: "INC" | "DEC" | "RESET";
};
type CounterEvents = { type: "INC" } | { type: "DEC" } | { type: "RESET" };
export const counterMachine = createMachine<CounterContext, CounterEvents>({
predictableActionArguments: true,
initial: "active",
context: {
count: 0,
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 }) },
INC: { actions: assign({ count: ({ context }) => context.count + 1 }) },
DEC: { actions: assign({ count: ({ context }) => context.count - 1 }) },
RESET: { actions: assign({ count: 0 }) },
},
},
......
......@@ -8,8 +8,11 @@ type ToggleEvents = {
type: "TOGGLE";
};
export const toggleMachine = createMachine<ToggleContext, ToggleEvents>({
predictableActionArguments: true,
export const toggleMachine = createMachine({
types: {} as {
context: ToggleContext;
events: ToggleEvents;
},
id: "toggle",
initial: "inactive",
states: {
......@@ -20,4 +23,7 @@ export const toggleMachine = createMachine<ToggleContext, ToggleEvents>({
on: { TOGGLE: "inactive" },
},
},
context: {
value: "inactive",
},
});
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>
);
}
import Counter from "../components/Counter";
import Toggle from "../components/Toggle";
import Counter from "./_components/Counter";
import Toggle from "./_components/Toggle";
export default function IndexPage() {
return (
......
......@@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited
// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information.
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
{
"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"
}
}
......@@ -13,8 +13,13 @@
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
"jsx": "preserve",
"plugins": [
{
"name": "next"
}
]
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"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