react-router与react-router-dom

一、路由的跳转

1.在react-router中

router4.0以上版本
直接this.props.history.push('/path')可以进行跳转了
或者引入hashHistory
<Router history={hashHistory} routes={routes} />

hashHistory.push( 'cstats/allProd');

router3.0以上版本
this.props.router.push('/path')实现跳转

2.在react-router-dom中

直接this.props.history.push('/path')可以进行跳转了

3.带参数的路由的跳转
###react-router
###配置路由时需引入hashHistory
<Router history={hashHistory}>

 hashHistory.push({
      pathname: 'cstats/allProd',
      state: {
        lastDate: end,
        rankType: this.state.rankType
      }
    });
###react-router-dom
  this.props.history.push({
      pathname: '/risk/mhs/detail',
      search: queryString.stringify({
        prod: prod,
        type: type
      })
    })
4.在子组件中跳转路由

需要给父组件添加属性history

###父组件
 <ProdScoreRank
     data={totalRank}
     title="全部产品线排名"
     height={750}
     itemHeight={16}
     itemMargin={3}
     search={false}
     more={true}
     startTime={startTime}
     endTime={endTime}
     history={this.props.history}
     onMoreClick={() => {
        this.handleMoreVisible(true);
    }}
     />
###子组件
 this.props.history.push({
          pathname:'/risk/mhs/myProd',
          search: queryString.stringify({
              prod:prod
          })
      })

二、参数的获取

使用this.props.match.params.xxx 可以获取到当前路由的参数

###在react-router中
   let currentProd = queryString.parse(this.props.location.query).prod;
###在react-router-dom中
   let currentProd = queryString.parse(this.props.location.search).prod;

三、this.props

1.react-router
CB95C6D5-5FE4-402F-9CAE-393359E0FCFF_meitu_1.jpg

可以看出在react-router3.0版本中有以下属性:

1.1this.props.children

它表示组件的所有子节点。

1.2this.props.location
image.png
1.3this.props.params

参数匹配

1.4this.props.route
image.png
1.5this.props.routeParams
1.6this.props.router
image.png
1.7this.props.routes
image.png
2.react-router-dom
image.png

在react4.0版本中有以下属性:

2.1this.props.history

image.png

常用的属性有:this.props.history.push

2.2this.props.location

image.png

常用的属性:this.props.location.pathname,this.props.location.search,this.props.location.state

2.3this.props.match
this.props.match

常用的属性有:this.props.match.paramsthis.props.match.path

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Lesson11、首先确保安装了Node.js和npm依赖包管理工具2、在git上克隆官方示例程序 git clo...
    冰_心阅读 2,738评论 0 16
  • 一、基本用法 React Router 安装命令如下。 $ npm install -S react-router...
    sunnyghx阅读 4,541评论 0 6
  • React Router 4.0 (以下简称 RR4) 已经正式发布,它遵循React的设计理念,即万物皆组件。所...
    梁相辉阅读 97,632评论 24 195
  • 选择器 css选择器有五种 选择器是一种选择方式,选中想要的元素的方式,叫选择器,有很多选择器,但是目的都一样 元...
    狂风舞蝶丶阅读 1,183评论 0 0
  • 她优雅地走着 犹如夜色垂暮大地 人流不经意漫过 舞台灯光像失事现场 我无言踱步 或掀开书页簌簌 虚诞与妄作在身后压...
    荻枏阅读 217评论 0 3