How to make an endless route?
I have an unknown nesting, following which we need to display it in the route, for example
Folder url.com/folder
-Child 1 url.com/folder/child1
--Child 2 url.com/folder/child1/child2
---Child 3 url.com/folder/child1/child2/child3
---- Child 4 url.com/folder/child1/child2/child3/child4
...
<Route path="folder">
<ComponentExample />
</Route>
How to determine the route if the nesting is unknown?
2
Answers
Use the current segment, e.g. "directory", followed by the
"*"
wildcard character to indicate that anything after the segment can also be matched.This routed component then renders descendent routes relative to this parent path.
And so on until you finally reach a leaf node that doesn’t render another set of descendent routes.
If you just intend to render this same
ComponentExample
for any"/folder/*
then you can use just this as the path and use the splat to get the rest of the path.If you are using react-router-dom v5+, just set path=folder/*, the router will match every route starting with folder/