[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}><Route
dath="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 ...
随机推荐
- 【思维题 kmp 构造】bzoj4974: [Lydsy1708月赛]字符串大师
字符串思博题这一块还是有点薄弱啊. Description 一个串T是S的循环节,当且仅当存在正整数k,使得S是T^k(即T重复k次)的前缀,比如abcd是abcdabcdab的循环节 .给定一个长度 ...
- vue.js笔记1.0
事件: 事件冒泡行为: 1.@click="show($event)" show:function (ev) { ev.cancelBubble=true; } 2.@click. ...
- 【linux】【CPU】【x86】平台说明
节选自 <鸟哥的linux私房菜> http://cn.linux.vbird.org/linux_basic/0520rpm_and_srpm_1.php 操作硬件平台:这是个很好玩的地 ...
- thinkphp5中php7中运行会出现No input file specified. 这个你改个东西
<IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews RewriteEngine On RewriteCond %{RE ...
- Python学习笔记:输入输出,注释,运算符,变量,数字类型,序列,条件和循环控制,函数,迭代器与生成器,异常处理
输入输出 输入函数input()和raw_input() 在Python3.x中只有input()作为输入函数,会将输入内容自动转换str类型: 在Python2.x中有input()和raw_inp ...
- 【HIHOCODER 1055】 刷油漆(树上背包)
描述 小Ho有着一棵灰常好玩的树玩具!这棵树玩具是由N个小球和N-1根木棍拼凑而成,这N个小球都被小Ho标上了不同的数字,并且这些数字都是处于1..N的范围之内,每根木棍都连接着两个不同的小球,并且保 ...
- Splay的用法
splay区间增减查询 #include<cstdio> #include<algorithm> using namespace std; ; const int INF = ...
- HDU 3315 KM My Brute
参考题解 二分图的最优匹配.图很容易建立.再处理相似度的时候.把每个权值扩大100倍.然后再对i==j时 特殊标记.使他们的权值再++1.后面选择的时候就很容易挑出.按原匹配 匹配的个数. 100*( ...
- CodeForces 489F DP Special Matrices
首先统计一下前m行中,有x列1的个数为0,有y列1的个数为1. 设d(i, j)表示有i列1的个数为0,有j列1的个数为1,能到达这个状态的矩阵的个数. 则d(x, y) = 1 每一行都是两个1一起 ...
- JS实现——贪吃蛇
把以下代码保存成Snake.html文件,使用Google或360浏览器打开 <!DOCTYPE HTML> <html> <head> <meta char ...