-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow an array of strings in <Route path> #5866
Comments
Thanks! It |
It works but not as expected. |
Working great, just throwing the propTypes warning mentioned above 👍 |
What needs to be done to remove this prop-types warning? In the meantime, I patch this in my own codebase by overriding the default prop types. For those of you reading this, here is what I do: import { Route } from "react-router-dom"
import PropTypes from "prop-types"
Route.propTypes = {
computedMatch: PropTypes.object,
path: PropTypes.oneOfType([PropTypes.array, PropTypes.string]),
exact: PropTypes.bool,
strict: PropTypes.bool,
sensitive: PropTypes.bool,
component: PropTypes.func,
render: PropTypes.func,
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
location: PropTypes.object
} |
Hi, there seems to be an issue with match.params when defining multiple paths for one route with same named params. Example, using following paths: const paths = [
'/:mutation(cs)',
'/:mutation(cs)-:currency(czk|eur)',
] Url /cs will have match.params.mutation undefined. It clearly gets overwritten by the 'non matched' mutation from the second path, which is not matched. This is likely comes from the way pathToRegexp works. let keys = [];
pathToRegexp(paths, keys); Will set keys to: [
{
"name": "mutation",
"prefix": "/",
"delimiter": "/",
"optional": false,
"repeat": false,
"partial": false,
"asterisk": false,
"pattern": "cs"
},
{
"name": "mutation",
"prefix": "/",
"delimiter": "/",
"optional": false,
"repeat": false,
"partial": true,
"asterisk": false,
"pattern": "cs"
},
{
"name": "currency",
"prefix": "",
"delimiter": "/",
"optional": false,
"repeat": false,
"partial": false,
"asterisk": false,
"pattern": "czk|eur"
}
] The workaround is to name the param in first path differently, and then resolve the params manually. const paths = [
'/:mutationA(cs)',
'/:mutationB(cs)-:currency(czk|eur)',
] Using version 4.2.2. |
Getting same issue as @dorrogeray in one of my projects. The workaround to have different named params for each route is not great. What is the status on this PR? This is highly needed and makes sense to have as it aligns with pathToRegexp expected behavior. When will this go live? |
It would be really happy if this feature will be added, but for now I found a solution for my use case. I made a parameter optional, for use cases like:
before: <Route exact path="/users" component={UserList} />
<Route exact path="/users/create" component={UserList>} /> after: <Route exact path="/users/:action?" component={UserList>} />
Furthermore, {
match: {
params: {
action: undefined | string
}
}
} refs: |
solves the problem :-) |
Closed by #5889. |
This is a follow-up from #5393.
It would be nice if
<Route path>
could accept an array of strings instead of a single string. This would make it possible to render the same<Route>
element at multiple different URLs as suggested here.The reason #5393 was not merged is because there was a problem with the way the code worked with respect to relative routes. When this work is done,
match.path
should still be a string, not an array.The text was updated successfully, but these errors were encountered: