如何在react-router4.2中使用js 路由跳转?

发布于 2022-09-07 09:09:00 字数 966 浏览 10 评论 0

1.页面有有个按钮 ,点击这个按钮跳转页面

官方文档说:使用withRouter解决

=====================
import React from 'react'
import PropTypes from 'prop-types'
import { withRouter } from 'react-router'

class ShowTheLocation extends React.Component {
static propTypes = {

match: PropTypes.object.isRequired,
location: PropTypes.object.isRequired,
history: PropTypes.object.isRequired

}

render() {

const { match, location, history } = this.props

return (
  <div>You are now at {location.pathname}</div>
)

}
}

const ShowTheLocationWithRouter = withRouter(ShowTheLocation)

=====================
在实际应用的时候 这段代码怎么用呢?
withRouter 是一个高阶组件? 返回一个ShowTheLocationWithRouter函数? 那这个函数怎么用到跳转呢?

const { match, location, history } = this.props // 这行是什么意思呢?

使用 history.push('/xxx') 这样吗

现在有一个button 组件 怎么在这个组件上添加事件进行路由跳转呢

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残月升风 2022-09-14 09:09:00

// history.js
import createHistory from 'history/createBrowserHistory'
export default createHistory()

import history from 'history'
history.push(path)
不知所踪 2022-09-14 09:09:00

拿到history后就可以用代码进行跳转了,ReactTraining/history: Manage session history with JavaScript这是history的仓库,相关方法查看这个的文档

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文