Uncaught TypeError Cannot read property setState of undefined

0 votes

I am getting the following error

Uncaught TypeError: Cannot read property 'setState' of undefined

even after binding delta in the constructor.

class Counter extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            count : 1
        };

        this.delta.bind(this);
    }

    delta() {
        this.setState({
            count : this.state.count++
        });
    }

    render() {
        return (
            <div>
                <h1>{this.state.count}</h1>
                <button onClick={this.delta}>+</button>
            </div>
        );
    }
}
May 18, 2020 in CSS by kartik
• 37,520 points
14,820 views

2 answers to this question.

0 votes

Hello,

When using ES6 code in React always use arrow functions, because this context is automatically binded.

so use this:

(videos) => {
    this.setState({ videos: videos });
    console.log(this.state.videos);
};

instead of:

function(videos) {
    this.setState({ videos: videos });
    console.log(this.state.videos);
};

Thank you!!

answered May 18, 2020 by Niroj
• 82,840 points
0 votes

Use

this.delta() = this.delta.bind(this);

in place of this.delta.bind(this);
answered Aug 22, 2020 by Abhishek

Related Questions In CSS

0 votes
0 answers

Webpack 4 css modules TypeError: Cannot read property 'context' of undefined

I just upgraded to webpack 4. I ...READ MORE

Jul 20, 2022 in CSS by Edureka
• 13,620 points
1,589 views
0 votes
0 answers

What are good uses of the css `content` property?

Because CSS is for display and not ...READ MORE

Jul 18, 2022 in CSS by Edureka
• 13,620 points
301 views
0 votes
1 answer

CSS shadow white line at the end of linear-gradient background

This is a byproduct of the gradient ...READ MORE

answered Jun 1, 2022 in CSS by Edureka
• 12,690 points
2,754 views
0 votes
1 answer

Why is the CSS border-color inheriting the color property?

An element in CSS can have two ...READ MORE

answered Jun 10, 2022 in CSS by Edureka
• 12,690 points
671 views
0 votes
1 answer

How can I position my div at the bottom of its container?

Bottom and position properties can be used ...READ MORE

answered Jun 10, 2022 in CSS by Edureka
• 12,690 points
11,616 views
0 votes
1 answer

Change color of PNG image via CSS?

The simplest one line that worked for ...READ MORE

answered Jun 1, 2022 in CSS by Edureka
• 12,690 points
6,510 views
0 votes
1 answer

What are the vulnerability related to PHP Form?

Hii, The $_SERVER["PHP_SELF"] variable can be used by ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,840 points
3,186 views
+1 vote
1 answer

How can we send message multiple time to a specific person or group in whatsapp using loop?

Hii @kartik,  This is simple task to send single ...READ MORE

answered Feb 28, 2020 in Java-Script by Niroj
• 82,840 points
18,627 views
0 votes
1 answer

What is meant by passing the variable by value and reference in PHP?

Hello, When the variable is passed as value ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,840 points
3,261 views
0 votes
1 answer

Connection with MySQL server using PHP. How can we do that?

Hey @kartik, You have to provide MySQL hostname, ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,840 points
1,295 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP