diff --git a/examples/with-mobx-react-lite/.babelrc b/examples/with-mobx-react-lite/.babelrc deleted file mode 100644 index 297803ec0d746..0000000000000 --- a/examples/with-mobx-react-lite/.babelrc +++ /dev/null @@ -1,4 +0,0 @@ -{ - "presets": ["next/babel"], - "plugins": [["@babel/plugin-proposal-class-properties", { "loose": false }]] -} diff --git a/examples/with-mobx-react-lite/.gitignore b/examples/with-mobx-react-lite/.gitignore deleted file mode 100644 index c87c9b392c020..0000000000000 --- a/examples/with-mobx-react-lite/.gitignore +++ /dev/null @@ -1,36 +0,0 @@ -# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. - -# dependencies -/node_modules -/.pnp -.pnp.js - -# testing -/coverage - -# next.js -/.next/ -/out/ - -# production -/build - -# misc -.DS_Store -*.pem - -# debug -npm-debug.log* -yarn-debug.log* -yarn-error.log* -.pnpm-debug.log* - -# local env files -.env*.local - -# vercel -.vercel - -# typescript -*.tsbuildinfo -next-env.d.ts diff --git a/examples/with-mobx-react-lite/README.md b/examples/with-mobx-react-lite/README.md index 80fd5d8791fcf..480d868bd1ead 100644 --- a/examples/with-mobx-react-lite/README.md +++ b/examples/with-mobx-react-lite/README.md @@ -1,41 +1,3 @@ -# MobX V6 with Mobx React Lite +# MobX React Light -Usually splitting your app state into `pages` feels natural but sometimes you'll want to have global state for your app. This is an example on how you can use mobx that also works with our universal rendering approach. - -In this example we are going to display a digital clock that updates every second. The first render is happening in the server and then the browser will take over. To illustrate this, the server rendered clock will have a different background color than the client one. - -To illustrate SSG and SSR, go to `/ssg` and `/ssr`, those pages are using Next.js data fetching methods to get the date in the server and return it as props to the page, and then the browser will hydrate the store and continue updating the date. - -The trick here for supporting universal mobx is to separate the cases for the client and the server. When we are on the server we want to create a new store every time, otherwise different users data will be mixed up. If we are in the client we want to use always the same store. That's what we accomplish on `store.js`. - -Page.js component is using the clock store to start and stop the store clock. - -Clock.js component is using the clock store to read the time. - -StoreProvider.js component is used to instantiate the `Store` both on the server and on the client. - -Both components are using a custom hook `useStore` to pull in the `Store` from the provider. - -## 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-mobx-react-lite) - -[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-mobx-react-lite&project-name=with-mobx-react-lite&repository-name=with-mobx-react-lite) - -## 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-mobx-react-lite with-mobx-react-lite-app -``` - -```bash -yarn create next-app --example with-mobx-react-lite with-mobx-react-lite-app -``` - -```bash -pnpm create next-app --example with-mobx-react-lite with-mobx-react-lite-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)). +**Note:** This example has been moved to [examples/with-mobx](../with-mobx/) diff --git a/examples/with-mobx-react-lite/components/Clock.js b/examples/with-mobx-react-lite/components/Clock.js deleted file mode 100644 index 18a2dd149fe90..0000000000000 --- a/examples/with-mobx-react-lite/components/Clock.js +++ /dev/null @@ -1,28 +0,0 @@ -import { observer } from 'mobx-react-lite' -import { useStore } from './StoreProvider' - -const Clock = observer(function Clock(props) { - // use store from the store context - const store = useStore() - - return ( -
- {store.timeString} - -
- ) -}) - -export default Clock diff --git a/examples/with-mobx-react-lite/components/Page.js b/examples/with-mobx-react-lite/components/Page.js deleted file mode 100644 index 183a3e96d261d..0000000000000 --- a/examples/with-mobx-react-lite/components/Page.js +++ /dev/null @@ -1,34 +0,0 @@ -import { observer } from 'mobx-react-lite' -import Link from 'next/link' -import { useEffect } from 'react' -import Clock from './Clock' -import { useStore } from './StoreProvider' - -const Page = observer(function Page(props) { - // use store from the store context - const store = useStore() - - //start the clock when the component is mounted - useEffect(() => { - store.start() - - // stop the clock when the component unmounts - return () => { - store.stop() - } - }, [store]) - - return ( -
-

{props.title}

- - -
- ) -}) - -export default Page diff --git a/examples/with-mobx-react-lite/package.json b/examples/with-mobx-react-lite/package.json deleted file mode 100644 index a2142bbe70de7..0000000000000 --- a/examples/with-mobx-react-lite/package.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "private": true, - "scripts": { - "dev": "next dev", - "build": "next build", - "start": "next start" - }, - "dependencies": { - "mobx": "^6.0.3", - "mobx-react-lite": "^3.1.5", - "next": "latest", - "react": "^17.0.2", - "react-dom": "^17.0.2" - }, - "devDependencies": { - "@babel/core": "7.14.5", - "@babel/plugin-proposal-class-properties": "^7.1.0" - } -} diff --git a/examples/with-mobx-react-lite/pages/_app.js b/examples/with-mobx-react-lite/pages/_app.js deleted file mode 100644 index 7aff31ee3c05c..0000000000000 --- a/examples/with-mobx-react-lite/pages/_app.js +++ /dev/null @@ -1,9 +0,0 @@ -import { StoreProvider } from '../components/StoreProvider' - -export default function App({ Component, pageProps }) { - return ( - - - - ) -} diff --git a/examples/with-mobx-react-lite/pages/index.js b/examples/with-mobx-react-lite/pages/index.js deleted file mode 100644 index 95c2063569583..0000000000000 --- a/examples/with-mobx-react-lite/pages/index.js +++ /dev/null @@ -1,5 +0,0 @@ -import Page from '../components/Page' - -export default function Index() { - return -} diff --git a/examples/with-mobx-react-lite/pages/other.js b/examples/with-mobx-react-lite/pages/other.js deleted file mode 100644 index 7919d82fa7a64..0000000000000 --- a/examples/with-mobx-react-lite/pages/other.js +++ /dev/null @@ -1,5 +0,0 @@ -import Page from '../components/Page' - -export default function Other() { - return -} diff --git a/examples/with-mobx-react-lite/pages/ssg.js b/examples/with-mobx-react-lite/pages/ssg.js deleted file mode 100644 index 496dac3e7063f..0000000000000 --- a/examples/with-mobx-react-lite/pages/ssg.js +++ /dev/null @@ -1,11 +0,0 @@ -import Page from '../components/Page' - -export default function SSG() { - return -} - -// If you build and start the app, the date returned here will have the same -// value for all requests, as this method gets executed at build time. -export function getStaticProps() { - return { props: { initialState: { lastUpdate: Date.now() } } } -} diff --git a/examples/with-mobx-react-lite/pages/ssr.js b/examples/with-mobx-react-lite/pages/ssr.js deleted file mode 100644 index f414935aa0b25..0000000000000 --- a/examples/with-mobx-react-lite/pages/ssr.js +++ /dev/null @@ -1,12 +0,0 @@ -import Page from '../components/Page' - -export default function SSR() { - return -} - -// The date returned here will be different for every request that hits the page, -// that is because the page becomes a serverless function instead of being statically -// exported when you use `getServerSideProps` or `getInitialProps` -export function getServerSideProps() { - return { props: { initialState: { lastUpdate: Date.now() } } } -} diff --git a/examples/with-mobx-react-lite/store.js b/examples/with-mobx-react-lite/store.js deleted file mode 100644 index f21bf6099d773..0000000000000 --- a/examples/with-mobx-react-lite/store.js +++ /dev/null @@ -1,46 +0,0 @@ -import { action, observable, computed, runInAction, makeObservable } from 'mobx' -import { enableStaticRendering } from 'mobx-react-lite' - -enableStaticRendering(typeof window === 'undefined') - -export class Store { - lastUpdate = 0 - light = false - - constructor() { - makeObservable(this, { - lastUpdate: observable, - light: observable, - start: action, - hydrate: action, - timeString: computed, - }) - } - - start = () => { - this.timer = setInterval(() => { - runInAction(() => { - this.lastUpdate = Date.now() - this.light = true - }) - }, 1000) - } - - get timeString() { - const pad = (n) => (n < 10 ? `0${n}` : n) - const format = (t) => - `${pad(t.getUTCHours())}:${pad(t.getUTCMinutes())}:${pad( - t.getUTCSeconds() - )}` - return format(new Date(this.lastUpdate)) - } - - stop = () => clearInterval(this.timer) - - hydrate = (data) => { - if (!data) return - - this.lastUpdate = data.lastUpdate !== null ? data.lastUpdate : Date.now() - this.light = !!data.light - } -} diff --git a/examples/with-mobx/.babelrc b/examples/with-mobx/.babelrc deleted file mode 100644 index df448a053ed45..0000000000000 --- a/examples/with-mobx/.babelrc +++ /dev/null @@ -1,7 +0,0 @@ -{ - "presets": ["next/babel"], - "plugins": [ - ["@babel/plugin-proposal-decorators", { "legacy": true }], - ["@babel/plugin-proposal-class-properties", { "loose": false }] - ] -} diff --git a/examples/with-mobx/README.md b/examples/with-mobx/README.md index ebc3802e7125f..80fd5d8791fcf 100644 --- a/examples/with-mobx/README.md +++ b/examples/with-mobx/README.md @@ -1,4 +1,4 @@ -# MobX example +# MobX V6 with Mobx React Lite Usually splitting your app state into `pages` feels natural but sometimes you'll want to have global state for your app. This is an example on how you can use mobx that also works with our universal rendering approach. @@ -8,28 +8,34 @@ To illustrate SSG and SSR, go to `/ssg` and `/ssr`, those pages are using Next.j The trick here for supporting universal mobx is to separate the cases for the client and the server. When we are on the server we want to create a new store every time, otherwise different users data will be mixed up. If we are in the client we want to use always the same store. That's what we accomplish on `store.js`. -The clock, under `components/Clock.js`, has access to the state using the `inject` and `observer` functions from `mobx-react`. In this case Clock is a direct child from the page but it could be deep down the render tree. +Page.js component is using the clock store to start and stop the store clock. + +Clock.js component is using the clock store to read the time. + +StoreProvider.js component is used to instantiate the `Store` both on the server and on the client. + +Both components are using a custom hook `useStore` to pull in the `Store` from the provider. ## 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-mobx) +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-mobx-react-lite) -[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-mobx&project-name=with-mobx&repository-name=with-mobx) +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-mobx-react-lite&project-name=with-mobx-react-lite&repository-name=with-mobx-react-lite) ## 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-mobx with-mobx-app +npx create-next-app --example with-mobx-react-lite with-mobx-react-lite-app ``` ```bash -yarn create next-app --example with-mobx with-mobx-app +yarn create next-app --example with-mobx-react-lite with-mobx-react-lite-app ``` ```bash -pnpm create next-app --example with-mobx with-mobx-app +pnpm create next-app --example with-mobx-react-lite with-mobx-react-lite-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)). diff --git a/examples/with-mobx/components/Clock.js b/examples/with-mobx/components/Clock.js index 73e6b6e2d108b..18a2dd149fe90 100644 --- a/examples/with-mobx/components/Clock.js +++ b/examples/with-mobx/components/Clock.js @@ -1,8 +1,13 @@ -import { observer } from 'mobx-react' -const Clock = observer((props) => { +import { observer } from 'mobx-react-lite' +import { useStore } from './StoreProvider' + +const Clock = observer(function Clock(props) { + // use store from the store context + const store = useStore() + return ( -
- {props.timeString} +
+ {store.timeString}