Skip to content

Commit

Permalink
updated the example of with-xstate to utilize the App Router (#69960)
Browse files Browse the repository at this point in the history
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
Sam-Phillemon9493 and samcx authored Sep 11, 2024
1 parent ee48ef7 commit 5348db4
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 74 deletions.
17 changes: 17 additions & 0 deletions examples/with-xstate/app/_components/Counter.tsx
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>
);
}
15 changes: 15 additions & 0 deletions examples/with-xstate/app/_components/Toggle.tsx
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>
);
}
26 changes: 26 additions & 0 deletions examples/with-xstate/app/_machines/counter.ts
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 }) },
},
},
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -20,4 +23,7 @@ export const toggleMachine = createMachine<ToggleContext, ToggleEvents>({
on: { TOGGLE: "inactive" },
},
},
context: {
value: "inactive",
},
});
16 changes: 16 additions & 0 deletions examples/with-xstate/app/layout.tsx
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>
);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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 (
Expand Down
18 changes: 0 additions & 18 deletions examples/with-xstate/components/Counter.tsx

This file was deleted.

14 changes: 0 additions & 14 deletions examples/with-xstate/components/Toggle.tsx

This file was deleted.

26 changes: 0 additions & 26 deletions examples/with-xstate/machines/counter.ts

This file was deleted.

2 changes: 1 addition & 1 deletion examples/with-xstate/next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
19 changes: 10 additions & 9 deletions examples/with-xstate/package.json
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"
}
}
9 changes: 7 additions & 2 deletions examples/with-xstate/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}

0 comments on commit 5348db4

Please sign in to comment.