[英]Accessing state values in React Component
我對reactjs有點陌生。 在React.createclass元素中,您可以訪問輸入值或任何狀態值,例如this
change: function(e) {
this.setState({author: e.target.value});
},
但是在React.component中這是不可能的,所以我如何在React.component中實現類似的任務
謝謝
如果要將方法傳遞給事件處理程序(如onChange={ this.change }
並使用ES2015
類,則必須自己為這些方法設置this
值,例如
class App extends React.Component {
constructor() {
super();
this.state = { author: '' };
this.change = this.change.bind(this); // set this for change method
}
change(e) {
this.setState({ author: e.target.value });
}
render() {
return <input onChange={ this.change } value={ this.state.author } />
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.