Created
May 27, 2018 11:06
-
-
Save 4nte/44003df9de1532229a7caa12dcf1a0cb to your computer and use it in GitHub Desktop.
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
// @flow | |
import React from 'react'; | |
import { render } from 'react-dom'; | |
import { Provider, Subscribe, Container } from 'unstated'; | |
type CounterState = { | |
count: number | |
}; | |
class CounterContainer extends Container<CounterState> { | |
state = { | |
count: 0 | |
}; | |
increment() { | |
this.setState({ count: this.state.count + 1 }); | |
} | |
decrement() { | |
this.setState({ count: this.state.count - 1 }); | |
} | |
} | |
function Counter() { | |
return ( | |
<Subscribe to={[CounterContainer]}> | |
{counter => ( | |
<div> | |
<button onClick={() => counter.decrement()}>-</button> | |
<span>{counter.state.count}</span> | |
<button onClick={() => counter.increment()}>+</button> | |
</div> | |
)} | |
</Subscribe> | |
); | |
} | |
render( | |
<Provider> | |
<Counter /> | |
</Provider>, | |
document.getElementById('root') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment