2017-03-25 10 views
1

작은 할 일 목록 응용 프로그램에 문제가 있습니다. 내가 오류 Cannot read property 'todos' of undefined"this"는 내 클래스 내에서 정의되지 않습니다

thisaddTodo 내부 바인딩 왜

하지만 removeTodo이 항목을 삭제하려고 할 때마다?

트리거는 여기에 수행됩니다 <button onClick={() => removeTodo(id)}> X </button>

데모 감사

JSfiddle에.

+0

가능한 중복 당신은 또한 그래서 당신은 참조를 잃지 말고 MobX 배열 replace을 사용할 수 있나요? ] (http://stackoverflow.com/questions/34361379/arrow-function-vs-function-declaration-expressions-are-they-equivalent-exch) – Andreas

답변

0

removeTodo을 속성 초기화 된 화살표 함수로 만들거나 action.bound 데코레이터를 사용할 수 있습니다.

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

또는 : 그들은 교환/동일합니다 함수 선언/표현 대 [화살표 기능의

@action.bound 
removeTodo(id) { 
    var filtered = this.todos.filter(todo => todo.id !== id); 
    this.todos.replace(filtered); 
} 
+1

고맙습니다 Tholle 이제 작동 중입니다. 나는 단순히''@ action.bound()''를 내 함수에 추가하고 모든 것이 예상대로 작동한다. –

+0

@jonathandion 좋아요! – Tholle