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

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.
主站蜘蛛池模板: 通城县| 遂溪县| 丹巴县| 台东市| 陕西省| 青州市| 闸北区| 房山区| 区。| 锦屏县| 商城县| 清苑县| 灯塔市| 策勒县| 台山市| 荣昌县| 曲阜市| 鹰潭市| 淅川县| 通许县| 鹤山市| 鄱阳县| 丹凤县| 页游| 姜堰市| 富锦市| 金山区| 巴彦淖尔市| 云安县| 莱西市| 休宁县| 扎赉特旗| 临沧市| 个旧市| 金川县| 康定县| 金湖县| 容城县| 南华县| 南部县| 渑池县|