官术网_书友最值得收藏!

The exact prop

In our previous <Route> example, let's change the '/home' route path to '/', as shown here:

<div className="container">
<Route
path="/"
component={HomeComponent}
/>
<Route
path="/dashboard"
component={DashboardComponent}
/>
</div>

With these routes in place, when the browser's URL is set to /dashboard, you'll notice that the content from both components is displayed as follows:

Inside Home route
Inside Dashboard route

Here, the '/' in '/dashboard' matches both of the <Route> paths, '/' and '/dashboard' ; therefore it renders content from both the components. To match the browser's location.pathname exactly with the <Route> component's path, add the exact prop to the <Route>, as shown here:

..
<Route
path="/"
component={HomeComponent}
exact
/>
..

Similarly, when you try to access the '/dashboard' and '/dashboard/portfolio' paths, you'll notice that in both instances, DashboardComponent is rendered. To prevent '/dashboard/portfolio' from matching the <Route> component with the '/dashboard' path, add the exact prop.

React-Router uses the path-to-regexp library internally to determine whether a route element's path prop matches the current location.
主站蜘蛛池模板: 商河县| 进贤县| 衡阳市| 海安县| 天峻县| 阳高县| 泸州市| 察雅县| 潞西市| 论坛| 精河县| 沽源县| 沁源县| 阿拉善右旗| 泗水县| 安图县| 涡阳县| 宜阳县| 武清区| 永善县| 修文县| 炎陵县| 嫩江县| 靖远县| 区。| 元朗区| 隆安县| 二连浩特市| 浑源县| 阳山县| 宁都县| 深水埗区| 瑞昌市| 安福县| 平凉市| 铜陵市| 芮城县| 柳江县| 化州市| 遵义县| 汝城县|