JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > React >

React fixes the Uncaught Error: You cannot render a Router inside another Router error

Author:JIYIK Last Updated:2025/03/16 Views:

When we have 2 router components in the same React application, we will get the error “You cannot render a <Router>inside another <Router>“. To fix this error, you need to make sure you use the router in your index.js file and do not use any nested router components.

React You cannot render a Router inside another Router

Make sure the App component in the index.js file is included in the Router component.

import {createRoot} from 'react-dom/client';
import App from './App';
import {BrowserRouter as Router} from 'react-router-dom';

const rootElement = document.getElementById('root');
const root = createRoot(rootElement);

// 👇️ wrap App in Router

root.render(
  <Router>
    <App />
  </Router>
);

RouterAnd remove any nested or BrowserRoutercomponents from our App .

import React from 'react';
import {Route, Link, Routes} from 'react-router-dom';

// 👇️ 不要在 <Router> 进行封装
export default function App() {
  return (
    <div>
      <div>
        <nav>
          <ul>
            <li>
              <Link to="/">Home</Link>
            </li>
            <li>
              <Link to="/about">About</Link>
            </li>
          </ul>
        </nav>

        <Routes>
          <Route path="/about" element={<About />} />
          <Route path="/" element={<Home />} />
        </Routes>
      </div>
    </div>
  );
}

function Home() {
  return <h2>Home</h2>;
}

function About() {
  return <h2>About</h2>;
}

Your entire React application should be wrapped in just one Router (or BrowserRouter) component.

The best place to wrap your React application with a Router component is in the index.js file, since this is the entry point for your React application.

Once our entire application is wrapped in a Router component, we can use any hook from the react router package anywhere in the component.

If you cannot find where the Router component is used, you can use the IDE to find <Router>where it is used.

In VSCode, we can use the shortcut CTRL + Shift + fSearch <Router>and find the nested instance of the Router (or BrowserRouter) component that must be deleted.

We should not have more than one router in our React application.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

React Tutorial - Transferring Props

Publish Date:2025/03/16 Views:185 Category:React

React transfers Props. Props are generated when components are encapsulated. Components expose some properties (Props) to the outside world to complete some functions.

React Tutorial: Props Anti-Pattern

Publish Date:2025/03/16 Views:183 Category:React

React's Props anti-pattern, using Props to generate state in getInitialState is an anti-pattern - Anti-Pattern.

React Tutorial - Props Validation

Publish Date:2025/03/16 Views:99 Category:React

Props validation is a very useful way to use components correctly. It can avoid many bugs and problems as your application becomes more and more complex. In addition, it can make your program more readable.

Why do you need to bind event handlers in React Class Components?

Publish Date:2025/03/16 Views:58 Category:React

When using React, we must have come across control components and event handlers. We need to use `.bind()` in the constructor of the custom component to bind these methods to the component instance. As shown in the following code:

Solution to the error "does not contain a default export" in React

Publish Date:2025/03/16 Views:187 Category:React

When we try to use `default import` to import from a module that does not have a `default export`, we get a "does not contain a default export" error. To fix the error, make sure the module has named exports and wrap the import in curly braces, e.g.

Solve the Module not found: Can't resolve 'react-bootstrap' error

Publish Date:2025/03/16 Views:85 Category:React

To resolve the error "Module not found: Error: Can't resolve 'react-bootstrap'", make sure to install the react-bootstrap package by opening a terminal in the root directory of the project and running the command `npm install react-bootstrap bootstrap

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial