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

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 组件 怎么在这个组件上添加事件进行路由跳转呢

阅读 2.3k
2 个回答

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

import history from 'history'
history.push(path)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题