class组件、函数组件获取当前页的路由

2021-08-25  本文已影响0人  krystal_H
import { withRouter } from 'react-router-dom'
class Nav extends Component {
  //...
  componentDidMount() {
    this.props.history.push('/xxx')
  }
}

export default withRouter(Nav)
import { useHistory } from "react-router-dom"
function HomeButton() {
  let history = useHistory();

  function handleClick() {
    history.push("/home");
  }

  return (
    <button type="button" onClick={handleClick}>Go home</button>
  )
}

2.引入withRouter

import { withRouter } from 'react-router-dom'
function HomeButton(props) {
  //....
  props.history.push('/xxxx')
}
export default withRouter(HomeButton)
上一篇下一篇

猜你喜欢

热点阅读