简体   繁体   English

反应TypeError:无法读取未定义的属性'bind'

[英]React TypeError: Cannot read property 'bind' of undefined

        <div className="questions" id="questions">
          {Array(this.state.numQuestions).fill().map((number, questionIdx) => {
            return (
              <div> 
                <label>Question</label>
              <input type="text" onChange={this.handleQuestionChange.bind(questionIdx)}  />
              </div>);
          Array(4).fill().map((number, index) => {
          return <input type="text" key={index} onChange={this.handleAnswerChange.bind(questionIdx)}  />
          })}
          )
          })}
        </div>

I am getting a strange error for the line <input type="text" onChange={this.handleQuestionChange.bind(questionIdx)} /> 我在行<input type="text" onChange={this.handleQuestionChange.bind(questionIdx)} />遇到一个奇怪的错误

I'm not quite sure what the error means let alone how to fix it, here's the error information: 我不太确定该错误意味着什么,更不用说如何解决它了,这是错误信息:

TypeError: Cannot read property 'bind' of undefined TypeError:无法读取未定义的属性“ bind”

Thanks! 谢谢!

It means that this.handleQuestionChange is undefined, so you can't call bind() (or anything else) on it. 这意味着this.handleQuestionChange是未定义的,因此您不能在其上调用bind() (或其他任何方法)。

Check your object / functions and make sure it exists. 检查您的对象/函数并确保它存在。

I prefer the arrow function, it's more elegant. 我更喜欢箭头功能,它更优雅。 Just simply define it like: 只需将其定义为:

 const handleAnswerChange = (index) => { // do anything you like console.log(index) } 

Then you don't have to bind your function. 然后,您不必绑定功能。 It's already worked. 它已经工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM