Nested Routes
Whenever we want to deal with nested routes (quite common) there is a bunch of hooks that react-router-dom
provides us and a pattern on how to use them that is quite useful to remember:
**App: **uses absolute paths for routes
**Page: **uses useRouteMatch
to exract url
and path
. Uses url
for Links since it contains the actual url, whereas it uses path
for routes since it contains the route path (something like /topics/:topicId
.
SubPage: uses useParams
to extract URL parameters (in this case the topicId) and useRouteMatch
to define it’s own subroutes like we did on it’s parent component.
Content: finally, purely presentational component that doesn’t have any more routes to navigate but just focuses on the content, uses useParams
to get whatever different levels of URL params where passed to get to this content, in this case the url to access this content would look something like localhots:3000/topics/react/state-management
which means that the path would look something like /topics/:topicId/:subId
and so we extract those two like: { topicId, subId }
.