Skip to content

Instantly share code, notes, and snippets.

@wickedev
Last active May 22, 2020 03:55
Show Gist options
  • Save wickedev/fcc9e205b44ee8016dfa8a2e16a3800e to your computer and use it in GitHub Desktop.
Save wickedev/fcc9e205b44ee8016dfa8a2e16a3800e to your computer and use it in GitHub Desktop.
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