import React from 'react'
import PropTypes from 'prop-types'
import { withRouter } from 'react-router'
// A simple component that shows the pathname of the current location
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 (
<p>You are now at {location.pathname}</p>
)
}
}
// Create a new component that is "connected" (to borrow redux
// terminology) to the router.
export default withRouter(ShowTheLocation)
react-router v4之前的版本,有一個叫做
withRouter
的高階元件。你在定義自己的modal組件時包一層即可。包一層withRouter之後,就可以存取到你想要的屬性了,你還可以進一步學習,看看裡面都有些什麼。
也可以使用withRouter,透過this.props.location取得