{"id":143,"hash":"e0120e86055568cd6ca19d4d5f80c9374c87808b08d2c2eb937e68e6941698e3","pattern":"React - uncaught TypeError: Cannot read property &#39;setState&#39; of undefined","full_message":"I am getting the following error\n\n  Uncaught TypeError: Cannot read property 'setState' of undefined\n\neven after binding delta in the constructor.\n\nclass Counter extends React.Component {\n    constructor(props) {\n        super(props);\n\n        this.state = {\n            count : 1\n        };\n\n        this.delta.bind(this);\n    }\n\n    delta() {\n        this.setState({\n            count : this.state.count++\n        });\n    }\n\n    render() {\n        return (\n            <div>\n                <h1>{this.state.count}</h1>\n                <button onClick={this.delta}>+</button>\n            </div>\n        );\n    }\n}","ecosystem":"npm","package_name":"reactjs","package_version":null,"solution":"This is due to this.delta not being bound to this.\n\nOption A: In order to bind set this.delta = this.delta.bind(this) in the constructor:\n\nconstructor(props) {\n    super(props);\n\n    this.state = {\n        count : 1\n    };\n\n    this.delta = this.delta.bind(this);\n}\n\nCurrently, you are calling bind. But bind returns a bound function. You need to set the function to its bound value.\n\nOption B: Use an arrow function:\n\nconstructor(props) {\n    super(props);\n\n    this.state = {\n        count : 1\n    };\n\n}\n\ndelta = ()=> {\n    this.setState({\n        count : this.state.count++\n    });\n}","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/32317154/react-uncaught-typeerror-cannot-read-property-setstate-of-undefined","votes":455,"created_at":"2026-04-19T04:41:23.144328+00:00","updated_at":"2026-04-19T04:51:05.201160+00:00"}