withRouter组件(推荐阅读)

作用:

默认情况下必须经过路由匹配渲染的组件才存在this.props,才拥有路由参数,执行this.props.history.push('/detail')跳转到对应路由的页面,然而不是所有组件都直接与路由相连(通过路由跳转到此组件)的,当这些组件需要路由参数时,使用withRouter就可以给此组件传入路由参数,将react-router的history、location、match三个对象传入props对象上,此时就可以使用this.props

如何使用:

比如app.js这个组件,一般是首页,不是通过路由跳转过来的,而是直接从浏览器中输入地址打开的,如果不使用withRouter此组件的this.props为空,没法执行props中的history、location、match等方法。

使用时可以通过下面两种方式:

1.在要注入属性的组件上使用'@withRouter'

2.类似‘withRouter(App)’

import React,{Component} from 'react'
import {Switch,Route,NavLink,Redirect,withRouter} from 'react-router-dom' //引入withRouter
import One from './One'
import NotFound from './NotFound'
/*或者直接在组件上使用‘@withRouter’*/
class App extends Component{
    //此时才能获取this.props,包含(history, match, location)三个对象
    console.log(this.props);  //输出{match: {…}, location: {…}, history: {…}, 等}
    render(){return (<div className='app'>
            <NavLink to='/one/users'>用户列表</NavLink>
            <NavLink to='/one/companies'>公司列表</NavLink>
            <Switch>
                <Route path='/one/:type?' component={One} />
                <Redirect from='/' to='/one' exact />
                <Route component={NotFound} />
            </Switch>
        </div>)
    }
}
export default withRouter(App);  //这里要执行一下WithRouter
复制代码

实例

可以根据路由切换浏览器的title属性,对props.history进行监听,切换路由的时候获取当前的路由路径,同时可以根据不同的路由设置不同的浏览器title标题。

import React,{Component} from 'react'
import {Switch,Route,NavLink,Redirect,withRouter} from  'react-router-dom'
import One from './One'
import NotFound from './NotFound'
class App extends Component{
        constructor(props){
                super(props);
                props.history.listen((location)=>{  //在这里监听location对象
                        console.log(location.pathname);  //切换路由的时候输出"/one/users"和"/one/companies"
                        switch(location.pathname){   //根据路径不同切换不同的浏览器title
                                case '/one/users' : document.title = '用户列表'; break;
                                case '/one/companies' : document.title = '公司列表'; break;
                                default : break;
                        }
                })
        }
        render(){
                return (<div className='app'>
                        <NavLink to='/one/users'>用户列表</NavLink>
                        <NavLink to='/one/companies'>公司列表</NavLink>
                        <Switch>
                                <Route path='/one/:type?'  component={One} />
                                <Redirect from='/' to='/one' exact />
                                <Route component={NotFound} />
                        </Switch>
                </div>)
        }
}
export default withRouter(App);
复制代码

 

 


作者:八叉树
链接:https://juejin.im/post/5bae31b0e51d450e827b568e
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值