Click handler in a React class component throws on this.setState
TypeError: Cannot read properties of undefined (reading 'setState')Short answer
In a class component, a method passed as onClick is called without its instance, so this is undefined and this.setState fails. Bind the method once in the constructor, or define it as an arrow-function class field. Both keep this pointing at the component. Function components with useState avoid the problem.
Fix it fast
1Find out which cause you have
Log this at the top of the handler. If it prints undefined, the method lost its instance and the causes below apply.
handleClick() { console.log(this); // undefined means the method is unbound }2Match what you see, then jump to its fix
What does the error look like?
TypeError: Cannot read properties of undefined (reading 'setState')Uncaught TypeError: Cannot read property 'setState' of undefinedTypeError: Cannot read properties of undefined (reading 'state')
Cause 1 of 2
The method is passed as a callback without being bound
1Check
Look at how the method is declared. A normal method (handleClick() { ... }) is not tied to the instance, and React calls it later as a plain function.
grep -n "onClick={this\." src/*.jsx2Fix
Bind the method in the constructor, as the React reference for class components shows.
class Counter extends Component {
constructor(props) {
super(props);
this.state = { counter: 0 };
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState({ counter: this.state.counter + 1 });
}
}3Verify
Click the button; the counter changes and the console shows no TypeError.
npm test -- CounterConfirmed by react.dev
Cause 2 of 2
A shared handler would be simpler as an arrow-function class field
1Check
Count the bind lines in the constructor against the handlers used in render. A missing one is the failing handler.
grep -n "bind(this)" src/Counter.jsx2Fix
Declare the handler as a class field holding an arrow function. The arrow keeps this tied to the instance, so no bind line is needed. The same page uses this form for its own handlers.
class Counter extends Component {
state = { counter: 0 };
handleClick = () => {
this.setState({ counter: this.state.counter + 1 });
};
render() {
return <button onClick={this.handleClick}>{this.state.counter}</button>;
}
}3Verify
Run the app and click every handler that was failing.
npm startConfirmed by react.dev
How do I stop it coming back?
- Pick one style for the whole project: constructor bind or arrow class fields.
- For new code, write function components and use the useState hook instead of this.setState.
- Use the updater form this.setState(prev => ...) when the new value depends on the old state.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fix