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 in CSS by kartik
• 11,890 points
110 views

1 answer 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 by Niroj
• 23,950 points

Related Questions In CSS

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 in PHP by Niroj
• 23,950 points
71 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 in Java-Script by Niroj
• 23,950 points
248 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 in PHP by Niroj
• 23,950 points
41 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 in PHP by Niroj
• 23,950 points
44 views
0 votes
1 answer

Uncaught TypeError: Cannot read property 'setState' of undefined

Hello kartik, You dont have to bind anything, ...READ MORE

answered Apr 27 in Java-Script by Niroj
• 23,950 points
76 views
0 votes
1 answer

Error:Uncaught TypeError: Cannot read property 'setState' of undefined

Hello @kartik, This is due to this.delta not being bound ...READ MORE

answered 2 days ago in Angular by Niroj
• 23,950 points
16 views
0 votes
1 answer

Uncaught TypeError: Cannot read property 'msie' of undefined - jQuery tools

Hello, Use the following script tag in your ...READ MORE

answered Apr 28 in JQuery by Niroj
• 23,950 points
89 views
0 votes
1 answer

Uncaught TypeError: Cannot read property 'indexOf' of undefined

Hello @kartik, First of all, you don't need ...READ MORE

answered 4 days ago in Java-Script by Niroj
• 23,950 points
61 views
0 votes
1 answer

Ethereum Node Connection: TypeError: Cannot read property 'version' of undefined

In the code that you are using ...READ MORE

answered Oct 24, 2018 in Blockchain by Omkar
• 69,060 points
446 views
0 votes
1 answer

Cannot read property 'getBlock' of undefined

getBlock is used by web3 and to use it, ...READ MORE

answered Jan 29, 2019 in Blockchain by Omkar
• 69,060 points
114 views