React Routing

hashHistory 会通过 hash 进行对应。好处是简单易用,不用多余设定。
browserHistory 适用于伺服器端渲染,但需要设定伺服器端避免处理错误,这部份我们会在后面的章节详细说明。注意的是若是使用 Webpack 开发用伺服器需加上 –history-api-fallback

$ webpack-dev-server --inline --content-base . --history-api-fallback

createMemoryHistory 主要用于伺服器渲染,使用上会建立一个存在记忆体的 history 物件,不会修改浏览器的网址位置。

const history = createMemoryHistory(location)

  • path path 是对应 URL 的规则。例如:/repos/torvalds 会对应到 /repos/:name 的位置,并将参数传入 Repos 组件中。由 this.props.params.name 取得参数。顺带一提,若为查询参数 /user?q=torvalds 则由 this.props.location.query.q 取得参数。
//常用书写方法
import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, hashHistory, IndexRoute } from 'react-router';
import App from './components/App';
import Home from './components/Home';
import Repos from './components/Repos';
import About from './components/About';
import User from './components/User';
import Contacts from './components/Contacts';ReactDOM.render(<Router history={hashHistory}><Route path="/" component={App}><IndexRoute component={Home} /><Route path="/repos/:name" component={Repos} /><Route path="/about" component={About} /><Route path="/user" component={User} /><Route path="/contacts" component={Contacts} /></Route></Router>,document.getElementById('app')
);
//另一种书写方法
const routes = (<Route path="/" component={App}><IndexRoute component={Home} /><Route path="/repos/:name" component={Repos} /><Route path="/about" component={About} /><Route path="/user" component={User} /><Route path="/contacts" component={Contacts} /></Route>
);ReactDOM.render(<Router routes={routes} history={hashHistory} />,document.getElementById('app')
);

React Routing相关推荐

  1. React学习途径和资料分享

    转自:  https://www.jianshu.com/p/de421e0db710 这段时间因为项目的原因,要做web前端,趁此机会调研了一下当前前端的技术,根据调研的结果,React毫无疑问是现 ...

  2. 关于React Router v4的虚张声势指南

    In this article, we'll cover the important things you can quickly learn to be informed and confident ...

  3. 如何使create-react-app与Node Back-end API一起使用

    This is a very common question among newer React developers, and one question I had when I was start ...

  4. qiankun 2.x 运行时沙箱 源码分析

    当学习成为了习惯,知识也就变成了常识.感谢各位的 点赞.收藏和评论. 新视频和文章会第一时间在微信公众号发送,欢迎关注:李永宁lyn 文章已收录到 github,欢迎 Watch 和 Star. 简介 ...

  5. 如何实现一个sandbox?

    qiankun 中沙箱的实现比较复杂,本章讲解 ProxySandbox 的实现方式. createSandbox 是在 loadApp 的时候执行的.返回值中包含 mount 和 unmount 两 ...

  6. 如何使用Angular Router在Angular 9应用程序中创建导航

    This article will demonstrate the proper way to create page navigation leveraging routing features p ...

  7. react中使用构建缓存_通过构建海滩度假胜地网站,了解如何使用React,Contentful和Netlify...

    react中使用构建缓存 In this full course from John Smilga you will learn React by building a beach resort we ...

  8. 如何使用React.js和Heroku快速实现从想法到URL的转变

    by Tom Schweers 由汤姆·史威士(Tom Schweers) 如何使用React.js和Heroku快速实现从想法到URL的转变 (How to go from idea to URL ...

  9. angular react_Angular 2 vs React:将会有鲜血

    angular react Angular 2 has reached Beta and appears poised to become the hot new framework of 2016. ...

最新文章

  1. SAP PM 初级系列23 - IW22 事务代码里创建维修工单
  2. IOS支付宝集成一系列问题
  3. PHP新手上路文件上传
  4. C Primer+Plus(十一)
  5. Swoole实现私聊群聊
  6. 牛客多校2 - All with Pairs(字符串哈希+next数组)
  7. 第27讲:令人抓狂的 JavaScript 混淆技术
  8. 程序员要记住的几条建议
  9. 修改oracle rac sid,修改Oracle RAC的_asm_hbeatiowait的方法
  10. 如何把Access转成SQL Server的方法介绍
  11. 如何解决iOS界面操作导致导致NSTimer暂停计时的问题?
  12. 获取和分析Dump的几种工具简介
  13. Roller 博客系统的搭建过程
  14. Linux安全加固之账号密码安全策略
  15. Unity设置为中文
  16. 淘宝奇门对接(淘宝订单加密改造)
  17. 基于因果逻辑库的定性事件结果及结果方向性预测
  18. 从linux启动到rootfs的挂载分析 https://blog.csdn.net/kevin_hcy/article/details/17663341
  19. 如何快速将pdf转换excel转换
  20. 先进过程控制与DCS控制系统之间的关系

热门文章

  1. 103个后台PSD源文件、素材网站
  2. CPU占用过高问题排查
  3. 计算机系笔记本电脑配置,笔记本电脑硬件配置全解析, 看完让你轻松选择笔记本...
  4. 中兴通讯能制造服务器吗,中兴通讯服务器搭载“黑匣子”功能,让故障无处遁形...
  5. cf四大服务器位置,CF:从最初的42个服务器到现在的四大战区,穿越火线还能火多久?...
  6. 全球十大外汇黄金交易平台排名
  7. 高博SLAM基础课第四讲——非线性优化
  8. meanshift算法
  9. mac 部署 ansible 记录
  10. 学习笔记(2)——TransE算法(Translating Embedding)