Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Click handler in a React class component throws on this.setState

ErrorTypeError: 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.

  • Applies to React 19, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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
    }
  2. 2Match what you see, then jump to its fix

    1. 1this is undefined only when the handler runs from onClick={this.handleClick}The method is passed as a callback without being boundFix
    2. 2Many handlers, each needing its own bind line, and one was forgottenA shared handler would be simpler as an arrow-function class fieldFix

What does the error look like?

  • TypeError: Cannot read properties of undefined (reading 'setState')
  • Uncaught TypeError: Cannot read property 'setState' of undefined
  • TypeError: 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/*.jsx

2Fix

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 -- Counter

Confirmed by react.dev

Did this fix it?

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.jsx

2Fix

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 start

Confirmed by react.dev

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.