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 in Node-js by Ashutosh
• 23,230 points
61 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 by Tanmay

Related Questions In Node-js

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
103 views
0 votes
1 answer

How do I parse a data URL in Node?

Hello @kartik, Put the data into a Buffer ...READ MORE

answered Nov 30, 2020 in Node-js by Niroj
• 82,840 points
1,899 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 in Node-js by Tanmay
55 views
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 in Node-js by Kavya
101 views
0 votes
1 answer

How do I create a custom object in react?

Creating a custom popover in React enhances ...READ MORE

answered Dec 31, 2024 in Node-js by Navya
110 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