In the When to avoid lazy components section, you saw where to avoid making components lazy when there is no benefit in doing so. The same pattern can be applied when you're using react-router as the mechanism to navigate around your application. Let's take a look at an example. Here are the imports we'll need:
import React, { Suspense, lazy } from "react";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
import { FadeLoader } from "react-spinners";
Next, we'll create our lazy components:
const First = lazy(() =>
Promise.all([
import("./First"),
new Promise(resolve => {
setTimeout(() => {
resolve();
}, 3000);
})
]).then(([m]) => m)
);
const Second = lazy(() =>
Promise.all([
import("./Second"),
new Promise(resolve => {
setTimeout(() => {
resolve();
}, 3000);
})
]).then(([m]) => m)
);
Finally, we have...