据我了解,<Route path="/" component={App} />将提供App与路由相关的props,如location和params。如果我的App组件有许多嵌套的子组件,我如何让子组件无需访问这些props:
- 从 App 传递props
- 使用窗口对象
- 为嵌套的子组件创建路由
我以为this.context.router会有一些与路线相关的信息,但this.context.router似乎只有一些功能来操纵路线。
据我了解,<Route path="/" component={App} />将提供App与路由相关的props,如location和params。如果我的App组件有许多嵌套的子组件,我如何让子组件无需访问这些props:
我以为this.context.router会有一些与路线相关的信息,但this.context.router似乎只有一些功能来操纵路线。
(更新) V5.1 & Hooks (需要 React >= 16.8)
您可以在组件中使用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
(更新) V4 & V5
您可以使用withRouterHOC 来注入match,history并location在您的组件props中。
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)
(更新) V3
你可以使用withRouterHOC 来在你的组件 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)
原答案
如果你不想使用 props,你可以使用React Router 文档中描述的上下文
首先,您必须设置您的childContextTypes和getChildContext
class App extends React.Component{
getChildContext() {
return {
location: this.props.location
}
}
render() {
return <Child/>;
}
}
App.childContextTypes = {
location: React.PropTypes.object
}
然后,您将能够使用这样的上下文访问子组件中的位置对象
class Child extends React.Component{
render() {
return (
<div>{this.context.location.pathname}</div>
)
}
}
Child.contextTypes = {
location: React.PropTypes.object
}
如果上述解决方案对您不起作用,您可以使用 import { withRouter } from 'react-router-dom';
使用它,您可以将您的子类导出为 -
class MyApp extends Component{
// your code
}
export default withRouter(MyApp);
还有你的路由器课程 -
// your code
<Router>
...
<Route path="/myapp" component={MyApp} />
// or if you are sending additional fields
<Route path="/myapp" component={() =><MyApp process={...} />} />
<Router>