Unverified Commit 661f3fbd authored by PapatMayuri's avatar PapatMayuri Committed by GitHub

Updated the example of with-unstated to utilize the App Router. (#73211)

This PR updates the with-unstated  example for using the App Router.
Here are the changes that have been made:

- Renamed the "pages" folder to the "app" folder.
- Added the layout.tsx file as part of the App Router.
- Moved the components folder inside app folder.
- Updated the package.json file.
- Added hooks for counter and clock.

    

CC: @samcx

---------
Co-authored-by: 's avatarsamcx <sam@vercel.com>
parent 9cbc4d88
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz
# testing
/coverage
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
*.pem
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# local env files
.env*.local
# vercel
.vercel
# typescript
*.tsbuildinfo
next-env.d.ts
# Unstated example
This example shows how to integrate [Unstated Next](https://github.com/jamiebuilds/unstated-next) with Next.js.
There are two pages, `/` and `/about`, both render a counter and a timer, the state is saved for the current page and reset when switching to a different page. To keep a shared state between pages you can add the state providers to `pages/_app.js` instead of using the page.
## Deploy your own
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) or preview live with [StackBlitz](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/with-unstated)
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-unstated&project-name=with-unstated&repository-name=with-unstated)
## How to use
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:
```bash
npx create-next-app --example with-unstated with-unstated-app
```
```bash
yarn create next-app --example with-unstated with-unstated-app
```
```bash
pnpm create next-app --example with-unstated with-unstated-app
```
Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
import ClockContainer from "../containers/clock";
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();
return (
<div className={clock.light ? "light" : ""}>
{format(new Date(clock.lastUpdate))}
<style jsx>{`
div {
padding: 15px;
display: inline-block;
color: #82fa58;
font:
50px menlo,
monaco,
monospace;
background-color: #000;
}
.light {
background-color: #999;
}
`}</style>
</div>
);
}
import CounterContainer from "../containers/counter";
export default function Counter() {
const counter = CounterContainer.useContainer();
return (
<div>
<h1>
Count: <span>{counter.count}</span>
</h1>
<button onClick={() => counter.decrement()}>-1</button>
<button onClick={() => counter.increment()}>+1</button>
<button onClick={() => counter.reset()}>Reset</button>
</div>
);
}
import { useState, useEffect } from "react";
import { createContainer } from "unstated-next";
function useClock() {
const [data, setData] = useState({ lastUpdate: 0, light: false });
useEffect(() => {
let interval = setInterval(() => {
setData({ lastUpdate: Date.now(), light: !data.light });
}, 1000);
return () => clearInterval(interval);
}, [data.light]);
return data;
}
export default createContainer(useClock);
import { useState } from "react";
import { createContainer } from "unstated-next";
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 };
}
export default createContainer(useCounter);
{
"private": true,
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"unstated-next": "1.1.0"
}
}
import Link from "next/link";
import ClockContainer from "../containers/clock";
import CounterContainer from "../containers/counter";
import Clock from "../components/Clock";
import Counter from "../components/Counter";
export default function Index() {
return (
<CounterContainer.Provider>
<ClockContainer.Provider>
<div>
<Link href="/">go to Index</Link>
<br />
<br />
<div>
<Clock />
<Counter />
</div>
</div>
</ClockContainer.Provider>
</CounterContainer.Provider>
);
}
import Link from "next/link";
import ClockContainer from "../containers/clock";
import CounterContainer from "../containers/counter";
import Clock from "../components/Clock";
import Counter from "../components/Counter";
export default function Index() {
return (
<CounterContainer.Provider>
<ClockContainer.Provider>
<div>
<Link href="/about">go to About</Link>
<br />
<br />
<div>
<Clock />
<Counter />
</div>
</div>
</ClockContainer.Provider>
</CounterContainer.Provider>
);
}
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