Skip to content

Commit 1992e2a

Browse files
author
Luis Alvarez D
authored
Example/update unstated (#11534)
* Updated components and deps * Updated readme
1 parent d61eced commit 1992e2a

File tree

13 files changed

+108
-174
lines changed

13 files changed

+108
-174
lines changed

examples/with-unstated/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Unstated example
22

3-
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).
3+
This example shows how to integrate [Unstated Next](https://github.com/jamiebuilds/unstated-next) with Next.js.
44

5-
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.
5+
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.
66

77
## Deploy your own
88

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
export default ({
2-
clock: {
3-
state: { lastUpdate, light },
4-
},
5-
}) => {
1+
import ClockContainer from '../containers/clock'
2+
3+
const pad = n => (n < 10 ? `0${n}` : n)
4+
5+
const format = t =>
6+
`${pad(t.getUTCHours())}:${pad(t.getUTCMinutes())}:${pad(t.getUTCSeconds())}`
7+
8+
export default function Clock() {
9+
const clock = ClockContainer.useContainer()
10+
611
return (
7-
<div className={light ? 'light' : ''}>
8-
{format(new Date(lastUpdate))}
12+
<div className={clock.light ? 'light' : ''}>
13+
{format(new Date(clock.lastUpdate))}
914
<style jsx>{`
1015
div {
1116
padding: 15px;
@@ -21,8 +26,3 @@ export default ({
2126
</div>
2227
)
2328
}
24-
25-
const format = t =>
26-
`${pad(t.getUTCHours())}:${pad(t.getUTCMinutes())}:${pad(t.getUTCSeconds())}`
27-
28-
const pad = n => (n < 10 ? `0${n}` : n)
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
export default ({ counter }) => (
2-
<div>
3-
<h1>
4-
Count: <span>{counter.state.count}</span>
5-
</h1>
6-
<button onClick={() => counter.decrement()}>-1</button>
7-
<button onClick={() => counter.increment()}>+1</button>
8-
<button onClick={() => counter.reset()}>Reset</button>
9-
</div>
10-
)
1+
import CounterContainer from '../containers/counter'
2+
3+
export default function Counter() {
4+
const counter = CounterContainer.useContainer()
5+
6+
return (
7+
<div>
8+
<h1>
9+
Count: <span>{counter.count}</span>
10+
</h1>
11+
<button onClick={() => counter.decrement()}>-1</button>
12+
<button onClick={() => counter.increment()}>+1</button>
13+
<button onClick={() => counter.reset()}>Reset</button>
14+
</div>
15+
)
16+
}

examples/with-unstated/components/index.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

examples/with-unstated/containers/ClockContainer.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

examples/with-unstated/containers/CounterContainer.js

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useState, useEffect } from 'react'
2+
import { createContainer } from 'unstated-next'
3+
4+
function useClock() {
5+
const [data, setData] = useState({ lastUpdate: 0, light: false })
6+
7+
useEffect(() => {
8+
let interval = setInterval(() => {
9+
setData({ lastUpdate: Date.now(), light: !data.light })
10+
}, 1000)
11+
12+
return () => clearInterval(interval)
13+
}, [data.light])
14+
15+
return data
16+
}
17+
18+
export default createContainer(useClock)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useState } from 'react'
2+
import { createContainer } from 'unstated-next'
3+
4+
function useCounter() {
5+
const [count, setCount] = useState(0)
6+
const decrement = () => setCount(count - 1)
7+
const increment = () => setCount(count + 1)
8+
const reset = () => setCount(0)
9+
10+
return { count, decrement, increment, reset }
11+
}
12+
13+
export default createContainer(useCounter)

examples/with-unstated/containers/index.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

examples/with-unstated/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
},
99
"dependencies": {
1010
"next": "latest",
11-
"react": "^16.7.0",
12-
"react-dom": "^16.7.0",
13-
"unstated": "^2.1.1"
11+
"react": "16.13.1",
12+
"react-dom": "16.13.1",
13+
"unstated-next": "1.1.0"
1414
}
1515
}

0 commit comments

Comments
 (0)