Javascript &引用;ReferenceError:getInput未定义";inreact输入元素 类HelloWorldComponent扩展了React.Component{ 构造函数(){ 超级() this.getInput=this.getInput.bind(this) } getInput(){ 警惕(“专注”); } render(){ 报税表( ); } } ReactDOM.render( , document.getElementById('react\u example') );

Javascript &引用;ReferenceError:getInput未定义";inreact输入元素 类HelloWorldComponent扩展了React.Component{ 构造函数(){ 超级() this.getInput=this.getInput.bind(this) } getInput(){ 警惕(“专注”); } render(){ 报税表( ); } } ReactDOM.render( , document.getElementById('react\u example') );,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,这个代码怎么了?无法获取火灾警报,我得到getInput未定义错误 您忘记添加正确的引用。使用this.getInput代替getInput 像这样 class HelloWorldComponent extends React.Component { constructor() { super() this.getInput = this.getInput.bind(this) } getInput() { alert('focused'); } r

这个代码怎么了?无法获取火灾警报,我得到getInput未定义错误


您忘记添加正确的引用。使用this.getInput代替getInput

像这样

class HelloWorldComponent extends React.Component {
  constructor() {
    super()
    this.getInput = this.getInput.bind(this)
  }
  getInput() {
    alert('focused');
  }
  render() {
    return (      
      <input type="text" onFocus={getInput}/>    
    );
  }
}

ReactDOM.render(
  <HelloWorldComponent/>,
  document.getElementById('react_example')
);
类HelloWorldComponent扩展了React.Component{
构造函数(){
超级()
this.getInput=this.getInput.bind(this);
}
getInput(){
警惕(“专注”);
}
render(){
报税表(
);
}
}
ReactDOM.render(
,
document.getElementById('react\u example')
);

您忘记添加正确的引用。使用this.getInput代替getInput

像这样

class HelloWorldComponent extends React.Component {
  constructor() {
    super()
    this.getInput = this.getInput.bind(this)
  }
  getInput() {
    alert('focused');
  }
  render() {
    return (      
      <input type="text" onFocus={getInput}/>    
    );
  }
}

ReactDOM.render(
  <HelloWorldComponent/>,
  document.getElementById('react_example')
);
类HelloWorldComponent扩展了React.Component{
构造函数(){
超级()
this.getInput=this.getInput.bind(this);
}
getInput(){
警惕(“专注”);
}
render(){
报税表(
);
}
}
ReactDOM.render(
,
document.getElementById('react\u example')
);

您应该使用
this.getInput
而不是
getInput


您应该使用
this.getInput
而不是
getInput


使用
this.getInput
调用函数。 您还可以在ES6中使用箭头函数来避免绑定

class HelloWorldComponent extends React.Component {
  constructor() {
    super()
    this.getInput = this.getInput.bind(this);
  }
  getInput() {
    alert('focused');
  }
  render() {
    return (      
      <input type="text" onFocus={this.getInput}/>    
    );
  }
}

ReactDOM.render(
  <HelloWorldComponent/>,
  document.getElementById('react_example')
);
你可以避免

getInput = () => {
    alert('focused');
 }
在构造函数中。这是正确的ES6方法


使用
this.getInput
调用函数。 您还可以在ES6中使用箭头函数来避免绑定

class HelloWorldComponent extends React.Component {
  constructor() {
    super()
    this.getInput = this.getInput.bind(this);
  }
  getInput() {
    alert('focused');
  }
  render() {
    return (      
      <input type="text" onFocus={this.getInput}/>    
    );
  }
}

ReactDOM.render(
  <HelloWorldComponent/>,
  document.getElementById('react_example')
);
你可以避免

getInput = () => {
    alert('focused');
 }
在构造函数中。这是正确的ES6方法


this.getInput=this.getInput.bind(this)
-为什么?@JaromandaX为什么不?听着,我可能错了,但我会认为
this.getInput
不需要绑定到
this
,因为它已经是
this
的属性了。。。我承认,我只能说我99%肯定
这个
是如何工作的,但它看起来像是奇怪的(冗余的)代码me@JaromandaX这是必需的,因为我使用es6语法,请在这里使用您认为应该有效的代码。
this.getInput=this.getInput.bind(this)
-为什么?@JaromandaX为什么不可以?看,我可能错了,但是我认为
this.getInput
不需要绑定到
this
,因为它已经是
this
的属性了。。。我承认,我只能说我99%肯定
这个
是如何工作的,但它看起来像是奇怪的(冗余的)代码me@JaromandaX这是必需的,因为我使用es6语法,请尝试使用您认为应该有效的代码。您的第一个建议不是有效的es6。这是一个实验性的特性,需要额外的配置才能工作。@FelixKling this.getInput用于访问类成员函数。我从未对此提出异议。也许我没有表达清楚。我说的是
getInput=()=>…
。你的第一个建议是无效的ES6。这是一个实验性的特性,需要额外的配置才能工作。@FelixKling this.getInput用于访问类成员函数。我从未对此提出异议。也许我没有表达清楚。我说的是
getInput=()=>…