Javascript 反应类型错误:无法读取属性';绑定&x27;未定义的 {Array(this.state.numQuestions).fill().map((number,questionIdx)=>{ 返回( 问题 ); 数组(4).fill().map((数字,索引)=>{ 回来 })} ) })}
我收到一个奇怪的行错误Javascript 反应类型错误:无法读取属性';绑定&x27;未定义的 {Array(this.state.numQuestions).fill().map((number,questionIdx)=>{ 返回( 问题 ); 数组(4).fill().map((数字,索引)=>{ 回来 })} ) })},javascript,reactjs,Javascript,Reactjs,我收到一个奇怪的行错误 我不太清楚错误的含义,更不用说如何修复了,以下是错误信息: TypeError:无法读取未定义的属性“bind” 谢谢 这意味着this.handleQuestionChange未定义,因此不能对其调用bind()(或任何其他内容) 检查对象/函数并确保它存在。我更喜欢箭头函数,它更优雅。 只需简单地定义如下: const handleAnswerChange=(索引)=>{ //你喜欢做什么就做什么 console.log(索引) }哦,好的,可以了!哈哈,我把函数拼
我不太清楚错误的含义,更不用说如何修复了,以下是错误信息:
TypeError:无法读取未定义的属性“bind”
谢谢 这意味着
this.handleQuestionChange
未定义,因此不能对其调用bind()
(或任何其他内容)
检查对象/函数并确保它存在。我更喜欢箭头函数,它更优雅。 只需简单地定义如下:
const handleAnswerChange=(索引)=>{
//你喜欢做什么就做什么
console.log(索引)
}
哦,好的,可以了!哈哈,我把函数拼错了。现在一切都好了,谢谢!组件类上有handleQuestionChange()
方法吗?我只是不小心拼错了,现在一切都好了,谢谢!要使用bind
,第一个参数应该是this
,但该错误表示函数未定义。应该像this.handleQuestionChange.bind(this,questionIdx)
<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>