react-router - 从子节点获取父路由路径(react-router-V6)
问题描述
V6相关问题!
我有嵌套路由,在子组件中我有选项卡组件...在使用导航(...)之前我需要获取父级的路径
如果我点击导航所有作品并且我在(/View/test1 和 /View/test2 和 /View/test3 )之间切换
但是我需要在单击任何选项卡之前获取父路径...因为我的选项卡组件使用相对路径,所以我需要突出显示它是否处于活动状态..
是否有获取父路由路径的钩子?我正在寻找诸如 usegetParrentPath() 之类的东西或可以构建该路线的某些功能...
function GetTabs() {
return [
{
label: "Test1",
path: `Test1`,
},
{
label: "Test2",
path: `Test2`,
},
{
label: "Test3",
path: `Test3`,
},
] as RouterTabItemType[];
}
function App(){
<Routes>
<Route path "/View/*" element={<View/>
<Routes>
}
function View(){
<Routes>
<Route path "test1" element={<Test1/> // Full path will be /View/test1
<Route path "test2" element={<Test2/> // Full path will be /View/test2
<Route path "test3" element={<Test3/> // Full path will be /View/test3
<Routes>
}
编辑:
我找到了一种解决方案,但在行动之前获得完整路径的反向方法......
const resolver = useResolvedPath(to);
//@ts-ignore
const match = useMatch(resolver.pathname);
使用这个钩子...
解决方案
推荐阅读
- android - 如何使用导航架构组件创建 BottomSheetDialogFragment?
- python - Django:如何在我的登录页面上实现一个记住我的键
- r - geom_raster 给出“条纹”结果
- reactjs - React 是使用类名管理多个主题的好方法吗?
- python - 如何获取 GitHub 存储库的贡献者总数?
- java - 在 macOS 上通过 brew 安装 JRE(但不是 JDK)
- angular7 - 如何将数据传递给自定义组件的标记?
- javascript - 如何禁用具有以逗号分隔的多个值的选项?
- android - Android WebView - 滚动条不填充 layout_height
- uinavigationcontroller - 如何从导航控制器中找到可见的 pushViewController?