Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React router v6 routes with Fallback

How to implement routes with fallback in the react router v6.
I got this error:

Uncaught Error: [RouteWithFallbackBoundary] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>

const RouteWithFallbackBoundary = (props: PathRouteProps) => (
    <Route {...props}>
        <MyErrorBoundary key={props.path}>
            <Suspense fallback={"loading"}>{props.children}</Suspense>
        </MyErrorBoundary >
    </Route>
);
const MY_ROUTES = {
    test: {
        path: '/test',
        component: lazy(() => import('../Component/Test')),
    }
}
  <Routes>
            <RouteWithFallbackBoundary path={MY_ROUTES.test.path}>
                <MY_ROUTES.test.component />
            </RouteWithFallbackBoundary>
  </Routes>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You just need to invert the Route and the RouteWithFallbackBoundary components so the Route is what is rendered into the Routes component.

Example:

const RouteWithFallbackBoundary = ({ children }) => (
  <MyErrorBoundary>
    <Suspense fallback={"loading"}>
      {children}
    </Suspense>
  </MyErrorBoundary >
);

<Routes>
  <Route
    path={MY_ROUTES.test.path}
    element={(
      <RouteWithFallbackBoundary>
        <MY_ROUTES.test.component />
      </RouteWithFallbackBoundary>
    )}
  />
</Routes>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading