bind()方法创建一个新的函数,在bind()被调用时,这个新函数的this被指定为bind() 的第一个参数,而其余参数将作为新函数的参数,供调用时使用。
import React from ‘react’;
class MyComponent extends React. Component {
constructor(props){
super(props);
this. state =

value :""
}
this. handleChange = this. handleChange. bind(this);
// 将"handleChange"方法绑定到"MyComponent” 组件
}
hand1eChange(e){
//do something amazing here
}
render () {
return (
<>
<input type={this. props. type}
value={this. state. value}
onChange={this. handleChange}
/>
</>
)
}
}

Was this helpful?

0 / 0

发表回复 0

Your email address will not be published.