javascript - 访问匹配的子路由自定义道具(React Router v5)
问题描述
如何从 React 中的父组件访问匹配的路由自定义道具?
也就是说,拥有这样的路由器(react-router-config
风格):
{
component: App,
routes: [
{
path: '/',
exact: true,
component: Home,
},
{
path: '/account/',
component: Account,
headerOpened: true,
routes: [
{
path: '/account/mi-cuenta/',
exact: true,
component: MyAccount,
},
{
path: '/account/preferencias/',
exact: true,
component: Settings,
},
{
path: '/account/mi-consumo/',
exact: true,
component: MyConsumption,
},
],
},
{ component: NotFound },
],
}
如何从我的App
组件访问headerOpened
传递给的自定义道具Account
?
使用withRouter
HOC in App
,我无法在 match 或 location 对象中找到任何内容。我唯一拥有的是 location.pathname ,例如,显示/account/mi-cuenta/
.
然后,我有匹配对象,其中仅包含参数(在这种情况下为空),但没有headerOpened
道具的迹象。
从父组件获取此属性的原因是,此属性是组件的渲染器,Header
现在需要该值。
在这里,一个小片段的App
组件可能看起来像:
return (
<div>
<Header opened={ ??? }/>
{renderRoutes(route.routes)}
<Footer />
<div/>
);
解决方案
推荐阅读
- openlayers - 如何通过单击其内部空间来选择多边形
- angular - 如何自动导航 ngu 轮播点
- php - 在 php 中使用 curl 发送摘要身份验证请求时出现错误请求 400 错误
- google-apps-script - 如何在另一个电子表格中使用 Google Apps 脚本?
- python-2.7 - 我正在尝试制作端口扫描仪
- java - 尝试在导航标题部分添加横幅广告后,应用程序崩溃
- php - 两个表一个包含记录,第二个包含位置我想显示第一个表记录而不连接
- ios - 视频关闭时不会触发 willEndFullScreenPresentationWithAnimationCoordinator
- asp.net-web-api - rdlc报告无法解析正则表达式
- html - 工具提示显示在元素下方