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
  • 相关阅读:
    Redis 主从复制
    LESSON THREE
    SSIS OLEDB COMMAND RULES
    Hadoop step by step _ install and configuration environment
    repcached的安装练习
    Check list
    简单对象定位——xpath定位
    简单对象定位
    Python webdriver API- 浏览器的操作
    第一个自动化脚本示例
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6596293.html
Copyright © 2011-2022 走看看