[英]State is not defined no-undef ReactJS?
因此,我不断收到错误Line 29: 'answers' is not defined no-undef
而且我不确定所发生的事情或如何解决(我对React和JS来说是新手)。 谁能解释这意味着什么,并给我一些指导? 谢谢!
class App extends Component {
constructor(props){
super(props);
this.state = {
key: myUUID,
title: "",
author: "",
questions: [],
answers: []
}
}
addQuestion(){
questionNum++;
this.setState({
answers }, () => {
this.state.answers.concat("hello")
});
}
更新:这是新的addQuestion
函数的外观
addQuestion(){
questionNum++;
this.setState({
answers: this.state.answers.concat("hello")
});
}
单击此按钮时,我会调用此函数<button onClick={this.addQuestion}>Add Question</button>
现在我收到此错误TypeError: Cannot read property 'setState' of undefined
我想也许你的意思是把它写像这样,将结合this
与箭头函数的函数:
addQuestion = () => {
questionNum++;
this.setState({
answers: this.state.answers.concat("hello")
});
}
要么
const answers = this.state.answers.concat("hello");
this.setState({ answers });
您编写它的方式, answers
是不确定的,并且() => {this.state.answers.concat("hello")}
是对setState
的回调
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.