Skip to content

Instantly share code, notes, and snippets.

@djyde
Created March 28, 2017 15:05
Show Gist options
  • Save djyde/cb47277a70c485626eb134f67ce6fb82 to your computer and use it in GitHub Desktop.
Save djyde/cb47277a70c485626eb134f67ce6fb82 to your computer and use it in GitHub Desktop.
Simplest cans counter application
import cans, { inject } from 'cans'
import { observable, action } from 'cans/mobx'
import { BrowserRouter, Route } from 'cans/router'
const app = cans()
// model
app.model({
namespace: 'counter',
observable: observable({
count: 0,
incr: action.bound(function () {
this.count += 1
}),
decr: action.bound(function () {
this.count -= 1
})
})
})
// view
const Counter = inject(({ models })) => {
return (
<div>
<span>{models.counter.count}</span>
<button onClick={models.counter.incr}>+</button>
<button onClick={models.counter.decr}>-</button>
</div>
)
}
// router
const route = () => (
<BrowserRouter>
<Route path='/' component={Counter} />
</BrowserRouter>
)
app.route(route)
// mount the app
app.start(document.querySelector('#root'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment