Skip to content

Instantly share code, notes, and snippets.

@viettranx
Last active April 13, 2018 09:25
Show Gist options
  • Save viettranx/9d98375e62a25595553e3d0147da8228 to your computer and use it in GitHub Desktop.
Save viettranx/9d98375e62a25595553e3d0147da8228 to your computer and use it in GitHub Desktop.
Simple store with MobX
import { observable, action } from 'mobx'
class TaskListStore {
@observable list = [
{ title: 'Go to the office', isFinished: true },
{ title: 'Prepare tasks for today', isFinished: false },
{ title: 'Team meeting', isFinished: false },
{ title: 'Commit tasks changed', isFinished: false }
]
@action finishItem (index) {
const copiedList = this.list.slice()
const isFinished = copiedList[index].isFinished
if (isFinished) return
copiedList[index].isFinished = true
this.list = copiedList // update store by re-assigning
}
@action deleteItem (index) {
this.list = this.list.filter((item, i) => i != index)
}
}
const store = new TaskListStore()
export default store
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment