Skip to content
Advertisement

mapping doesn’t work inside react-router-dom’s Routes component

instead of type everything manually ,I just want to render my routes as an array.

JavaScript

and in my app.js file, I did something like this:

JavaScript

But its giving error , while I tried to execute it.

JavaScript

but but, If I put like this it works:

<Route path=”/” element={} />

update- non-auth layout piucture nonauth layout

I think , I did some silly mistake. can someone correct me here?

Advertisement

Answer

I don’t see any overt issue with your code, and it in fact appears to run without issue.

Edit mapping-doesnt-work-inside-react-router-doms-routes-component

I suspect you may have a component export/import issue with one or more of the Home, About, and NonAuthLayout components, so double check these.

But, there’s a simpler way

Don’t re-invent the wheel. react-router-dom exports a useRoutes hook that effectively does this.

Reconfigure your routes config to comply with the RRD syntax:

JavaScript

NonAuthLayout

NonAuthLayout is now a layout route and needs to render an Outlet instead of a children prop.

JavaScript

App

JavaScript

Edit mapping-doesnt-work-inside-react-router-doms-routes-component (forked)

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement