9

Following is the code that I used to set the state.

handleAddNewQuiz(event){
    this.quiz = new Quiz(this.db, this.newQuizName, function(err, affected, value){
        if(!err){
            this.setState( { quiz : value});  // ERROR: Cannot read property 'setState' of undefined
        }
    });
    event.preventDefault();
};

Even though the database is created successfully, I cannot call this.setState, as it's always undefined.

I tried:

self = this;

handleAddNewQuiz(event){
    this.quiz = new Quiz(this.db, this.newQuizName, function(err, affected, value){
        if(!err){
            self.setState( { quiz : value});  // ERROR: self.setState is not a function
        }
    });
    event.preventDefault();
};

But it still fails, Also tried with a = this, and using a.setState, but still no luck.

How can I solve this?

1
  • use ()=> {} instead using function(){}. Commented Apr 7, 2017 at 10:33

2 Answers 2

20

You need to bind correct this (class context) with callback method, then only you will be able to access the class properties and methods.


Possible Solutions:

1- Use arrow function, like this:

 handleAddNewQuiz(event){
        this.quiz = new Quiz(this.db, this.newQuizName, (err, affected, value) => {
            if(!err){
                this.setState( { quiz : value}); 
            }
        });
        event.preventDefault();
    };

2- Or use .bind(this) with callback method, like this:

handleAddNewQuiz(event){
    this.quiz = new Quiz(this.db, this.newQuizName, function(err, affected, value){
        if(!err){
            this.setState( { quiz : value});  
        }
    }.bind(this));
    event.preventDefault();
};

The way you are using will also work, save the reference of this inside the handleAddNewQuiz method, like this way:

handleAddNewQuiz(event){
    let self = this;    //here save the reference of this
    this.quiz = new Quiz(this.db, this.newQuizName, function(err, affected, value){
        if(!err){
            self.setState( { quiz : value});  
        }
    });
    event.preventDefault();
};
Sign up to request clarification or add additional context in comments.

3 Comments

i would rather pick 1 and 2 variant cuz due to unnecessary usage of one more variable is not so good example
@ddeadlink, i also used to prefer 1st and 2nd way, in 3rd way, i suggested him how to save reference in a third variable like he is using in his ques.
tottaly understand what you were leading to, so i upvoted)
1

Mayank's answer is correct.. Alternatively you can use https://www.npmjs.com/package/core-decorators

and use the @autobind decorator before the function.

1 Comment

Thanks for your suggestion :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.