[react-router] 平时积累
path通配符:
<Route path="/hello/:name">
// 匹配 /hello/michael
// 匹配 /hello/ryan
<Route path="/hello(/:name)">
// 匹配 /hello
// 匹配 /hello/michael
// 匹配 /hello/ryan
<Route path="/files/*.*">
// 匹配 /files/hello.jpg
// 匹配 /files/hello.html
<Route path="/files/*">
// 匹配 /files/
// 匹配 /files/a
// 匹配 /files/a/b
<Route path="/**/*.jpg">
// 匹配 /files/hello.jpg
// 匹配 /files/path/to/file.jpg
router路由的匹配规则为从上到下,如果有两个相同的路由,会匹配第一个,第二个无效。
router常用组件如下:
Link
Link组件用于取代<a>元素,生成一个链接,允许用户点击后跳转到另一个路由。它基本上就是<a>元素的React 版本,可以接收Router的状态。
render(){
return (
<ul>
<li><Link to="/haha">哈哈</Link></li>
</ul>
)
}
增加样式
<Link to="/haha" activeStyle={{ color: #f00 }}></Link>
增加class
<Link to="/haha" activeclassName></Link> IndexRoute
<Router>
<Route dath="/" component={App}><Routedath="about" component={About}></Route>
<Route dath="con" component={Con}></Route>
</Route></Router> 访问根路由“/”时,不会加载任何子组件,<IndexRoute component={Home}></IndexRoute>,这样,访问根路径时会直接加载Home组件,相当于给根路由默认指定了一个组件来加载
注意:IndexRoute组件没有路径参数dath. IndexLink 加载根路由'/'时,activeclassName和activeStyle会失效,或者说总是生效,因为它会匹配根路由下的所有子路由,而IndexLink会使用路由的精确匹配,不会出错
<IndexLink activeStyle={{color: '#f00'}} activeclassName="font"></IndexLink> Redirect 从当前路由跳转到另一个路由
<Redirect from="/a" to="/b"></Redirect> 从"/a"跳转到"/b" IndexRedirect 访问根路由"/"时,将路径指向某个特定的子路由。
<Rouder>
<Roude dath="/" component={App}>
<IndexRedirect to="/about"></IndexRedirect>
<Route dath="/about" component={About}></Route>
</Roude>
</Rouder> history history属性用来监听地址栏的变化,一般分为3种
hashHistory- browserHistory
- createMemoryHistory
hashHistory: <Router history={hashHistory} routes={routes}> 通过路由的hash部分切换 #
browserHistory: <Router history={browserHistory} routes={routes}></Router> 显示正常的路径,背后调用的是浏览器的History API,但是这种情况需要对服务器进行改造,否则用户直接向服务器请求某个子路由,会导致找不到网页的404错误,
如果开发服务器使用的是webpack-dev-server,加上--history-api-fallback参数就可以了。
$ webpack-dev-server --inline--content-base . --history-api-fallback
createMemoryHistory: 主要用于服务器渲染,不与浏览器url互动 const history=createMemoryHistory(location)
表单处理
<form>
<input type="text" placeholder="name" />
<input type="password" placeholder="password" />
</form>
[react-router] 平时积累的更多相关文章
- React native 平时积累笔记
常用插件: react-native-check-box 复选框react-native-sortable-listview 列表拖拽排序 react-native-doc-viewer 预览组件 r ...
- [Redux] Filtering Redux State with React Router Params
We will learn how adding React Router shifts the balance of responsibilities, and how the components ...
- [转] React Router 使用教程
PS:react-route就是一个决定生成什么父子关系的组件,一般和layout结合起来,保证layout不行,内部的子html进行跳转 你会发现,它不是一个库,也不是一个框架,而是一个庞大的体系. ...
- [Redux] Navigating with React Router <Link>
We will learn how to change the address bar using a component from React Router. In Root.js: We need ...
- [Redux] Adding React Router to the Project
We will learn how to add React Router to a Redux project and make it render our root component. Inst ...
- React Router基础使用
React是个技术栈,单单使用React很难构建复杂的Web应用程序,很多情况下我们需要引入其他相关的技术 React Router是React的路由库,保持相关页面部件与URL间的同步 下面就来简单 ...
- 最新的chart 聊天功能( webpack2 + react + router + redux + scss + nodejs + express + mysql + es6/7)
请表明转载链接: 我是一个喜欢捣腾的人,没事总喜欢学点新东西,可能现在用不到,但是不保证下一刻用不到. 我一直从事的是依赖angular.js 的web开发,但是我怎么能一直用它呢?看看最近火的一塌糊 ...
- react router 4.0以上的路由应用
thead>tr>th{padding:8px;line-height:1.4285714;border-top:1px solid #ddd}.table>thead>tr& ...
- React Router 使用教程
一.基本用法 React Router 安装命令如下. $ npm install -S react-router 使用时,路由器Router就是React的一个组件. import { Router ...
- 关于react router 4 的小实践
详细代码栗子:https://github.com/wayaha/react-dom-CY clone然后 npm install npm start 分割线 1.这个项目使用create-react ...
随机推荐
- Java 多线程同步生产者消费者问题-monitor
对这个问题更深一点理解是,每一个线程都在竞争这个类的实例的monitor对象. java会为每个object对象分配一个monitor,当某个对象的同步方法(synchronized methods ...
- Ubuntux下简单设置vim
我自己在vim下的设置,基本写简单脚本用的,在~/.vimrc作出如下设置 syntax on "高亮 set nu "行号显示 set smartindent "基于a ...
- 如何用纯 CSS 创作一个过山车 loader
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/KBxYZg/ 可交互视频 此视频是 ...
- 如何用纯 CSS 创作一副国际象棋
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/WyXrjz 可交互视频 ...
- win10安装pytorch——前面有坑,快跳进去鸭
嗯!花费了不少时间才把pytorch安装成功.主要原因就是: 清华和中科大的Anaconda国内镜像源关闭了 activate.bat 不是内部或外部命令(这个真实奇怪) 1. 安装过程 可以去Ana ...
- scanf(),gets(),getchar()
scanf()与gets()区别: scanf( )函数和gets( )函数都可用于输入字符串,但在功能上有区别.若想从键盘上输入字符串"hi hello",则应该使用gets() ...
- poj 1979 走多少个‘ . '问题 dfs算法
题意:给你一个迷宫地图,让你走.问最多可以走多少个“." 思路:dfs 找到起点,然后对起点进行dfs操作. dfs操作时,要把当前的位置标志成"#"表示已经走过,然后进 ...
- luogu3381 【模板】最小费用最大流
每次选代价最小的流增广 #include <iostream> #include <cstring> #include <cstdio> #include < ...
- PostgreSQL 全文索引
-- 首先要创建自定义的词典,在不使用停用词文件的情况下创建自定义词典,例如: CREATE TEXT SEARCH DICTIONARY english_stem_nostop ( Template ...
- 算法理论——PLA
全称 perceptron learning algrithm 用武之地 二值分类问题,资料线性可分 算法核心(以二维平面为例) 找到一条直线WTX=0,一边全为+1,另一边全为-1.找到了这条线(即 ...