How can you implement route-based code splitting using React Router and React s lazy and Suspense

0 votes
Can i know How can you implement route-based code splitting using React Router and React's lazy and Suspense?
Apr 21 in Node-js by Ashutosh
• 28,250 points
50 views

1 answer to this question.

0 votes

Route-Based Code Splitting with React Router + React.lazy & Suspense

Step-by-step:

Lazy load components:

import React, { lazy, Suspense } from 'react';

const Home = lazy(() => import('./Home'));

const About = lazy(() => import('./About'));

Wrap routes with Suspense:

import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';

function App() {

  return (

    <Router>

      <Suspense fallback={<div>Loading...</div>}>

        <Routes>

          <Route path="/" element={<Home />} />

          <Route path="/about" element={<About />} />

        </Routes>

      </Suspense>

    </Router>

  );

}

answered Apr 21 by anonymous

Related Questions In Node-js

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How do you implement breadcrumbs in a React-Router app?

Breadcrumbs help users navigate by showing the ...READ MORE

answered Feb 24 in Node-js by Kavya
164 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP