How do you handle nested routes with complex layouts

0 votes
Can i know How do you handle nested routes with complex layouts?
Feb 23 in Node-js by Ashutosh
• 19,190 points
59 views

1 answer to this question.

0 votes

To manage nested routes with complex layouts in React Router, follow these steps:

1. Define a Layout Component

Create a shared layout with a common UI (e.g., navbar, sidebar).

import { Outlet, Link } from "react-router-dom";

const DashboardLayout = () => {

  return (

    <div>

      <nav>

        <Link to="stats">Stats</Link> | <Link to="settings">Settings</Link>

      </nav>

      <main>

        <Outlet /> {/* Renders nested routes */}

      </main>

    </div>

  );

};

export default DashboardLayout;

2. Define Nested Routes in App.js

Use <Outlet /> in the layout to render child components.

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

import DashboardLayout from "./DashboardLayout";

import Stats from "./Stats";

import Settings from "./Settings";

import Home from "./Home";

function App() {

  return (

    <Router>

      <Routes>

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

        {/* Nested Routes inside DashboardLayout */}

        <Route path="dashboard" element={<DashboardLayout />}>

          <Route path="stats" element={<Stats />} />

          <Route path="settings" element={<Settings />} />

        </Route>

      </Routes>

    </Router>

  );

}

export default App;

3. Create Nested Components

Components for Stats and Settings.

const Stats = () => <h2>Dashboard Stats</h2>;

export default Stats;


const Settings = () => <h2>Dashboard Settings</h2>;

export default Settings;

answered Feb 24 by Kavya

Related Questions In Node-js

0 votes
1 answer

How do you handle nested dynamic routes with React-Router?

Handling Nested Dynamic Routes in React Router ...READ MORE

answered Feb 24 in Node-js by Kavya
73 views
0 votes
1 answer

How do you model a many-to-many relationship in MongoDB with an example?

In MongoDB, a many-to-many relationship can be ...READ MORE

answered Feb 23 in Node-js by Kavya
59 views
0 votes
1 answer

How do you handle concerns for write operations in MongoDB?

Write operations in MongoDB need to be ...READ MORE

answered Feb 23 in Node-js by Kavya
90 views
0 votes
1 answer

How do you create protected routes in React?

Creating Protected Routes in React (React Router ...READ MORE

answered Feb 23 in Node-js by Kavya
64 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How do you handle a large amount of data in React?

Handling Large Amounts of Data in React To ...READ MORE

answered Feb 24 in Node-js by Kavya
58 views
0 votes
1 answer

How do you integrate authentication with React-Router?

To integrate authentication with React Router, follow ...READ MORE

answered Feb 24 in Node-js by Kavya
63 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