Do I have to call super.componentDidUpdate in React?

2k Views Asked by At

In a subclass of React.Component, do I have to call super.componentDidUpdate from my componentDidUpdate method? Or is it done automatically?

(I am trying to call it but there is an error message Cannot read property call of undefined)

3

There are 3 best solutions below

0
On BEST ANSWER

You do not, as you can see in the base class that you are extending, here, it has no componentDidUpdate, so calling a super method doesn't make sense.

Instead of always having the method be present, and sometimes have it do nothing, React instead checks if the method exists. That can be see here.

0
On

What are you trying to achieve.

I assume you extended React.Component with ES6 syntax thats why you saying its a subclass. If that's the case, you probably don't need/have to call super.componentDidUpdate at all

0
On

The base React.Component doesn't have a componentDidUpdate method. It just has hooks where it calls componentDidUpdate if the component has that method.

So, the error message that you are seeing means you tried to call a function named call of the object undefined. Cannot read property call of undefined