zoukankan      html  css  js  c++  java
  • [React Router v4] Render Catch-All Routes with the Switch Component

    There are many cases where we will need a catch-all route in our web applications. This can include 404-style routes when nothing is match or other use cases where where we receive an invalid route in React Router v4.

    We can use 'Switch' from 'react-router-dom', what 'Switch' will do is only render the first route that matches.

    To make a fallback Route, all we need to do is define a Route without any path.

    <Route render={() => <h1>Page not found</h1>} />
    import React from 'react';
    import {
      BrowserRouter as Router,
      Route,
      Link,
      Switch,
    } from 'react-router-dom';
    
    
    const Links = () =>
        <nav>
          <Link to="/">Home</Link>
          <Link to="/about">About</Link>
          <Link to="/contact/xxx/xxx/xxxx/x">Contact</Link>
        </nav>
    
    const App = (props) => (
      <Router basename={props.path}>
        <div>
          <Links />
          <Switch>
            <Route exact path="/" render={() => <h1>Home</h1>} />
            <Route path="/about" render={() => <h1>About</h1>} />
            <Route render={() => <h1>Page not found</h1>} />
          </Switch>
        </div>
      </Router>
    )
    
    export default App
  • 相关阅读:
    二 Capacity Scheduler 计算能力调度器
    一:yarn 介绍
    2.hbase原理(未完待续)
    1.安装hbase
    创建第一个vue项目
    初学vue(二)
    第一次面试
    面试题
    C#冒泡排序
    面试真题(.NET/Sqlserver/web前端)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6596293.html
Copyright © 2011-2022 走看看