Last active
April 13, 2018 09:25
-
-
Save viettranx/9d98375e62a25595553e3d0147da8228 to your computer and use it in GitHub Desktop.
Simple store with MobX
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
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