JSXで単純なハンドラをアタッチしています。 JSXで.bind(this)
を実行しない限り、change
メソッドに入ると、this.setState()
を実行すると、this
はundefined
になります。私は何か間違っているのですか?ハンドラが呼び出されたとき、Reactコンポーネント 'this'は定義されていません。
import React,{Component} from 'react';
import ReactDOM from 'react-dom';
import CounterConstants from './../constants/constants';
import AppDispatcher from './../dispatcher/AppDispatcher';
import CounterActions from './../actions/actions';
class Controls extends Component {
constructor(props) {
super(props);
this.state = {
todoText: ''
};
}
change(event){
this.setState({todoText: event.target.value});
}
submit(event){
if(this.state.todoText !== ''){
CounterActions.addTodo({text: this.state.todoText});
this.setState({todoText: ''});
}
}
render(){
return (
<div className="controls form-inline">
<input type="text" name="todoText" className="form-control" onChange={this.change.bind(this)} value={this.state.todoText} />
<button className="btn btn-primary" onClick={this.submit.bind(this)}>Add a todo</button>
</div>
)
}
}
export default Controls;
クール、もっと欲しい人向け: http://egorsmirnov.me/2015/08/16/react-and-es6-part3.html ありがとう@Elod – user1775718