Skip to content

Instantly share code, notes, and snippets.

@mbohgard
Created August 4, 2016 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbohgard/1dc66bb7f5fa66d2ed31cb320fcbc7e5 to your computer and use it in GitHub Desktop.
Save mbohgard/1dc66bb7f5fa66d2ed31cb320fcbc7e5 to your computer and use it in GitHub Desktop.
MobX implementation
// todoStore.js
class TodoStore {
@observable todos = []
getTodos = () => {
return this.todos
}
getTodo = id => {
return this.todos.find(todo => todo.id === id)
}
addTodo = text => {
this.todos.push({ id: Date.now(), text })
}
removeTodo = id => {
this.todos = this.todos.filter(todo => todo.id !== id)
}
}
const todoStore = new TodoStore()
export default todoStore
// then in my components I import todoStore and call it's methods
// (instead passing it around through props, I just import it directly)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment