React Router 在单个页面应用程序中显示多个视图方面起着重要作用。它用于在应用程序中定义多个路由。当用户在浏览器中键入特定 URL 时,如果此 URL 路径与路由器文件中的任何“路由”匹配,则用户将被重定向到该特定路由。因此,我们需要向 React 应用程序添加一个 Router 库,它允许创建多个路由,每个路由都指向一个唯一的视图。
<switch>
<h1>React Router Example</h1>
<Route path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
</switch>