From dd595371b36b4f2d2d92ace4e0d9336c31fa93e9 Mon Sep 17 00:00:00 2001 From: PapatMayuri Date: Tue, 26 Nov 2024 18:44:39 +0530 Subject: [PATCH 1/3] Updated the example of with-unstated to utilize the App Router. --- .../{components => app/_components}/Clock.js | 10 +++--- .../with-unstated/app/_components/Counter.js | 17 +++++++++ examples/with-unstated/app/about/page.tsx | 17 +++++++++ .../clock.js => app/hooks/useClock.ts} | 9 +++-- .../with-unstated/app/hooks/useCounter.ts | 13 +++++++ examples/with-unstated/app/layout.tsx | 16 +++++++++ examples/with-unstated/app/page.tsx | 17 +++++++++ examples/with-unstated/components/Counter.js | 16 --------- examples/with-unstated/containers/counter.js | 13 ------- examples/with-unstated/package.json | 11 ++++-- examples/with-unstated/pages/about.js | 23 ------------ examples/with-unstated/pages/index.js | 23 ------------ examples/with-unstated/tsconfig.json | 35 +++++++++++++++++++ 13 files changed, 132 insertions(+), 88 deletions(-) rename examples/with-unstated/{components => app/_components}/Clock.js (73%) create mode 100644 examples/with-unstated/app/_components/Counter.js create mode 100644 examples/with-unstated/app/about/page.tsx rename examples/with-unstated/{containers/clock.js => app/hooks/useClock.ts} (65%) create mode 100644 examples/with-unstated/app/hooks/useCounter.ts create mode 100644 examples/with-unstated/app/layout.tsx create mode 100644 examples/with-unstated/app/page.tsx delete mode 100644 examples/with-unstated/components/Counter.js delete mode 100644 examples/with-unstated/containers/counter.js delete mode 100644 examples/with-unstated/pages/about.js delete mode 100644 examples/with-unstated/pages/index.js create mode 100644 examples/with-unstated/tsconfig.json diff --git a/examples/with-unstated/components/Clock.js b/examples/with-unstated/app/_components/Clock.js similarity index 73% rename from examples/with-unstated/components/Clock.js rename to examples/with-unstated/app/_components/Clock.js index 546a9a3ed6103..890631d028155 100644 --- a/examples/with-unstated/components/Clock.js +++ b/examples/with-unstated/app/_components/Clock.js @@ -1,16 +1,16 @@ -import ClockContainer from "../containers/clock"; - +"use client" +import { useClock } from "../hooks/useClock"; const pad = (n) => (n < 10 ? `0${n}` : n); const format = (t) => `${pad(t.getUTCHours())}:${pad(t.getUTCMinutes())}:${pad(t.getUTCSeconds())}`; export default function Clock() { - const clock = ClockContainer.useContainer(); + const { lastUpdate, light } = useClock(); return ( -
- {format(new Date(clock.lastUpdate))} +
+ {format(new Date(lastUpdate))} -
- ); -} diff --git a/examples/with-unstated/app/_components/Counter.js b/examples/with-unstated/app/_components/Counter.js deleted file mode 100644 index 66d23b05842ca..0000000000000 --- a/examples/with-unstated/app/_components/Counter.js +++ /dev/null @@ -1,17 +0,0 @@ -"use client" -import { useCounter } from "../hooks/useCounter"; // import the custom hook - -export default function Counter() { - const { count, increment, decrement, reset } = useCounter(); - - return ( -
-

- Count: {count} -

- - - -
- ); -} diff --git a/examples/with-unstated/app/about/page.tsx b/examples/with-unstated/app/about/page.tsx deleted file mode 100644 index 08be690d7ad5c..0000000000000 --- a/examples/with-unstated/app/about/page.tsx +++ /dev/null @@ -1,17 +0,0 @@ -import Link from "next/link"; -import Clock from "../_components/Clock"; -import Counter from "../_components/Counter"; - -export default function Index() { - return ( -
- go to Index -
-
-
- - -
-
- ); -} diff --git a/examples/with-unstated/app/hooks/useClock.ts b/examples/with-unstated/app/hooks/useClock.ts deleted file mode 100644 index 20528e5cdd83c..0000000000000 --- a/examples/with-unstated/app/hooks/useClock.ts +++ /dev/null @@ -1,17 +0,0 @@ -"use client"; - -import { useState, useEffect } from "react"; - -export function useClock() { - const [data, setData] = useState({ lastUpdate: 0, light: false }); - - useEffect(() => { - const interval = setInterval(() => { - setData({ lastUpdate: Date.now(), light: !data.light }); - }, 1000); - - return () => clearInterval(interval); - }, [data.light]); - - return data; -} diff --git a/examples/with-unstated/app/hooks/useCounter.ts b/examples/with-unstated/app/hooks/useCounter.ts deleted file mode 100644 index cfa16f8638c6c..0000000000000 --- a/examples/with-unstated/app/hooks/useCounter.ts +++ /dev/null @@ -1,13 +0,0 @@ -"use client"; - -import { useState } from "react"; - -export function useCounter() { - const [count, setCount] = useState(0); - const decrement = () => setCount(count - 1); - const increment = () => setCount(count + 1); - const reset = () => setCount(0); - - return { count, decrement, increment, reset }; - } - \ No newline at end of file diff --git a/examples/with-unstated/app/layout.tsx b/examples/with-unstated/app/layout.tsx deleted file mode 100644 index a14e64fcd5e33..0000000000000 --- a/examples/with-unstated/app/layout.tsx +++ /dev/null @@ -1,16 +0,0 @@ -export const metadata = { - title: 'Next.js', - description: 'Generated by Next.js', -} - -export default function RootLayout({ - children, -}: { - children: React.ReactNode -}) { - return ( - - {children} - - ) -} diff --git a/examples/with-unstated/app/page.tsx b/examples/with-unstated/app/page.tsx deleted file mode 100644 index 60e4189b9b9b4..0000000000000 --- a/examples/with-unstated/app/page.tsx +++ /dev/null @@ -1,17 +0,0 @@ -import Link from "next/link"; -import Clock from "./_components/Clock"; -import Counter from "./_components/Counter"; - -export default function Page() { - return ( -
- Go to About -
-
-
- - -
-
- ); -} diff --git a/examples/with-unstated/package.json b/examples/with-unstated/package.json deleted file mode 100644 index bfa7ed22087da..0000000000000 --- a/examples/with-unstated/package.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "private": true, - "scripts": { - "dev": "next dev", - "build": "next build", - "start": "next start" - }, - "dependencies": { - "next": "latest", - "react": "^18.3.1", - "react-dom": "^18.3.1", - "unstated-next": "^1.1.0" - }, - "devDependencies": { - "@types/node": "^22.10.0", - "@types/react": "^18.3.12", - "typescript": "^5.7.2" - } -} diff --git a/examples/with-unstated/tsconfig.json b/examples/with-unstated/tsconfig.json deleted file mode 100644 index f619795e7255f..0000000000000 --- a/examples/with-unstated/tsconfig.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "compilerOptions": { - "target": "ES2017", - "lib": [ - "dom", - "dom.iterable", - "esnext" - ], - "allowJs": true, - "skipLibCheck": true, - "strict": false, - "noEmit": true, - "incremental": true, - "module": "esnext", - "esModuleInterop": true, - "moduleResolution": "node", - "resolveJsonModule": true, - "isolatedModules": true, - "jsx": "preserve", - "plugins": [ - { - "name": "next" - } - ] - }, - "include": [ - "next-env.d.ts", - ".next/types/**/*.ts", - "**/*.ts", - "**/*.tsx" - ], - "exclude": [ - "node_modules" - ] -}