Categories
React Answers

How to fix TypeError: Cannot read property ‘props’ of undefined with React?

Spread the love

Sometimes, we want to fix TypeError: Cannot read property ‘props’ of undefined with React.

In this article, we’ll look at how to fix TypeError: Cannot read property ‘props’ of undefined with React.

How to fix TypeError: Cannot read property ‘props’ of undefined with React?

To fix TypeError: Cannot read property ‘props’ of undefined with React, we should make sure the value of this is correct.

For instance, we write

class Parent extends React.Component {
  state = {
    //...
  };

  onDelete = () => {
    //...
  };

  render() {
    return <Child onDelete={this.onDelete} />;
  }
}

to add the onDelete function which has the value of this set to the Parent component instance since it’s an arrow function.

Conclusion

To fix TypeError: Cannot read property ‘props’ of undefined with React, we should make sure the value of this is correct.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.