What’s React-Router

React Router 是一个基于 React 之上的强大路由库,它可以让你向应用中快速地添加视图和数据流,同时保持页面与 URL 间的同步。

前言

继续上一篇文章的延续<<React教程之create-react-app+webpack打包运行项目(react-webpack-starter)>>
https://blog.csdn.net/moshowgame/article/details/91984076

官方文档
https://react-guide.github.io/react-router-cn/docs/Introduction.html

开源项目
https://github.com/moshowgame/react-study

npm install

执行以下安装命令

npm install --save react-route react-router-dom

确认package.json里面有以下依赖,且大版本号基本一致(版本相差过大)

  "dependencies": {"react": "^16.8.6","react-dom": "^16.8.6","react-route": "^1.0.3","react-router-dom": "^5.0.1","react-scripts": "3.0.1"},

目录结构

基本核心代码都放在src下的components目录,首页index.html无需改变,保证有个rootdiv即可,再index.js中通过ReactDOM.render(<App />, document.getElementById('root'));来进行渲染


index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';ReactDOM.render(<App />, document.getElementById('root'));

App.js

这里引发了一个HashRouter vs BrowserRouter 的问题,请见文分解

import React from 'react';
//HashRouter,支持#识别,http://localhost:8080/#/Page3/
import { HashRouter as Router,Route} from 'react-router-dom';
//单页BrowserRouter
//import { BrowserRouter as Router,Route} from 'react-router-dom';
import Home from './home';
import Page1 from './page1';
import Page2 from './page2';
import Page3 from './page3';class App extends React.Component {render(){return(<Router ><div><Route exact path="/" component={Home} /><Route path="/Page1/:name" component={Page1} />                                                                                                                                                                                                                                                                                   <Route path="/Page2" component={Page2} />  <Route path="/Page3" component={Page3} />                                                                                                                                                                                                                                                                                                                                                                                                                                          </div></Router>)}}
export default App;

Home.js

import React from 'react';
import { Link } from 'react-router-dom';
import { Router, Route, Switch } from "react-router";class Home extends React.Component{render(){return(<div><p>This is Home!</p><Link to="/Page1/Moshow" style={{color:'black'}}><div>点击跳转到Page1</div></Link><Link to="/Page2/" style={{color:'black'}}><div>点击跳转到Page2</div></Link><Link to="/Page3/" style={{color:'black'}}><div>点击跳转到Page3</div></Link></div>);}
}export default Home;

Page1.js

import React from 'react';
import App from './App';class Page1 extends React.Component {render() {let mystyle = {width: '100%',height: '80px',backgroundColor: 'pink',fontSize: '24px',textAlign: 'center',//垂直居中lineHeight: '80px'//水平居中};console.log(this, this.props);return (<div><h1 style={mystyle}>This is Page 111111! your staff-id :{this.props.match.params.name}</h1></div>); }
}export default Page1;

Page2.js

import React from 'react';class Page2 extends React.Component{render(){let mystyle={width: '100%',height:'80px',backgroundColor:'greenyellow',fontSize:'24px',textAlign: 'center',//垂直居中lineHeight: '80px'//水平居中};return(<div><h1 style={mystyle}>This is Page 222222!</h1></div>);}
}export default Page2;

Page3.js

import React from 'react';const Page3 = ({match}) => {let mystyle={width: '100%',height:'80px',backgroundColor:'burlywood',fontSize:'34px',textAlign: 'center',//垂直居中lineHeight: '80px'//水平居中};return(<div><h1 style={mystyle}>hello {match.params.id} This is Page 333333!</h1></div>);
}export default Page3;

npm start

现在让我们开始运行项目把.输入

npm start

运行效果如下

http://localhost:8080/#

http://localhost:8080/#/Page1/:yourname

http://localhost:8080/#/Page2/
http://localhost:8080/#/Page3/

以下内容为补充部分 : BrowserRouter vs HashRouter

BrowserRouter

It uses history API, i.e. it’s unavailable for legacy browsers (IE 9 and lower and contemporaries). Client-side React application is able to maintain clean routes like example.com/react/route but needs to be backed by web server. Usually this means that web server should be configured for single-page application, i.e. same index.html is served for /react/route path or any other route on server side. On client side, window.location.pathname is parsed by React router. React router renders a component that it was configured to render for /react/route.

它使用浏览历史history API,所以他不适用于传统浏览器(IE 9及更低版本)。 客户端的React应用程序应该能够维护像example.com/react/route这样的干净路由,但需要由Web服务器支持。 通常这意味着Web服务器应该配置为单页面应用程序,即为/ react / route路径或服务器端的任何其他路由提供相同的index.html。 在客户端,window.location.pathname由React路由器解析。 React路由器呈现一个组件,它被配置为为/ react / route呈现。

HashRouter

It uses URL hash, it puts no limitations on supported browsers or web server. Server-side routing is independent from client-side routing.

Backward-compatible single-page application can use it as example.com/#/react/route. The setup cannot be backed up by server-side rendering because it’s / path that is served on server side, #/react/route URL hash cannot be read from server side. On client side, window.location.hash is parsed by React router. React router renders a component that it was configured to render for /react/route, similarly to BrowserRouter.

Most importantly, HashRouter use cases aren’t limited to SPA. A website may have legacy or search engine-friendly server-side routing, while React application may be a widget that maintains its state in URL like example.com/server/side/route#/react/route. Some page that contains React application is served on server side for /server/side/route, then on client side React router renders a component that it was configured to render for /react/route, similarly to previous scenario.

它使用URL Hash哈希,它对受支持的浏览器或Web服务器没有限制。服务器端路由独立于客户端路由。

向后兼容的单页面应用程序可以将其用作example.com/#/react/route。服务器端渲染无法备份设置,因为它是服务器端提供的/path,#/react /route URL hash无法从服务器端读取。在客户端,window.location.hash由React路由器解析。 React路由器呈现一个组件,它被配置为为/ react / route呈现,类似于BrowserRouter。

最重要的是,HashRouter用例不仅限于SPA。一个网站可能具有遗留或搜索引擎友好的服务器端路由,而React应用程序可能是一个小部件,它在URL中保持其状态,如example.com/server/side/route#/react/route。一些包含React应用程序的页面在服务器端为/ server / side / route提供,然后在客户端React路由器呈现一个组件,它被配置为为/ react / route呈现,与之前的方案类似。(简单的讲,就是可以使用http://localhost:8080/#/Page1/Moshow这种方式来访问URL,无刷新页面,切换视图,用hash 实现路由切换,本身附带history记录,简单舒服。)

React教程之react-router路由(create-react-app+webpack+route)相关推荐

  1. react快速开始(二)-使用脚手架Create React App创建react应用

    文章目录 react快速开始(二)-Create React App入门 什么是Create React App 快速开始 使用IDE webstrom创建react项目 create react a ...

  2. 利用 Create React Native App 快速创建 React Native 应用

    React Native App简介 打开React Native官方文档你会发现,在Getting Started章节下新添加一个Quick Start Tab页.Quick Start是在v0.4 ...

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

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

  4. react router 路由守卫_react实现hash路由

    众所周知,目前单页面使用的路由有两种实现方式: hash 模式 history 模式 hash 模式 路由原理: 我们先来看hash模式,页面首次加载时需要在load事件中解析初始的URL,从而展示进 ...

  5. 【翻译】基于 Create React App路由4.0的异步组件加载(Code Splitting)

    基于 Create React App路由4.0的异步组件加载 本文章是一个额外的篇章,它可以在你的React app中,帮助加快初始的加载组件时间.当然这个操作不是完全必要的,但如果你好奇的话,请随 ...

  6. React Router路由详解

    React Router路由详解 一.基础 1.安装使用 2.React Router 中通用的组件 路由组件 BrowserRouter 和 HashRouter 路径匹配组件: Route 和 S ...

  7. react更改路由入参_JavaScript基础教程 react router路由传参

    本篇教程介绍了JavaScript基础教程 react router路由传参,希望阅读本篇文章以后大家有所收获,帮助大家对JavaScript的理解更加深入. < 今天,我们要讨论的是react ...

  8. React路由管理 —— React Router 总结

    React项目通常都有很多的URL需要管理,最常使用的解决方案就是React Router了,最近学习了一下,主要是看了一下官方的英文文档,加以总结,以备后查. React Router是做什么的呢, ...

  9. 深圳Web培训:web应用安全培训教程之react

    深圳Web培训:web应用安全培训教程之react react是一个用于构建用户界面的JavaScript库文件,react可以使构建交互UI的过程变得简单.对于你的应用里面的每个状态视图,当数据变化 ...

最新文章

  1. php 比较字符串差,PHP字符串比较函数strcmp()和strcasecmp()使用总结
  2. 代码实战 | 用LeGO-LOAM实现BFS点云聚类和噪点剔除
  3. Java 8 Lambda 表达式详解
  4. 第一次现场看球,第一次场看国家队打球,第一次场看国家队打架,第一次上电视,第... ......
  5. PHP解决约瑟夫环问题
  6. visio 转 pdf 裁边 ——使用pdfcrop对PDF裁边
  7. MobaXterm_Personal_20.1最简单方式链接阿里云服务器
  8. node.js读写文件
  9. DELPHI XE5-8 弹出列表框供选择
  10. python-回文字符串
  11. 人人都能玩航拍 手把手教你装4轴
  12. 递归实现顺序输出整数
  13. mysql切片库_Database数据库切片模式
  14. HTML5期末大作业:旅游网页设计与实现——旅游风景区网站HTML+CSS+JavaScript 景点静态网页设计 学生DW静态网页设计
  15. jQuery的文档处理
  16. 家里装电线时,为啥说“走顶”比“走地”好
  17. 历史类:四大文明古国
  18. 软件著作权申请时间是多久?
  19. tws耳机哪个品牌好?国产好用的tws耳机推荐
  20. JDK8u201安装

热门文章

  1. impacket使用指南
  2. 全球首次实现8K超高清电视直播和5G网络下的8K电视播出
  3. linux信息收集命令,linux中收集系统信息的一些命令
  4. 数字图像处理教程图片素材及源码下载
  5. linux内核 设备列表,Linux 设备树(Device Tree)(转载)
  6. 1028 人口普查 (20 分)C++ 示例代码
  7. 双路高速 AD 实验
  8. HTML5游戏实战之消灭星星(完整源代码)
  9. 大学计算机实验报告示例,实验报告范例(学生).doc
  10. Vuejs脚手架十三