Given I have 2 path rendering the same component, how do I avoid to repeat route configs like this :
<Route path="/path1" component={MyComp} />
<Route path="/path2" component={MyComp} />
Best solution I found so far (but seems a bit strange) :
<Route path="/:path(path1|path2)" component={MyComp} />
<Route path={["/common-one", "/common-two"]} component={Common} />
For version 5 above
https://reactrouter.com/web/api/Route
Great answer. This also helps in the following scenario:
<Route path="/path/path1" component={MyComp} />
<Route path="/path/:subpath(path2|path3)" component={MyAnotherComp} />
Without (path2|path3) both components MyComp and MyAnotherComp are mounted if the path is /path/path2.
PS. Would have added as a comment, but not eligible :).
If you need to allow multiple match (different parameters) but also without parameter - e.g. match URLs:
/questions
/questions/posted
/questions/answered
You can do this with optional parameter (note question mark after the bracket)
<Route path="/questions/:tab(posted|answered)?" component={...} />
Related
So, I am using react router V3, and surprisingly one detail as little as one uppercase letter makes one route to not match. So this does worl:
<Route
path="payments"
component={AccountBalance}
>
<Route path=":paymentId" component={AccountBalance} />
</Route>
While this doesn't produce any navigation
<Route
path="payments"
component={AccountBalance}
>
<Route path=":paymentID" component={AccountBalance} />
</Route>
The difference is paymentID vs paymentId.
Reading react router v3 docs, there is nothing suggesting that this should happen
The route path must be lowercase, for example, you can use,
<Route path=":payment_id" ... />
for example, in javascript,
let helloWorld;
and
let HelloWorld;
are not equal, becouse theese variables were not written equal.
I have a component called <TaskView> that renders some info about a specific task. My tasks are nested so that I would be able to navigate like so /task1/task1-1/task1-1-1... and so on for N (indefinitely).
How should I write a path that would resolve this url ?
To write a path for level 1 nested I would do: <Route path="/task/:p1" component={taskView} />, for level 2 I would do <Route path="/task/:p1/:p2" component={taskView} /> I don't know how deep the task structure would be, so how would I write a path for an undefined number of parameters ?
ReactRouter has a notion of non exact matches. It will attempt to find the best match so you don't need to define all these subroutes to mount the same components.
React : difference between <Route exact path="/" /> and <Route path="/" />
So actually you should be able to do
<Route path="/task/:p1" component={taskView} />
just fine.
My problem is that i cant figure out the best way to handle multiple dynamic params in a deep link without having to have it fully rely on the order of the Routes in the JSX
I'll start by outline the flow of my app.
List of Television Series -> user clicks series -> List of Episodes in Series -> user clicks episode -> Show stats about episode
after this flow, i want my path to be /series/:series_id/episode/:episode_id.
Currently i have this working by setting up my router basically like so:
<Router>
<Switch>
<Route exact path="/series" component={SeriesLibrary} />
<Route
path="/series/:series_id/episode/:episode_id"
component={Episode}
/>
<Route path="/series/:series_id" component={EpisodeLibrary} />
</Switch>
</Router
but that is relying on the multi-dynamic path (the one with the series_id and episode_id) coming before the one that just has the series_id!
This is because even when the path is series/4356/episode/4567 it would match the series/4356 before ever rendering the Episode component.
Is there a better way for me to address the issue of multiple dynamic deep linked params in react router?
I feel like this cannot be best practice, relying strictly on the order of JSX elements for your app to work?
You can pass exact prop to to EpisodeLibrary:
<Router>
<Switch>
<Route exact path="/series" component={SeriesLibrary} />
<Route exact path="/series/:series_id" component={EpisodeLibrary} />
<Route
path="/series/:series_id/episode/:episode_id"
component={Episode}
/>
</Switch>
</Router>
Using react-router-dom I have a route like so:
<Route path='/:year?/:month?' component={ThingUsingYearAndMonth} />
But really I want BOTH year and month to exist (e.g. /2018/Dec or /2017/Jan) or neither. I.e. just /2018 wouldn't match
I would probably handle that using two routes -- one for when both are provided and one for when neither are provided:
<Route path='/:year/:month' component={ThingUsingYearAndMonth} />
<Route path='/' exact component={ThingUsingYearAndMonth} />
Try providing explicit parameters with regex:
<Route path='/:year(\\d{4})?/:month([A-Za-z]+)?' component={ThingUsingYearAndMonth} />
I don't think there is any mechanism provided by the react-router-dom to validate the route params. You are better off creating your own Higher Order Component and validate the route params in it. Or you can use regex to validate your params in the route itself.
More examples are definitely needed in the docs for this but basically I am after something like this for example:
<Route path="/" component={CoreLayout}>
<IndexRoute component={IndexLayout} />
<Route path="foo/bar" component={MyComponent}>
<Route path="#baz=laa" component={SubComponent} />
</Route>
</Route>
I've tried importing history attribute and all sorts and cannot get it to match. Our issue is that we are building a new mobile site, keep the same paths as the desktop site, however we have some routes which are new on mobile and we want to add those using the hash.
If anybody has any ideas, would be much appreciated. Thanks