1. 程式人生 > >react-router v4 路由跳轉 動態引數

react-router v4 路由跳轉 動態引數

  • react-router v4 路由跳轉
  • react-router v4 路由傳參

使用 withRouter

withRouter高階元件,提供了history讓你使用~

import React from "react";
import {withRouter} from "react-router-dom";

class MyComponent extends React.Component {
  ...
  myFunction() {
    this.props.history.push("/some/Path");
  }
  ...
}
export default withRouter
(MyComponent);

router4路由傳參取參

// 定義有引數的路由
<Route exact path="/search/:category/:keyword?" component={Search}/>
// 獲取路由中引數
{this.props.match.params.category}