-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Updated components and deps * Updated readme
- Loading branch information
Luis Alvarez D
authored
Apr 1, 2020
1 parent
d61eced
commit 1992e2a
Showing
13 changed files
with
108 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |