Last active
May 22, 2020 03:55
-
-
Save wickedev/fcc9e205b44ee8016dfa8a2e16a3800e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { action, observable } from 'mobx' | |
import { observer } from 'mobx-react' | |
import React, { useContext } from 'react' | |
class CounterViewModel { | |
@observable count: number = 0 | |
@action.bound public plus() { | |
this.count++ | |
} | |
@action.bound public minus() { | |
this.count-- | |
} | |
} | |
const CounterViewModelContext = React.createContext(new CounterViewModel()) | |
export function App() { | |
return ( | |
<Counter /> | |
) | |
} | |
const Counter = observer(() => { | |
const counter = useContext(CounterViewModelContext) | |
return ( | |
<div> | |
<div>{counter.count}</div> | |
<button onClick={counter.minus}>-</button> | |
<button onClick={counter.plus}>+</button> | |
</div> | |
) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment