While trying to make nested routes in react router dom v5 I found this answer that explains how to do it very well
(please take a look at the code here because it's a little bit difference from the answer mentioned above)
Layouts.js
const NotFound = () => <h1>Not Found</h1>;
function Layouts() {
return (
<Switch>
<Route path="/auth" component={AuthLayout} />
<Route path="/app" component={AppLayout} />
<Route path="/" component={NotFound} />
</Switch>
);
}
AuthLayout
const Signup = () => <p>Login</p>;
const Login = () => <p>Sign up</p>;
function AuthLayout() {
return (
<div>
<h1>Auth Layout</h1>
<Route path="/auth/signup" exact component={Signup} />
<Route path="/auth/login" exact component={Login} />
{/* Commenting this because I want to go to NotFound component */}
{/* <Redirect from="/auth" to="/auth/login" exact /> */}
</div>
);
}
AppLayout
const Home = () => <p>Home</p>;
const Dashboard = () => <p>Dashboard</p>;
function AppLayout() {
return (
<div>
<h1>App Layout</h1>
<Route path="/app/home" exact component={Home} />
<Route path="/app/dashboard" exact component={Dashboard} />
{/* Commenting this because I want to go to NotFound component */}
{/* Redirect from="/app" to="/app/home" exact /> */}
</div>
);
}
But this have one problem, that if you go to a route with /app/somethingnotfound
it won't go to <Route path="/" component={NotFound} />
, it will "stay inside" AppLayout
and render no route.
How can I make /app/somethingnotfound
go to <Route path="/" component={NotFound} />
in this case?
Edit:
Just to be more clear: I don't want to just add <Route component={NotFound} />
inside AuthLayout
and AppLayout
because it will render other things. What I really need is to show the top level NotFound
.
App Layout
`) which isn't what I need – Vencovsky Jun 20 '20 at 17:36