I have the following scenario where I don't know ahead of time which component to load. The solution of which is as follows using React.lazy
import React, { lazy, Suspense } from "react";
export default class CallingLazyComponents extends React.Component {
render() {
var ComponentToLazyLoad = null;
if(this.props.name == "A") {
ComponentToLazyLoad = lazy(() => import("./AComponent"));
} else if(this.props.name == "B") {
ComponentToLazyLoad = lazy(() => import("./BComponent"));
}
return (
<div>
<h1>This is the Base User: {this.state.name}</h1>
<Suspense fallback={<div>Loading...</div>}>
<ComponentToLazyLoad />
</Suspense>
</div>
)
}
}
How can I achieve the same using Loadable Components