There is a new concept of code splitting and async routing using suspense and lazy introduced by react itself. With that concept, how can we show the progress bar at the top of the page when the route is changed. I could show the loading icon, text etc but not the progress bar(0 to 100%). Here is how i have done
const About = lazyLoading(() => import("./components/About"), {
fallback: <h1>Loading...</h1>
});
const Home = lazyLoading(() => import("./components/Home"), {
fallback: <h1>Loading...</h1>
});
const BasicExample = () => {
return (
<Router>
<div>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
</ul>
<hr />
<Route exact path="/" render={() => <Home name="hello" />} />
<Route path="/about" component={About} />
</div>
</Router>
);
};
render(<BasicExample />, document.getElementById("root"));
lazyloading.js
const lazyloading = (importFunc, { fallback = null }) => {
const LazyComponent = lazy(importFunc);
return props => (
<Suspense fallback={fallback}>
<LazyComponent {...props} />
</Suspense>
);
};
lazyloading.defaultProps = {
fallback: null
};
export default lazyloading;
Here is the example in codesandbox either where i have progress component too but did not have idea on how i implement it when using suspense and lazy in the way I am doing