简体   繁体   English

React 18 [MainLayoutRoute] 不是<route>零件</route>

[英]React 18 [MainLayoutRoute] is not a <Route> component

I use the MainLayout component to add the Navbar to the required pages and redirect the user to the login page if he's not logged in.我使用MainLayout componentNavbar添加到所需页面,如果用户未登录,则将其重定向到登录页面。

However now since I updated to react 18 and Component in Route was replaced by element.但是现在自从我更新为react 18并且Route中的 Component 被 element 替换了。 this part is no longer working and threw an error这部分不再工作并抛出错误

Uncaught Error: [MainLayoutRoute] is not a <Route> component.
All component children of <Routes> must be a <Route> or <React.Fragment
const MainLayoutRoute = ({ path, component: Component, render, ...rest }) => {
  return (
    <Route
      path={path}
      {...rest}
      render={(props) => {
        if (!auth.BasicAuth())
          return (
            <Redirect
              to={{
                pathname: "/login",
                state: { from: props.location },
              }}
            />
          );
        return Component ? (
          <>
            <Navbar />
            <Component {...props} />
          </>
        ) : (
          render(props)
        );
      }}
    />
  );
};

A Route component is used in React applications to define a path that the app should respond to.在 React 应用程序中使用 Route 组件来定义应用程序应响应的路径。 When the app's URL matches the path specified by the Route, the component that is associated with that route will be rendered.当应用程序的 URL 与 Route 指定的路径匹配时,将呈现与该路由关联的组件。 This allows developers to create dynamic, single-page applications that can render different content based on the current URL.这允许开发人员创建动态的单页应用程序,这些应用程序可以根据当前 URL 呈现不同的内容。

On the other hand, MainLayoutRoute is not a Route component.另一方面,MainLayoutRoute 不是 Route 组件。 It is a component that is used to wrap other components and provide them with a common layout.它是一个组件,用于包装其他组件并为它们提供通用布局。 This is useful when building applications with multiple pages, as it allows the developer to define a consistent layout that can be applied to all pages.这在构建具有多个页面的应用程序时很有用,因为它允许开发人员定义可应用于所有页面的一致布局。 Find more info here - https://moderncodr.com/react-18-mainlayoutroute-is-not-a-route-component/在此处查找更多信息 - https://moderncodr.com/react-18-mainlayoutroute-is-not-a-route-component/

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

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