diff --git a/examples/with-unstated/README.md b/examples/with-unstated/README.md new file mode 100644 index 00000000..2873b7d5 --- /dev/null +++ b/examples/with-unstated/README.md @@ -0,0 +1,44 @@ +[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/canary/examples/with-unstated) + +# Unstated example + +## How to use + +### Using `create-next-app` + +Execute [`create-next-app`](https://github.com/segmentio/create-next-app) with [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) or [npx](https://github.com/zkat/npx#readme) to bootstrap the example: + +```bash +npx create-next-app --example with-unstated with-unstated-app +# or +yarn create next-app --example with-unstated with-unstated-app +``` + +### Download manually + +Download the example [or clone the repo](https://github.com/zeit/next.js): + +```bash +curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-unstated +cd with-unstated +``` + +Install it and run: + +```bash +npm install +npm run dev +# or +yarn +yarn dev +``` + +Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download)) + +```bash +now +``` + +## The idea behind the example + +This example shows how to integrate Unstated in Next.js. For more info about Unstated you can visit [here](https://github.com/jamiebuilds/unstated). The example is basically same as [redux example](https://github.com/zeit/next.js/tree/canary/examples/with-redux) \ No newline at end of file diff --git a/examples/with-unstated/components/Clock.js b/examples/with-unstated/components/Clock.js new file mode 100644 index 00000000..1aba00fe --- /dev/null +++ b/examples/with-unstated/components/Clock.js @@ -0,0 +1,23 @@ +export default ({ clock: { state: { lastUpdate, light } } }) => { + return ( +
+ {format(new Date(lastUpdate))} + +
+ ) +} + +const format = t => `${pad(t.getUTCHours())}:${pad(t.getUTCMinutes())}:${pad(t.getUTCSeconds())}` + +const pad = n => n < 10 ? `0${n}` : n diff --git a/examples/with-unstated/components/Counter.js b/examples/with-unstated/components/Counter.js new file mode 100644 index 00000000..c1989d04 --- /dev/null +++ b/examples/with-unstated/components/Counter.js @@ -0,0 +1,8 @@ +export default ({ counter }) => ( +
+

Count: {counter.state.count}

+ + + +
+) diff --git a/examples/with-unstated/components/index.js b/examples/with-unstated/components/index.js new file mode 100644 index 00000000..3a113c2d --- /dev/null +++ b/examples/with-unstated/components/index.js @@ -0,0 +1,4 @@ +import Clock from './Clock' +import Counter from './Counter' + +export { Clock, Counter } diff --git a/examples/with-unstated/containers/ClockContainer.js b/examples/with-unstated/containers/ClockContainer.js new file mode 100644 index 00000000..fd6c81d8 --- /dev/null +++ b/examples/with-unstated/containers/ClockContainer.js @@ -0,0 +1,14 @@ +import { Container } from 'unstated' + +export default class ClockContainer extends Container { + state = { + lastUpdate: 0, + light: false + } + constructor () { + super() + this.interval = setInterval(() => { + this.setState((state) => ({ light: !state.light, lastUpdate: Date.now() })) + }, 1000) + } +} diff --git a/examples/with-unstated/containers/CounterContainer.js b/examples/with-unstated/containers/CounterContainer.js new file mode 100644 index 00000000..ad8c5e55 --- /dev/null +++ b/examples/with-unstated/containers/CounterContainer.js @@ -0,0 +1,19 @@ +import { Container } from 'unstated' + +export default class CounterContainer extends Container { + state = { + count: 0 + } + + increment = () => { + this.setState((state) => ({ count: state.count + 1 })) + } + + decrement = () => { + this.setState((state) => ({ count: state.count - 1 })) + } + + reset = () => { + this.setState({ count: 0 }) + } +} diff --git a/examples/with-unstated/containers/index.js b/examples/with-unstated/containers/index.js new file mode 100644 index 00000000..791aaa05 --- /dev/null +++ b/examples/with-unstated/containers/index.js @@ -0,0 +1,4 @@ +import CounterContainer from './CounterContainer' +import ClockContainer from './ClockContainer' + +export { CounterContainer, ClockContainer } diff --git a/examples/with-unstated/package.json b/examples/with-unstated/package.json new file mode 100644 index 00000000..a404c739 --- /dev/null +++ b/examples/with-unstated/package.json @@ -0,0 +1,15 @@ +{ + "name": "with-unstated", + "version": "1.0.0", + "scripts": { + "dev": "next", + "build": "next build", + "start": "next start" + }, + "dependencies": { + "next": "latest", + "react": "^16.4.1", + "react-dom": "^16.4.1", + "unstated": "^2.1.1" + } +} diff --git a/examples/with-unstated/pages/_app.js b/examples/with-unstated/pages/_app.js new file mode 100644 index 00000000..297c5ed2 --- /dev/null +++ b/examples/with-unstated/pages/_app.js @@ -0,0 +1,18 @@ +import App, {Container} from 'next/app' +import React from 'react' +import { Provider } from 'unstated' + +class MyApp extends App { + render () { + const {Component, pageProps} = this.props + return ( + + + + + + ) + } +} + +export default MyApp diff --git a/examples/with-unstated/pages/index.js b/examples/with-unstated/pages/index.js new file mode 100644 index 00000000..20c50c03 --- /dev/null +++ b/examples/with-unstated/pages/index.js @@ -0,0 +1,29 @@ +import React from 'react' +import { Subscribe } from 'unstated' +import { ClockContainer, CounterContainer } from '../containers' +import { Clock, Counter } from '../components' + +class Index extends React.Component { + componentWillUnmount () { + clearInterval(this.timer) + } + render () { + return ( + + { + (clock, counter) => { + this.timer = clock.interval + return ( +
+ + +
+ ) + } + } +
+ ) + } +} + +export default Index