Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Class constructor cannot be invoked without 'new' wrapped in withRouter

I have two React class components:

Class A

import React from 'react';
import { withRouter } from 'react-router-dom';

export class A extends React.Component {

}

export default withRouter(A);

Class B

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import React from 'react';
import { withRouter } from 'react-router-dom';

import A from './A';

export class B extends A {

}

export default withRouter(B);

When i render class B, i get ‘Class constructor cannot be invoked without ‘new” error. When i remove in Class A(parent of Class B) withRouter wrapper – all becomes fine. Why does this happen and what should i do to use exact this structure? Babel version/config? Router version? Anything else? Thank you.

>Solution :

You should inherit from the original component class A and not the wrapped version! For this you can use the static WrappedComponent property.

Corrected code:

export class B extends A.WrappedComponent {

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading