The React-Router-DOM v5 routers inclusively render all matches, and "/"
matches all routes, and so should be matched and rendered even when navigating to "/details"
.
However, since you are saying that only Users
is rendered when the path is "/details"
I suspect you have a Switch
component involved. The Switch
component exclusively matches routes, specifically it only renders the first matching Route
or Redirect
component. Sure, you can pass the exact
prop for the "/"
route so it's only matched when the URL path is exactly "/"
, but the more correct solution here is to order the routes correctly so there's no need to sprinkle the exact
prop all over your routes.
Within the Switch
component you should order the routes inversely by path specificity. In other words, more specific paths before less specific paths. The "/"
route should be the last route specified since it's the least specific path other than the "*"
wildcard "catch all". When ordered correctly there's virtually no need at all for the exact
prop.
import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import Users from "./components/Users";
import Details from "./components/Details";
ReactDOM.render((
<BrowserRouter>
<Switch>
{/* more specific routes like "/details/all", "/details/:id", etc */}
<Route path="/details" component={Details} />
<Route path="/" component={Users} />
</Switch>
</BrowserRouter>
), document.getElementById('app'));
If you happen to have a "catch-all" route for anything that isn't matched, including the home page route, it would come last and you'd actually use exact
on the home page route to differentiate it from the "catch-all".
Example:
<BrowserRouter>
<Switch>
<Route path="/details" component={Details} />
<Route exact path="/" component={Users} />
<Route path="*" component={NotFound} />
</Switch>
</BrowserRouter>
npm install -S 'prop-types'
and replacing React.PropsTypes with PropTypes . Also update your package.json to make use of the latest packaged since other packages may still be using React.PropTypes and React.createClass – Hatbox