My App.tsx:
<Switch>
<Route exact path="/" component={LoginPage} />
<Route path="/dashboard" component={Dashboard} />
<Route path="*" component={NoMatch} />
</Switch>
Dashboard.tsx component:
const { path } = useRouteMatch();
<Switch>
<Route exact path={`${path}`} component={DashboardIndex} />
<Route path={`${path}/users`} component={Users} />
</Switch>
The actual problem lies in the "/dashboard" nested routes. Since i'm not using an exact
prop on it, it would match anything, like "/dashboard/blablabla" though this route doesn't exist in my app. Only "/dashboard/users" does exist. How to deal with non existent routes, when exact
is not being used?
UPDATE
Ended up using this approach:
<Switch>
<Route exact path={`${path}`} component={DashboardIndex} />
<Route path={`${path}/users`} component={Users} />
<Route path={`${path}/*`} component={NoMatch} />
</Switch>
Also if you want to completely redirect to another page, just use <Redirect to="/path" />
:
<Route path={`${path}/*`}>
<Redirect to="/path" />
</Route>