Javascript react-router 在子组件中获取 this.props.location
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37516919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
react-router getting this.props.location in child components
提问by xiaofan2406
As I understand <Route path="/" component={App} />
will gives App
routing-related props like location
and params
. If my App
component has many nested child components, how do I get the child component to have access to these props without:
据我了解,<Route path="/" component={App} />
将提供App
与路由相关的道具,如location
和params
。如果我的App
组件有许多嵌套的子组件,我如何让子组件无需访问这些道具:
- passing props from App
- using window object
- creating routes for nested child components
- 从 App 传递道具
- 使用窗口对象
- 为嵌套子组件创建路由
I thought this.context.router
would have some information related to the routes, but this.context.router
seems to only have some functions to manipulate the routes.
我以为this.context.router
会有一些与路线相关的信息,但this.context.router
似乎只有一些功能来操纵路线。
回答by QoP
(Update) V5.1 & Hooks (Requires React >= 16.8)
(更新) V5.1 & Hooks (需要 React >= 16.8)
You can use useHistory
, useLocation
and useRouteMatch
in your component to get match
, history
and location
.
您可以在组件中使用useHistory
,useLocation
和useRouteMatch
来获取match
,history
和location
。
const Child = () => {
const location = useLocation();
const history = useHistory();
const match = useRouteMatch("write-the-url-you-want-to-match-here");
return (
<div>{location.pathname}</div>
)
}
export default Child
(Update) V4 & V5
(更新) V4 & V5
You can use withRouter
HOC in order to inject match
, history
and location
in your component props.
您可以使用withRouter
HOC 来注入match
,history
并location
在您的组件道具中。
class Child 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>{location.pathname}</div>
)
}
}
export default withRouter(Child)
(Update) V3
(更新) V3
You can use withRouter
HOC in order to inject router
, params
, location
, routes
in your component props.
你可以使用withRouter
HOC 来在你的组件 props 中注入router
, params
, location
, routes
。
class Child extends React.Component {
render() {
const { router, params, location, routes } = this.props
return (
<div>{location.pathname}</div>
)
}
}
export default withRouter(Child)
Original answer
原答案
If you don't want to use the props, you can use the context as described in React Router documentation
如果你不想使用 props,你可以使用React Router 文档中描述的上下文
First, you have to set up your childContextTypes
and getChildContext
首先,您必须设置您的childContextTypes
和getChildContext
class App extends React.Component{
getChildContext() {
return {
location: this.props.location
}
}
render() {
return <Child/>;
}
}
App.childContextTypes = {
location: React.PropTypes.object
}
Then, you will be able to access to the location object in your child components using the context like this
然后,您将能够使用这样的上下文访问子组件中的位置对象
class Child extends React.Component{
render() {
return (
<div>{this.context.location.pathname}</div>
)
}
}
Child.contextTypes = {
location: React.PropTypes.object
}