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

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.
主站蜘蛛池模板: 东至县| 卢湾区| 游戏| 正宁县| 明水县| 婺源县| 广昌县| 怀仁县| 兴宁市| 丹巴县| 奉新县| 五台县| 廊坊市| 潼关县| 项城市| 广西| 全南县| 辽中县| 灵寿县| 泰来县| 安顺市| 叶城县| 阳原县| 夏邑县| 广昌县| 太仓市| 伊川县| 青阳县| 乐昌市| 防城港市| 乐业县| 常德市| 高阳县| 利辛县| 呼图壁县| 册亨县| 高密市| 页游| 杭州市| 乐业县| 海兴县|