How do I implement nested routes in React Router and pass data between routes effectively

0 votes
Can you tell me How do I implement nested routes in React Router and pass data between routes effectively?
Mar 11, 2025 in Node-js by Ashutosh
• 33,350 points
674 views

1 answer to this question.

0 votes

To implement nested routes in React Router and pass data between routes effectively, follow these steps:

1. Set Up Nested Routes

Use the Outlet component from react-router-dom to render nested routes within a parent route.

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

function App() {

  return (

    <BrowserRouter>

      <Routes>

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

          <Route index element={<Home />} />

          <Route path="parent" element={<Parent />}>

            <Route path="child" element={<Child />} />

          </Route>

        </Route>

      </Routes>

    </BrowserRouter>

  );

}

function Layout() {

  return (

    <div>

      <h1>Layout</h1>

      <Outlet /> {/* Nested routes render here */}

    </div>

  );

}

function Home() {

  return <h2>Home</h2>;

}

function Parent() {

  return (

    <div>

      <h2>Parent</h2>

      <Outlet /> {/* Child route renders here */}

    </div>

  );

}

function Child() {

  return <h3>Child</h3>;

}

2. Pass Data Between Routes

You can pass data between routes using:

a. URL Parameters

Use useParams to access dynamic segments in the URL.

<Route path="parent/:id" element={<Parent />} />

function Parent() {

  const { id } = useParams();

  return <h2>Parent ID: {id}</h2>;

}

b. State via useNavigate or Link

Pass state when navigating using useNavigate or <Link>.

import { Link, useLocation } from 'react-router-dom';

function Parent() {

  const navigate = useNavigate();

  const data = { message: "Hello from Parent" };

 return (

    <div>

      <button onClick={() => navigate('child', { state: data })}>

        Go to Child

      </button>

      <Link to="child" state={data}>Go to Child</Link>

    </div>

  );

}

function Child() {

  const location = useLocation();

  const { message } = location.state || {};


  return <h3>Child: {message}</h3>;

}

c. Context API or State Management

For complex data sharing, use React Context or state management libraries like Redux.

3. Access Data in Nested Routes

Use useParams, useLocation, or context to access data in nested routes.

function Child() {

  const { id } = useParams();

  const location = useLocation();

  const { message } = location.state || {};

  return (

    <div>

      <h3>Child ID: {id}</h3>

      <p>Message: {message}</p>

    </div>

  );

}

answered Mar 11, 2025 by Tanmay

Related Questions In Node-js

0 votes
1 answer

How do you pass and use URL parameters in a React-Router route?

Passing and Using URL Parameters in React ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
521 views
0 votes
1 answer

How can you implement nested routes in a React application using React Router?

To implement nested routes in a React ...READ MORE

answered Apr 17, 2025 in Node-js by anonymous
598 views
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, 2025 in Node-js by Kavya
592 views
0 votes
1 answer
0 votes
1 answer

What is the difference between Node.js and Express.js?

Feature Node.js Express.js Definition A runtime environment for executing JavaScript outside ...READ MORE

answered Mar 11, 2025 in Node-js by Tanmay
659 views
0 votes
1 answer
0 votes
1 answer
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, 2025 in Node-js by Kavya
736 views
0 votes
1 answer
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