Skip to content

Commit

Permalink
Example/update unstated (#11534)
Browse files Browse the repository at this point in the history
* Updated components and deps

* Updated readme
  • Loading branch information
Luis Alvarez D committed Apr 1, 2020
1 parent d61eced commit 1992e2a
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 174 deletions.
4 changes: 2 additions & 2 deletions examples/with-unstated/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Unstated 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).
This example shows how to integrate [Unstated Next](https://github.com/jamiebuilds/unstated-next) with Next.js.

The "counter" shows you how to preserve state from server to client and share the state within different page, you can expect the same state for "counter" when switching between Index and About page, observe that "counter" behaves differently from the "clock" example.
There are two pages, `/` and `/about`, both render a counter and a timer, the state is saved for the current page and resetted 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

Expand Down
24 changes: 12 additions & 12 deletions examples/with-unstated/components/Clock.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
export default ({
clock: {
state: { lastUpdate, light },
},
}) => {
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={light ? 'light' : ''}>
{format(new Date(lastUpdate))}
<div className={clock.light ? 'light' : ''}>
{format(new Date(clock.lastUpdate))}
<style jsx>{`
div {
padding: 15px;
Expand All @@ -21,8 +26,3 @@ export default ({
</div>
)
}

const format = t =>
`${pad(t.getUTCHours())}:${pad(t.getUTCMinutes())}:${pad(t.getUTCSeconds())}`

const pad = n => (n < 10 ? `0${n}` : n)
26 changes: 16 additions & 10 deletions examples/with-unstated/components/Counter.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
export default ({ counter }) => (
<div>
<h1>
Count: <span>{counter.state.count}</span>
</h1>
<button onClick={() => counter.decrement()}>-1</button>
<button onClick={() => counter.increment()}>+1</button>
<button onClick={() => counter.reset()}>Reset</button>
</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>
)
}
4 changes: 0 additions & 4 deletions examples/with-unstated/components/index.js

This file was deleted.

14 changes: 0 additions & 14 deletions examples/with-unstated/containers/ClockContainer.js

This file was deleted.

28 changes: 0 additions & 28 deletions examples/with-unstated/containers/CounterContainer.js

This file was deleted.

18 changes: 18 additions & 0 deletions examples/with-unstated/containers/clock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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)
13 changes: 13 additions & 0 deletions examples/with-unstated/containers/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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)
4 changes: 0 additions & 4 deletions examples/with-unstated/containers/index.js

This file was deleted.

6 changes: 3 additions & 3 deletions examples/with-unstated/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
},
"dependencies": {
"next": "latest",
"react": "^16.7.0",
"react-dom": "^16.7.0",
"unstated": "^2.1.1"
"react": "16.13.1",
"react-dom": "16.13.1",
"unstated-next": "1.1.0"
}
}
37 changes: 0 additions & 37 deletions examples/with-unstated/pages/_app.js

This file was deleted.

52 changes: 22 additions & 30 deletions examples/with-unstated/pages/about.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
import React from 'react'
import Link from 'next/link'
import { Subscribe } from 'unstated'
import { ClockContainer, CounterContainer } from '../containers'
import { Clock, Counter } from '../components'
import ClockContainer from '../containers/clock'
import CounterContainer from '../containers/counter'
import Clock from '../components/Clock'
import Counter from '../components/Counter'

class About extends React.Component {
componentWillUnmount() {
clearInterval(this.timer)
}
render() {
return (
<Subscribe to={[ClockContainer, CounterContainer]}>
{(clock, counter) => {
this.timer = clock.interval
return (
<div>
<Link href="/index">
<button>go to Index</button>
</Link>
<div>
<Clock clock={clock} />
<Counter counter={counter} />
</div>
</div>
)
}}
</Subscribe>
)
}
export default function Index() {
return (
<CounterContainer.Provider>
<ClockContainer.Provider>
<div>
<Link href="/">
<a>go to Index</a>
</Link>
<br />
<br />
<div>
<Clock />
<Counter />
</div>
</div>
</ClockContainer.Provider>
</CounterContainer.Provider>
)
}

export default About
52 changes: 22 additions & 30 deletions examples/with-unstated/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
import React from 'react'
import Link from 'next/link'
import { Subscribe } from 'unstated'
import { ClockContainer, CounterContainer } from '../containers'
import { Clock, Counter } from '../components'
import ClockContainer from '../containers/clock'
import CounterContainer from '../containers/counter'
import Clock from '../components/Clock'
import Counter from '../components/Counter'

class Index extends React.Component {
componentWillUnmount() {
clearInterval(this.timer)
}
render() {
return (
<Subscribe to={[ClockContainer, CounterContainer]}>
{(clock, counter) => {
this.timer = clock.interval
return (
<div>
<Link href="/about">
<button>go to About</button>
</Link>
<div>
<Clock clock={clock} />
<Counter counter={counter} />
</div>
</div>
)
}}
</Subscribe>
)
}
export default function Index() {
return (
<CounterContainer.Provider>
<ClockContainer.Provider>
<div>
<Link href="/about">
<a>go to About</a>
</Link>
<br />
<br />
<div>
<Clock />
<Counter />
</div>
</div>
</ClockContainer.Provider>
</CounterContainer.Provider>
)
}

export default Index

0 comments on commit 1992e2a

Please sign in to comment.