Javascript 无法读取绑定的属性。该属性未在react中定义
如果jsx中有bind(this),那么在构造函数中使用bind是可选的,对吗Javascript 无法读取绑定的属性。该属性未在react中定义,javascript,reactjs,Javascript,Reactjs,如果jsx中有bind(this),那么在构造函数中使用bind是可选的,对吗 render(){ return( <input onChange={this.myFunc.bind(this)} type="text"/> ) } myFunc(){ alert('should trigger'); } render(){ 返回( ) } myFunc(){ 警报(“应触发”); } 但是我得到了无法读取bind属性的错误。这是我的完整js文件
render(){
return(
<input onChange={this.myFunc.bind(this)} type="text"/>
)
}
myFunc(){
alert('should trigger');
}
render(){
返回(
)
}
myFunc(){
警报(“应触发”);
}
但是我得到了无法读取bind属性的错误。这是我的完整js文件
而不是:
<input onChange={myFunc.bind(this)} type="text"/>
而不是:
<input onChange={myFunc.bind(this)} type="text"/>
Abdennour在技术上是正确的,但是每次调用.render()时绑定同一个方法的效率低于在构造函数中绑定一次的效率。许多开发人员认为这种方法是最好的实践:
class MyComponent extends React.Component {
constructor() {
super();
this.myFunc = this.myFunc.bind(this);
}
myFunc(){
alert('should trigger');
}
render() {
return(
<input onChange={this.myFunc} type="text"/>
);
}
}
类MyComponent扩展了React.Component{
构造函数(){
超级();
this.myFunc=this.myFunc.bind(this);
}
myFunc(){
警报(“应触发”);
}
render(){
返回(
);
}
}
Abdennour在技术上是正确的,但是每次调用.render()时绑定同一个方法的效率低于在构造函数中绑定一次的效率。许多开发人员认为这种方法是最好的实践:
class MyComponent extends React.Component {
constructor() {
super();
this.myFunc = this.myFunc.bind(this);
}
myFunc(){
alert('should trigger');
}
render() {
return(
<input onChange={this.myFunc} type="text"/>
);
}
}
类MyComponent扩展了React.Component{
构造函数(){
超级();
this.myFunc=this.myFunc.bind(this);
}
myFunc(){
警报(“应触发”);
}
render(){
返回(
);
}
}
Do
this.myFunc.bind(this)
您的Pastebin链接不起作用。请在此处发布所有相关代码,不要在其他网站上发布。请执行this.myFunc.bind(this)
您的Pastebin链接不起作用。请在这里发布所有相关代码,不要在其他网站上发布。我在文件中发布了这些代码,但仍然有错误。我在文件中发布了这些代码,但仍然有错误。