[React Router v4] Create Basic Routes with the React Router v4 BrowserRouter
React Router 4 has several routers built in for different purposes. The primary one you will use for building web applications is the BrowserRouter. In this lesson you will import the BrowserRouter and create some basic Route components.
After create your app with 'creat-react-app', we going to install the react-router-dom:
npm i -D react-router-dom@next
Import BrowserRouter:
import {
BrowserRouter as Router,
Route
} from 'react-router-dom';
Using Router:
<Router> <div> <Route exact path="/" component={App}></Route> <Route path="/about" component={About}></Route> <Route strict path="/about/" render={() => <h2>About render</h2>}></Route> <Route path="/demo" children={({match}) => match && <h2>demo</h2>}></Route> </div> </Router>
Here we use three different ways to render a component or Html to the DOM:
1. Using Component:
<Route exact path="/" component={App}></Route>
<Route path="/about" component={About}></Route>
Here the 'exact' flag tells that it should match only '/'.
2. Using render:
we can just render some html:
<Route strict path="/about/" render={() => <h2>About render</h2>}></Route>
3. Using children:
<Route path="/demo" children={({match}) => match && <h2>demo</h2>}></Route>
By default what we write into 'children' will be rendered no matter which path it matchs.
If for example, we only want it to be shown when match '/demo', we can check whether there is a 'match' object exists on props.
相关文章
- React Error: Failed at the xxx@0.1.0 start script 'react-scripts start'.
- react全家桶包括哪些_react 自定义组件
- React.createElement和ReactDom.render方法简单思路
- react hooks 生命周期渲染时机简述
- react源码解析1.开篇介绍和面试题_2023-02-27
- 一道React面试题把我整懵了_2023-02-28
- react源码分析:深度理解React.Context_2023-02-28
- react源码分析之协调与调度
- react:组件的生命周期、父子组件的生命周期
- 高级前端react面试题总结
- React中组件通信方式
- react event事件订阅传值
- 彻底搞懂 React 18 并发机制的原理
- React和Vue谁会淘汰谁?
- 前端二面高频react面试题集锦_2023-02-23
- react 基础之组件篇二——Style in React
- 4k字介绍 React Router 6.4 超大变化:引入 Data API。你不纯粹了!
- ORA-38408: The ADT “string” does not exist in the current schema. ORACLE 报错 故障修复 远程处理
- ORA-46014: The value of the “aclFile” element is too long. ORACLE 报错 故障修复 远程处理
- ORA-48408: The incident number exceeds the maximum number [string] ORACLE 报错 故障修复 远程处理
- ORA-48411: The trace files exceeds the maximum number [string] ORACLE 报错 故障修复 远程处理
- ORA-48928: The predicate exceeds the max limit string ORACLE 报错 故障修复 远程处理
- ORA-53410: The attribute string does not conform to the VM rule. ORACLE 报错 故障修复 远程处理
- ORA-13619: The procedure argument string is greater than the maximum allowable length of string characters. ORACLE 报错 故障修复 远程处理
- ORA-13638: The user interrupted the current operation. ORACLE 报错 故障修复 远程处理
- ORA-13703: The snapshot pair [string, string] for database_id string and instance_id string are not found in the current repository. ORACLE 报错 故障修复 远程处理
- Exploring the Power of Linux: The Versatility and Potential of Aud Files(linux系统aud文件)
- Discover the Versatility of Tilda Linux: The Perfect Operating System for Tech Enthusiasts.(tildalinux)
- Exploring the Power of as86 Linux: The Ultimate Guide for Beginners!(as86linux)