Javascript 对班级行为作出反应时,使用;这";关键词

Javascript 对班级行为作出反应时,使用;这";关键词,javascript,reactjs,react-native,Javascript,Reactjs,React Native,为什么我需要在onChange处理程序中为handleChange添加bind(this),以获得正确的this关键字 类搜索栏扩展React.Component{ 建造师(道具){ 超级(道具); } handleChange(){ console.log(这个) 这个.props.onUserInput( this.refs.filtertextireput.value, this.refs.inStockOnlyInput.checked ); } render(){ 返回( {' '}

为什么我需要在
onChange
处理程序中为
handleChange
添加
bind(this)
,以获得正确的
this
关键字

类搜索栏扩展React.Component{
建造师(道具){
超级(道具);
}
handleChange(){
console.log(这个)
这个.props.onUserInput(
this.refs.filtertextireput.value,
this.refs.inStockOnlyInput.checked
);
}
render(){
返回(

{' '}
只展示库存产品

); } }
这是因为使用新的React
扩展组件
语法,他们在创建组件时删除了自动绑定,就像在旧的
.createClass
语法中一样。默认情况下,使用es2015中的类语法,您必须将其绑定到一个方法,才能使用类this,因此这里也同样适用


您可以在博客文章中阅读React做出这一改变的决定。

然后,这就是未来的发展方向,非常感谢您的回答!