How do you handle nested dynamic routes with React-Router

0 votes
With the help of code and examples can you tell me How do you handle nested dynamic routes with React-Router?
Feb 22 in Node-js by Nidhi
• 11,580 points
76 views

1 answer to this question.

0 votes

Handling Nested Dynamic Routes in React Router (v6)

1. Define Nested Routes in createBrowserRouter

import { createBrowserRouter, RouterProvider } from "react-router-dom";

import Layout from "./Layout";

import UserProfile from "./UserProfile";

import UserPosts from "./UserPosts";

const router = createBrowserRouter([

  {

    path: "/user/:userId",

    element: <Layout />,

    children: [

      { path: "", element: <UserProfile /> }, // Matches `/user/:userId`

      { path: "posts", element: <UserPosts /> }, // Matches `/user/:userId/posts`

    ],

  },

]);

const App = () => <RouterProvider router={router} />;

export default App;

2. Use <Outlet /> to Render Nested Routes in Layout

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

const Layout = () => {

  return (

    <div>

      <h2>User Page</h2>

      <Outlet /> {/* Renders matched child routes */}

    </div>

  );

};

export default Layout;

3. Access Dynamic Params with useParams()

import { useParams } from "react-router-dom";

const UserProfile = () => {

  const { userId } = useParams();

  return <h3>Profile of User {userId}</h3>;

};

export default UserProfile;

answered Feb 24 by Kavya

Related Questions In Node-js

0 votes
1 answer

How do you handle nested routes with complex layouts?

To manage nested routes with complex layouts ...READ MORE

answered Feb 24 in Node-js by Kavya
61 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
66 views
0 votes
1 answer

How do you handle scroll restoration in a React-Router app?

By default, React Router does not restore ...READ MORE

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

How to pass parameters with react-router?

Passing Parameters with React Router 1. Define a ...READ MORE

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

What is the use of Switch in React Router?

In React Router v5, <Switch> is used ...READ MORE

answered Feb 23 in Node-js by Kavya
47 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
76 views
0 votes
1 answer
0 votes
1 answer

How do you structure a scalable Express.js project with multiple route modules?

Implementation Steps: Initialize Express App (app.js) const express = ...READ MORE

answered Feb 25 in Node-js by Navya
55 views
0 votes
1 answer

how to handle error in react native

Handling errors in React Native can be ...READ MORE

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