简体   繁体   English

react-router-dom 嵌套路由和重定向

[英]react-router-dom nested routes and redirects

I'm trying to expand a bit on the nested routes example over at react training in the quick start guide .我正在尝试在 快速入门指南中的反应训练中对嵌套路由示例进行一些扩展。

Code sandbox: https://codesandbox.io/s/routing-test-ubpjp代码沙箱: https://codesandbox.io/s/routing-test-ubpjp

In the code sandbox browser, if you navigate to /some - this works, then click SomeOne - this also works.在代码沙箱浏览器中,如果您导航到 /some - 这有效,然后单击 SomeOne - 这也有效。 However, clicking "SomeTwo" which should redirect to "Some" loads blank, why?但是,单击应该重定向到“Some”的“SomeTwo”会加载空白,为什么?

Also, those links fail to work right off the bat?此外,这些链接无法立即工作? There might be some strange stuff in the sandbox as I've been hitting it with random stuff to try and get it working.沙盒中可能有一些奇怪的东西,因为我一直在用随机的东西来尝试让它工作。

I just want to structure the routes such that I don't have to have all of these routes in one file.我只想构建路线,这样我就不必将所有这些路线都放在一个文件中。

I've done a lot of searching and have tried a lot of different kinds of implementations, but all of them end up having issues / problems, either with adding a catch-all route, or redirects.我进行了很多搜索并尝试了很多不同类型的实现,但所有这些最终都遇到了问题/问题,无论是添加一条包罗万象的路线还是重定向。

Removing the props from the Switch worked, but this breaks the AnimatePresence from framer-motion.从 Switch 中移除道具是可行的,但这会破坏帧运动中的 AnimatePresence。

Following the example of framer-motion at: https://codesandbox.io/s/framer-motion-x-react-router-n7qhp?file=/src/index.js以 framer-motion 为例: https://codesandbox.io/s/framer-motion-x-react-router-n7qhp?file=/src/index.js

The redirect still fails, even with the withRouter hook on the Navigation and redirect component.即使在导航和重定向组件上使用 withRouter 挂钩,重定向仍然失败。

You need not pass any props to Switch component.您无需将任何道具传递给 Switch 组件。 Also make sure that you are rendeing Navigation component as a Route or use withRouter HOC , so that it received Router props.还要确保将Navigation组件渲染为 Route 或使用withRouter HOC ,以便它接收到 Router 道具。

    <Switch>
        {children}
    </Switch> 

编辑表格值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM