2

Let's say i have two routes: /a/b, /a/b/d,

And i have a customized WrapperRoute, seems it always match to /a/b when i have /a/b/d typed as location. You can try with code-sandbox.

import React from "react";
import { render } from "react-dom";
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";

const routes = [
  {
    path: "/a/b",
    component: () => <div>Component A</div>
  },
  {
    path: "/a/b/d",
    component: () => <div>Component B</div>
  }
];

const App = () => (
  <Router>
    <div>
      <Link to="/a/b">/a/b</Link>
      <br />
      <Link to="/a/b/d">/a/b/d</Link>
      <Switch>
        {routes.map(route => {
          return (
            <WrapperRoute
              key={route.path}
              path={route.path}
              component={route.component}
            />
          );
        })}
      </Switch>
    </div>
  </Router>
);

render(<App />, document.getElementById("root"));

function WrapperRoute({ path, component: Page }) {
  return (
    <Route
      exact
      path={path}
      render={() => {
        return <Page />;
      }}
    />
  );
}

I am wondering why it happens, i have exact props defined

Howard
  • 3,814
  • 5
  • 24
  • 39
  • It should work if this is the exact code. – devserkan May 30 '18 at 05:59
  • Working simple example: https://codesandbox.io/s/0y86knpzm0 – devserkan May 30 '18 at 06:02
  • @devserkan thanks, that's very interesting, i will do more investigation to see if i got something wrong in my code – Howard May 30 '18 at 06:06
  • You are welcome. Probably something is going weird since there is `exact` prop, subpath should not hit the parent. – devserkan May 30 '18 at 06:07
  • @devserkan i added a `WrapperRoute`, seems it caused the issue, but i don't know why, can you take a look at it: https://codesandbox.io/s/2wmpm5olvj, thanks a lot – Howard May 30 '18 at 06:11
  • I can't find and answer why this is so. I'm not so experienced also :) But, apart from your solution it works if you add an `exact: true` key prop to your routes and `` or just `` – devserkan May 30 '18 at 07:23
  • By the way I will follow the question if someone gives a proper answer if your question unlocked again. – devserkan May 30 '18 at 07:24

0 Answers0