How to use react router lazy for routes?

I want to use the react router lazy api to load the components on route as example here https://reactrouter.com/en/main/route/lazy. How to achieve this is there a way to it? I want this because my bundle size is exceeding the required limit of 500 KB and also I am having a performance issues some times. Any suggestions on code-splitting using dynamic import would also be a great help for me.
7 Replies
kapa.ai
kapa.ai11mo ago
Hi @dipbazz. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, you can use the React.lazy function and Suspense component to achieve lazy loading in your application. This will help you to reduce the bundle size and improve the performance of your application. Here is an example of how you can use React.lazy to lazily load a component:
import React, { Suspense, lazy } from "react";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";

const Home = React.lazy(() => import("./Home"));
const About = React.lazy(() => import("./About"));

const App = () => (
<Router>
<Suspense fallback={<div>Loading...</div>}>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</Suspense>
</Router>
);
import React, { Suspense, lazy } from "react";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";

const Home = React.lazy(() => import("./Home"));
const About = React.lazy(() => import("./About"));

const App = () => (
<Router>
<Suspense fallback={<div>Loading...</div>}>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</Suspense>
</Router>
);
In this example, Home and About components are loaded lazily. They will be broken off into their separate bundles and these bundles will be loaded when any of the page routes attached to the component is loaded. You can also use Suspense to display a loading indicator while the component is being fetched. Here is an example:
<Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</Suspense>
<Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</Suspense>
In this example, "Loading..." will be displayed before the LazyComponent is downloaded completely and rendered. You should also consider using an ErrorBoundary component to catch any loading errors that might occur. Here is an example:
<ErrorBoundary>
<Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</Suspense>
</ErrorBoundary>;
<ErrorBoundary>
<Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</Suspense>
</ErrorBoundary>;
In this example, if there is an error in the component tree it is wrapped in, that component will not break the app rather it will render Something went wrong. in its place. You can find more information about lazy loading in React in this blog post (https://refine.dev/blog/react-lazy-loading/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai11mo ago
<@494653850632978472> kapa.ai is still learning and improving, please let us know how it did by reacting below
extended-salmon
extended-salmon11mo ago
Hello @dipbazz , The response from the bot looks good. One thing I'd like to add is that making lazy routes with refine is no different from any other React application, but I'll open this as a backlog item. It would be a good idea to add an example of this to our repository.
fascinating-indigo
fascinating-indigo11mo ago
Hey @alicanerdurmaz, Thank for your response but can I use the lazy component as described in react-router-v6 from this documentations https://reactrouter.com/en/main/route/lazy
extended-salmon
extended-salmon11mo ago
if you are using refine@4, Yes. it shouldn't be problem
fascinating-indigo
fascinating-indigo11mo ago
Hey @alicanerdurmaz, But when I am using lazy in my Route I get error as
Type '() => Promise<typeof import("/home/dipesh/Gravity/React/kilindar-dashboard/src/pages/dashboard/index")>' is not assignable to type 'LazyRouteFunction<NonIndexRouteObject> | LazyRouteFunction<IndexRouteObject> | undefined'.
Type '() => Promise<typeof import("/home/dipesh/Gravity/React/kilindar-dashboard/src/pages/dashboard/index")>' is not assignable to type 'LazyRouteFunction<NonIndexRouteObject> | LazyRouteFunction<IndexRouteObject> | undefined'.
extended-salmon
extended-salmon11mo ago
I need to debug this. I will when I have time