1

There is a problem with my small todo app. Every time I try to delete an item there is an error Cannot read property 'todos' of undefined

Why this is bound inside addTodo but not removeTodo ?

The trigger is done here: <button onClick={() => removeTodo(id)}> X </button>

Demo on JSfiddle

Thanks.

Jonathan Dion
  • 1,651
  • 11
  • 16
  • Possible duplicate of [Arrow function vs function declaration / expressions: Are they equivalent / exchangeable?](http://stackoverflow.com/questions/34361379/arrow-function-vs-function-declaration-expressions-are-they-equivalent-exch) – Andreas Mar 25 '17 at 16:30

1 Answers1

0

You could make removeTodo into a property initialized arrow function or use the action.bound decorator. You also have to use MobX array replace so you don't lose the reference:

removeTodo = (id) => {
  var filtered = this.todos.filter(todo => todo.id !== id);
  this.todos.replace(filtered);
}

Or:

@action.bound
removeTodo(id) {
  var filtered = this.todos.filter(todo => todo.id !== id);
  this.todos.replace(filtered);
}
Tholle
  • 108,070
  • 19
  • 198
  • 189