reactjsreact-routerreact-router-v4react-router-domreact-router-component

React-router-dom: Clicking on Link adds the route to the Url Link indefinitely


After several attempts, I have managed to implement basic nested-routing with React-router-dom.
Here's the simple project structure:
enter image description here

Here are the relevant files:

App.js

import React from "react";
import logo from "./logo.svg";
import "./App.css";
import { BrowserRouter, Switch, Route } from "react-router-dom";

import ParentComponent from "./Components/nestedComponents/ParentComponent";
import NavBar from "./Components/Shared/NavBar";
function App() {
  return (
    <div className="App">
      <BrowserRouter>
        <NavBar />
        <Switch>
          <Route path="/home" name="Home" component={ParentComponent} />
        </Switch>
      </BrowserRouter>
    </div>
  );
}

export default App;

NavBar.js

import React from "react";
import { Link } from "react-router-dom";

export default function NavBar() {
  return (
    <div>
      <Link to={`home/nestedComponentOne`}> Nested Component One </Link>
      <Link to={`home/nestedComponentTwo`}> Nested Component Two </Link>
    </div>
  );
}

ParentComponent.js

import React from "react";
import nestedComponentOne from "./nestedComponentOne";
import nestedComponentTwo from "./nestedComponentTwo";
import { Switch, Route } from "react-router-dom";

export default function ParentComponent() {
  return (
    <div>
      <Switch>
        <Route path="/home/nestedComponentOne" component={nestedComponentOne} />
        <Route path="/home/nestedComponentTwo" component={nestedComponentTwo} />
      </Switch>
    </div>
  );
}

nestedComponentOne.js

import React from "react";

export default function nestedComponentOne() {
  return <div>NESTED COMPONENT 1</div>;
}

nestedComponentTwo.js

import React from "react";

export default function nestedComponentTwo() {
  return <div>NESTED COMPONENT 2</div>;
}

So here's the Result: enter image description here
If I click on nestedComponentOne: enter image description here

If I click on nestedComponentTwo:
enter image description here

The problem is when I click again on nestedComponentOne (or Two) after the I have clicked it the first time, the route gets added to the url string instead of replacing it:
enter image description here


Solution

  • Some update need for your code. Working Demo

    NavBar.js

    Here you forget to add slash / at front to link from root.

          <Link to={`/home/nestedComponentOne`}> Nested Component One </Link>
          <Link to={`/home/nestedComponentTwo`}> Nested Component Two </Link>
    

    ParentComponent.js

    As we removed the Switch from this component, so we need to get the matching information from parent router and pass the path to navigate the corresponding your nested component

         export default function ParentComponent({ match }) {
          return (
            <div> 
              <Route path={`${match.path}/nestedComponentOne`} component={nestedComponentOne} />
              <Route path={`${match.path}/nestedComponentTwo`} component={nestedComponentTwo} />
            </div>
          );
        }