本文来自于官方文档,属于意译而非直译

  • 基本组件

React Router 有三种类型的组件,分别是:react-router、react-router-dom、react-router-native

你在web 程序中使用了路由组件,那你就应该引入 react-router-dom:

import { BrowserRouter, Route, Link } from 'react-router-dom'
  • 路由

React Router 应用程序的的核心是路由组件。对于 web 项目,react-router-dom 提供了 <BrowserRouter> 和 <HashRouter> 路由。这两种方法都专门创建了 history 对象 。一般来说,

如果你有一个响应请求的服务器,你应该使用  <BrowserRouter> 路由;如果你使用的是静态文件服务器,你应该使用  <HashRouter> 路由。

import { BrowserRouter } from 'react-router-dom'
ReactDOM.render((<BrowserRouter><App/></BrowserRouter>
), holder)

路由组件无法接受两个及两个以上的子元素,看一下我自己写的

ReactDom.render((<HashRouter history={hashHistory}><Switch><Route exact path="/" component={App}/><Route path="/repos" component={Repos}/><Route path="/about" component={About}/></Switch></HashRouter>
),document.getElementById('app'))

在这接受的是一个 switch 子元素。其实还可以这样写:

const RoutersComponent = ()=>(<Switch><Route exact path="/" component={App}/><Route path="/repos" component={Repos}/><Route path="/about" component={About}/></Switch>
)ReactDom.render((<HashRouter history={hashHistory}><RoutersComponent /> </HashRouter>
),document.getElementById('app'))

其实这个就和官方的例子没有什么区别了

  • 路由匹配

这里有两种路由匹配组件:<Route> 和 <Switch>

import { Route, Switch } from 'react-router-dom'

路由匹配是通过比较一个 <Route> 的路径和当前位置的路径名来完成的。当一个 <Route> 匹配时将渲染匹配到的内容,如果不匹配是将不会渲染。一个<Route> 没有 path,那它总是匹配的

// when location = { pathname: '/about' }
<Route path='/about' component={About}/> // renders <About/>
<Route path='/contact' component={Contact}/> // renders null
<Route component={Always}/> // renders <Always/>

你可以在你想要渲染内容的位置包含一个 <Route>,但是将 <Route> 放在一起是很有意义的。用 <Switch> 组件将 <Route> 组合在一起:

<Switch><Route exact path='/' component={Home}/><Route path='/about' component={About}/><Route path='/contact' component={Contact}/>
</Switch>

<Switch> 将 <Route> 组合在一起不是必须的,但是是非常有用的。<Switch> 将迭代所有的子元素 <Route>  并且只渲染当前位置匹配的第一个。当多个路径匹配到相同的路径名时,这是非常有帮助的。当在路径之间进行动画转换时,在确定没有路径匹配到当前位置时(你可以呈现404)

<Switch><Route exact path='/' component={Home}/><Route path='/about' component={About}/><Route path='/contact' component={Contact}/>{/* when none of the above match, <NoMatch> will be rendered */}<Route component={NoMatch}/>
</Switch>

解释一下 “只渲染匹配到的第一个” :(http://localhost:8081/#/repos)

<Switch><Route exact path="/repos" component={App}/><Route path="/repos" component={Repos}/><Route path="/about" component={About}/>
</Switch>

在这里你只可以看到 App 的内容,看不到 Repos 的内容,因为 App 是匹配到的第一个

更多专业前端知识,请上 【猿2048】www.mk2048.com

Raect Router 4 的使用 (1)相关推荐

  1. React router 的 Route 中 component 和 render 属性理解

    React router 的 Route 中 component 和 render 属性理解 Route 标签的三个互斥属性 render.component.children Route 就是用来匹 ...

  2. 几个经常用到的angular路由Router、ActivatedRoute 知识点:嵌套路由、路由跳转、路由传参、路由参数获取

    深度玩家可移步Angular - 常见路由任务 1.嵌套路由 const routes: Routes = [{path: 'first',component: FirstComponent,//同步 ...

  3. this.$router.push、replace、go的区别

    1.this.$router.push() 描述:跳转到不同的url,但这个方法会向history栈添加一个记录,点击后退会返回到上一个页面. 用法: 2.this.$router.replace() ...

  4. router路由react_使用React Router在React中受保护的路由

    router路由react In this video, you will see how to create a protected route using React Router. This r ...

  5. hitchhiker部署_Hitchhiker的React Router v4指南:无限远的递归路径!

    hitchhiker部署 Welcome to the third part of the Hitchhiker's Guide to React Router v4. In this article ...

  6. vue router 入门笔记

    vue router 入门笔记 tips: components优先级大于component,即当一个route对象里同时配置了component和components时component视为无效 即 ...

  7. 四 Vue学习 router学习

    index.js: 按需加载组件: const login = r => require.ensure([], () => r(require('@/page/login')), 'log ...

  8. Express4.x API (四):Router (译)

    Express4.x API 译文 系列文章 Express4.x API (一):application (译) -- 完成 Express4.x API (二):request (译) -- 完成 ...

  9. [React Router v4] Intercept Route Changes

    If a user has entered some input, or the current Route is in a "dirty" state and we want t ...

最新文章

  1. QT制作音乐播放器的相关知识点
  2. Session 和 Cookie 的区别与联系
  3. php 字符串 字典序序排序,C++ 怎么实现字典序排序法,自然排序
  4. 卸载docker后部署k8s后docker无法启动问题
  5. android中将自己的自定义组件打成JAR包
  6. 【ubuntu工具】ubuntu下文件对比软件meld
  7. python价值算法_第十课-Python金融学基础——夏普比率(Sharpe-ratio)和资产组合价值(portfolio-value)...
  8. H5页面input输入框,在ios手机中被顶出页面解决方案
  9. 测试 CS4344 立体声DA转换器
  10. 编程初学者看不懂程序的几点建议
  11. 力扣417题太平洋大西洋水流问题
  12. http2协议翻译(转)
  13. 什么是SEO搜索引擎优化?如何实现?
  14. 晨星评选的2020年获奖基金怎么样?
  15. 人脸识别显示内部服务器显示错误,百度人脸识别——人脸搜索,出现222207错误(未找到匹配用户)怎么解决...
  16. 【概率论与数理统计】猴博士 笔记 p15-16 一、二维连续型求概率
  17. 【2022·合辑】Python量化从入门到精通
  18. 虹科分享 | IOTA网络性能监控 | 如何有效分析VoIP问题
  19. CentOS 6.8 时间同步
  20. 使用java技术将Excel表格内容导入mysql数据库

热门文章

  1. android随机匹配,基于进程调度的Android真随机数和设备特征生成方法的研究
  2. md5与des算法有何不同_Python算法详解:为什么说算法是程序的灵魂?
  3. Django 数据导入和导出
  4. Leetcode 152.乘机最大子序列
  5. C#编程高并发的几种处理方法
  6. uploadify插件的使用
  7. PAT 1065 A+B and C (64bit) (20)
  8. hashCode之二--Java:重写equals()和hashCode()
  9. 红旗系统linux忘了开机密码,红旗Linux6.0中忘记了root密码
  10. cifar-10 图像转为jpg