javascript - State is not defined no-undef ReactJS? - Stack Overflow

So I keep getting the error Line 29:'answers' is not definedno-undef and I am not quite s

So I keep getting the error Line 29: 'answers' is not defined no-undef and I am not quite sure what's going on or how to fix it (I'm very new to React and JS). Could anyone perhaps explain what this means and give me a few pointers? Thanks!

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")
    });
  }

UPDATE: Here's what the new addQuestion function looks like

  addQuestion(){
    questionNum++;
    this.setState({
      answers: this.state.answers.concat("hello")
    });
  }

I call this function when this button is clicked <button onClick={this.addQuestion}>Add Question</button>

Now I am getting this error TypeError: Cannot read property 'setState' of undefined

So I keep getting the error Line 29: 'answers' is not defined no-undef and I am not quite sure what's going on or how to fix it (I'm very new to React and JS). Could anyone perhaps explain what this means and give me a few pointers? Thanks!

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")
    });
  }

UPDATE: Here's what the new addQuestion function looks like

  addQuestion(){
    questionNum++;
    this.setState({
      answers: this.state.answers.concat("hello")
    });
  }

I call this function when this button is clicked <button onClick={this.addQuestion}>Add Question</button>

Now I am getting this error TypeError: Cannot read property 'setState' of undefined

Share Improve this question edited Jul 9, 2018 at 18:53 dorkycam asked Jul 9, 2018 at 18:43 dorkycamdorkycam 5295 gold badges11 silver badges26 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 4

I think perhaps you mean to write it like this, which will bind this to the function with the arrow function:

addQuestion = () => {
  questionNum++;
  this.setState({
    answers: this.state.answers.concat("hello")
  });
}

or

const answers = this.state.answers.concat("hello");
this.setState({ answers });

The way you have it written, answers is undefined, and () => {this.state.answers.concat("hello")} is a callback to setState

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744392292a4571968.html

相关推荐

  • javascript - State is not defined no-undef ReactJS? - Stack Overflow

    So I keep getting the error Line 29:'answers' is not definedno-undef and I am not quite s

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信