Issue with react-router-dom (Route in Routes)
I have this routes
for all routes in docs I have container but if I go to the /docs ( for example)
I see this
How can I display only /docs component without error? (I still want to keep container)
If you know how it possible to do it with next - write about it
2 Replies
Haven't used react routes in a while but I think you would do something like this:
I typed this on my phone so there could be some type errors
I don't understand your changes
I want keep docs routes in container
Answered by Paul Chin Jr.
You are encountering this issue because of the order and structure of your routes. When the router encounters a URL, it tries to match it with the first <Route> that fits. In your case, the catch-all route /* for <Error404 /> is being matched before the /docs routes get a chance.
To fix this, you should nest your routes appropriately. Here's a way to structure your routes to avoid this problem:
With the above structure, when you visit /docs, the router will match the /docs route and then, because of the nested <Routes>, it will match the index route inside, displaying the <DocsHomePage /> component.
Regarding Next.js:
Next.js handles routing differently, based on the file system. For the scenario you described, you would have:
A pages directory with:
index.js (for <HomePage />)
login.js (for <Login />)
register.js (for <Register />)
payment.js (for <PaymentStatusPage />)
A docs directory inside pages with:
index.js (for <DocsHomePage />)
developer.js (for <DocsDeveloperPage />)
A customer directory inside docs with:
index.js (for <DocsCustomerPage />)
how-to-install-metamask.js (for <HowInstallMetamask />)
_error.js to customize the error page
To add a container for all the docs routes in Next.js, you'd usually use a _app.js file in the pages directory, and conditionally add the container for routes that match the /docs path.