Why is super(props) deprecated in the React class component?

Shown below is a perfectly fine (albeit very basic) React class that should be familiar to anyone who has meddled around with ReactJS.

class App extends Component {   
  constructor(props) {     

  render() {
   return <div>Hello, World!</div>

So what is the problem? Well, since recently, users have noticed a deprecated warning for the super(props) usages in TypeScript React classes, which has lead to a few concerned questions all over the internet. So has super(props) really been deprecated?

Turns out, it hasn’t. The deprecation message is actually a small bug in the DefinitelyTyped source code’s overloading constructors which has since been fixed via a patch.

If you are interested in the details: Essentially, the constructor overload here has an optional argument for a Legacy Context. This has been deprecated, and as a result, the overload has been correctly marked as deprecated. However, because the deprecated argument is optional, even the perfectly fine super(props) constructor calls have been affected, erroneously marking the constructor as deprecated.

The suspect code can be seen below.

* @deprecated
* @see https://reactjs.org/docs/legacy-context.html
constructor(props: P, context: any);

Now a fix has already been implemented to have a separate overload for the super(props) constructor. Looks like it has already been merged so this problem should soon be a thing of the past. If you are still interested in the details of the fix, and the discussions related to the same, have a look over here.

Leave a Reply

Your email address will not be published. Required fields are marked *