Simple Async Component for React
npm install @ozylog/async-component
import createAsyncComponent from '@ozylog/async-component';
import DashboardLoaderComponent from './DashboardLoaderComponent';
import {isAuthenticated} from './authApi';
const DashboardContainer = createAsyncComponent({
getComponent: async () => {
let component;
try {
const isAuth = await isAuthenticated();
if (isAuth) {
component = await System.import('./../containers/DashboardContainer');
} else {
component = await System.import('./../components/ErrorForbiddenComponent');
}
} catch (err) {
component = await System.import('./../components/ErrorInternalServerComponent');
}
return component;
},
LoaderComponent: DashboardLoaderComponent
});
export default (
<Route path='/dashboard' render={() => <DashboardContainer />} />
);
MIT