React - uncaught TypeError: Cannot read property 'setState' of undefined

后端 未结 18 2556
清歌不尽
清歌不尽 2020-11-22 13:35

I am getting the following error

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

even after binding delta in

18条回答
  •  渐次进展
    2020-11-22 14:30

    You dont have to bind anything, Just use Arrow functions like this:

    class Counter extends React.Component {
        constructor(props) {
            super(props);
    
            this.state = {
                count: 1
            };
    
        }
        //ARROW FUNCTION
        delta = () => {
            this.setState({
                count: this.state.count++
            });
        }
    
        render() {
            return (
                

    {this.state.count}

    ); } }

提交回复
热议问题