Maybe there is a better way, but I try to make my React components as reusable as possible. So I am using a bootstrap Card and inside this Card I wanna place different components from outside (with props) dynamically. Without props it works fine. But with props I got an error message "Error: Cannot find module '../../pages/Dummy'".
This works perfect:
import React, {Suspense} from 'react';
import { MDBCard, MDBCardBody } from "mdbreact";
const Area = (props) => {
const OtherComponent = React.lazy(() => import('../../pages/Dummy'));
return (
<MDBCard className="text-center">
<MDBCardBody>
<Suspense fallback={<div>Loading...</div>}>
<OtherComponent/>
</Suspense>
</MDBCardBody>
</MDBCard>
);
};
export default Area;
This doesn't work:
import React, {Suspense} from 'react';
import { MDBCard, MDBCardBody } from "mdbreact";
const Area = (props) => {
const OtherComponent = React.lazy(() => import(props.compName));
return (
<MDBCard className="text-center">
<MDBCardBody>
<Suspense fallback={<div>Loading...</div>}>
<OtherComponent/>
</Suspense>
</MDBCardBody>
</MDBCard>
);
};
export default Area;
Call from outside:
<Area compName='../../pages/Dummy'/>
It's like I cannot lazy-load with props. Very strange.
Btw. I don't need to use lazy-loading if there is an easier way to use sub-components dynamically. I just thought this is the only way.