简体   繁体   English

如何在 react-router-dom V5 中创建 404 Page Not Found

[英]How can i create 404 Page Not Found in react-router-dom V5

I use react-router-dom V5.我使用react-router-dom V5。 When I create Route component in my project I use <HashRouter> .当我在我的项目中创建Route组件时,我使用<HashRouter>

This is example of my component:这是我的组件的示例:

<HashRouter>
  <Switch>
    <Route path="/form" component={Login} />
    <Route path="/admin" component={Panel} />
    <Route path="/changepass" component={ChangePass} />
    <Route path="*" component={NotFound} />
  </Switch>
</HashRouter>

For example when I enter: "/akfakafk" it works correctly, but when I enter: "/admin/wtwtwtw" it's not working.例如,当我输入: "/akfakafk"时它可以正常工作,但是当我输入: "/admin/wtwtwtw"时它不工作。

I know it is not implemented correctly,我知道它没有正确实施,

I want to know, how can I implement this?我想知道,我该如何实现?

By the way in "/admin" render Panel component that has return another routes.顺便说一下,在"/admin"渲染Panel组件中返回了另一个路由。

This is my panel routes:这是我的面板路线:

const dashboardRoutes = [
  {
    path: "/dashboard",
    name: dashboard,
    icon: DashboardIcon,
    component: DashboardPage,
    layout: "/admin",
  },
  {
    path: "/devicesManagement",
    name: devicesManagement,
    icon: DevicesIcon,
    component: DevicesPage,
    layout: "/admin",
  }
]

The issue here is that in RRDv5 all route paths are effectively path "prefixes", meaning for example for path "/admin" that all "/admin/*" paths can be matched by it and rendered.这里的问题是,在 RRDv5 中,所有路由路径都是有效的路径“前缀”,例如对于路径"/admin"所有"/admin/*"路径都可以匹配并呈现。

If there are no descendent routes you can use the exact prop on routes you want to exactly match.如果没有后代路线,您可以在要完全匹配的路线上使用exact道具。

Example:例子:

<HashRouter>
  <Switch>
    <Route path="/form" component={Login} />
    <Route path="/admin" exact component={Panel} />
    <Route path="/changepass" component={ChangePass} />
    <Route path="*" component={NotFound} />
  </Switch>
</HashRouter>

Here "/admin" will be matched and render the Panel component, but "/admin/wtwtwtw" will not and the NotFound component will be rendered as expected.此处"/admin"将匹配并呈现Panel组件,但"/admin/wtwtwtw"不会匹配并且NotFound组件将按预期呈现。

If there are descendent routes then you sould again render another Switch component and routes and either another general "not found" route or redirect to a more general "not found" route.如果有后代路由,那么您应该再次渲染另一个Switch组件和路由以及另一个通用“未找到”路由或重定向到更通用的“未找到”路由。

Example:例子:

<HashRouter>
  <Switch>
    <Route path="/form" component={Login} />
    <Route path="/admin" component={Panel} />
    <Route path="/changepass" component={ChangePass} />
    <Route path="*" component={NotFound} />
  </Switch>
</HashRouter>

... ...

const Panel = props => {
  const { path } = useRouteMatch();
  ...

  return (
    ...
    <Switch>
      <Route path={`${path}/dashboard`} component={DashboardPage} />
      <Route path={`${path}/devicesManagement`} component={DevicesPage} />
      ...
      <Route path="*" component={NotFound} />
    </Switch>
  );
};

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

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