react-router @4用法整理

router@4

react-router@4官方文档
github源码react

这篇文章主要介绍了react-router@4的基本用法,包括动态路由,编程式导航等git

  1. 安装
  2. switch用法
  3. 动态路由的基本用法
  4. 编程式导航(withRouter)
  5. 总结
  1. 安装github

    npm i react-router-dom -S
  2. switch用法
    示例代码:
import { Switch, BrowserRouter as Router, Route, Redirect} from 'react-router-dom';
class SwitchCom extends Component {
    render() {
        return (
            <Router>
                <Switch>
                    <Route path="/cart" component={Cart}></Route>
                    <Route path="/search" component={Search} />
                    <Route path="/home" component={Main} />
                    <Redirect from="/" to="/home"></Redirect>           
                    <Route path="/mine" component={Mine}></Route>
                    <Route path="/class" component={Class}></Route>
                    <Route component={NoMatch}></Route>
                </Switch>
            </Router>
        )
    }
}

关于路由重定向: 使用Redirect..from..to的格式,注意位置须要在定义to路由的后面,好比to="/home",重定向就须要写在Route path="/home" 后面
关于404路由匹配,默认写在路由末尾,前面的路由都不匹配时,自动匹配404
关于Route,必须写在Router标签里面,不然会报错web

3.动态路由的基本用法:npm

import { BrowserRouter as Router, Route, NavLink} from 'react-router-dom';
<div className="tab-bar">
    <Route path="/index" exact component={Index}></Route>
    <Route path="/index/news" component={News}></Route>
    <Route path="/index/course" component={Course}></Route>
    <Route path="/index/mine" component={Mine}></Route>
    <ul className="footer">
        <li><NavLink exact to="/index" activeStyle={{ color: '#4dc060' }}>首页列表项目 </NavLink></li>
        <li><NavLink to="/index/news" activeStyle={{ color: '#4dc060' }}>资讯</NavLink></li>
        <li><NavLink to="/index/course" activeStyle={{ color: '#4dc060' }}>课程</NavLink></li>
        <li><NavLink to="/index/mine" activeClassName="selected">个人</NavLink></li>
    </ul>
</div>

上面的exact表示绝对匹配/index,若是不注明exact,则/index还会匹配/index/new等等
上面代码实现了一个相似tabbar切换的效果编程

关于NavLink 和 Link:
若是仅仅须要匹配路由,使用Link就能够了,而NavLink的不一样在于能够给当前选中的路由添加样式, 好比上面写到的activeStyleactiveClassNameredux

4.编程式导航(withRouter用法)react-router

import {withRouter} from 'react-router-dom';

goBack(){
    this.props.history.goBack();
}  
goDetail(){
    this.props.history.push('/detail');
}  
goDetailWithParam(item){
    this.props.history.push({pathname : '/cart',state:{item}});
}
    
<span className="ico" onClick={this.goBack.bind(this)}>
    <i className="iconfont">&#xe501;</i>
</span>
//这里的item来自for循环的每一项
<li onClick={this.goDetailWithParam.bind(this,item)} key={encodeURI(item.imgUrl)}>

export default withRouter(Header);

引入withRouter以后,就可使用编程式导航进行点击跳转, 须要注意的是export default的暴露如上面所写,若是结合redux使用,则暴露方式为: withRouter(connect(...)(MyComponent))
调用historygoBack方法会返回上一历史记录
调用historypush方法会跳转到目标页,如上面goDetail方法
跳转传参: push()能够接收一个对象参数,跳转以后,经过this.props.location.state接收dom

5 总结:
刚作过一个React的项目,搭配路由选择了react-router @4 ,收获挺多的,打算写文章记录一下收获(也算是遇到的一些坑). 感受@4比以前的router版本更加灵活一些,用法也更加简洁.仍是挺好用的.官方文档也只是用到哪些就看一看,并无从头看到尾,因此理解还不是很深入,若是上面理解有误差,还望指出,共同进步.this

相关文章
相关标签/搜索