I'm trying to implement a "lazy-route" to achieve code splitting in my React single page app.
- It's all client side rendered
- I'm using
react-router - Also using
Webpack
I have this component that renders all my routes.
AllRoutes.js
function AllRoutes() {
return(
<Switch>
<Route exact path={"/ROUTE_A"} component={Component_A}/>
<Route exact path={"/ROUTE_B"} component={Component_B}/>
// AND SO ON
</Switch>
);
}
I'm trying to do something like this:
import LazyRoute from './LazyRoute';
function AllRoutes() {
return(
<Switch>
<Route exact path={"/ROUTE_A"} component={Component_A}/>
<Route exact path={"/ROUTE_B"} component={Component_B}/>
<Route exact path={"/ROUTE_C"} component={LazyRoute}/> // THIS SHOULD LAZY LOAD
</Switch>
);
}
And this is what I've tried:
LazyRoute.js
function LazyRoute() {
return import("@pages/Component_C").then(({default: Component_C}) => {
return Component_C;
});
}
export default LazyRoute;
Webpack seems to be doing its part in splitting this LazyRoute bundle:
But I'm getting this error:
I know I'm retuning a Promise by doing return import(). But isn't this the whole point?


React.lazyorLoadable, but I like to do a raw implementation so I can understand it better.React.lazyyet. I'll upvote it, but I'm still looking for the "raw" solution, using only dynamic imports.