英文:
Can I match any of possible route matches with react router dom 6?
问题
我有3个可能的路径可以在一个路径下。
有没有办法可以匹配所有可能的匹配项?
例如,原始的代码是这样的
const MyComponent = () => {
const firstMatch = useMatch('/:appId/:firstKey');
const secondMatch = useMatch('/:appId/:firstKey/:secondKey');
const thirdMatch = useMatch('/:appId/:firstKey/:secondKey/:thirdKey');
if (firstMatch == null && secondMatch == null && thirdMatch == null) {
return <Navigate to="/" replace={true} />;
}
const firstKey =
(firstMatch?.params.firstKey) ||
(secondMatch?.params.firstKey) ||
(thirdMatch?.params.firstKey);
return ....
伪代码应该是..
const firstMatch = useMatch('/:appId/:firstKey?/:secondKey?/:thirdKey');
但我发现问号模式在版本6中被移除了。
我有更好的选择吗?
英文:
I have 3 possible paths that can be under one path.
Is there any method that I can match all possible matches?
For example, the code as-is like
const MyComponent = () => {
const firstMatch = useMatch('/:appId/:firstKey');
const secondMatch = useMatch('/:appId/:firstKey/:secondKey');
const thirdMatch = useMatch('/:appId/:firstKey/:secondKey/:thirdKey');
if (firstMatch == null && secondMatch == null && thirdMatch == null) {
return <Navigate to="/" replace={true} />;
}
const firstKey =
(firstMatch?.params.firstKey) ||
(secondMatch?.params.firstKey) ||
(thirdMatch?.params.firstKey);
return ....
pseudo-code should be..
const firstMatch = useMatch('/:appId/:firstKey?/:secondKey?/:thirdKey');
But I found that the question mark pattern is removed as of version 6.
Do I have better option?
答案1
得分: 1
可选路径参数适用于Route
组件的path
属性,但目前它们与useMatch
钩子和matchPath
实用程序功能不兼容。关于在这些钩子和实用程序中处理可选路径段的问题和讨论可以在GitHub上找到。
如果您希望实现比使用三个单独的useMatch
钩子更加DRY的方法,我建议将要匹配的路径加载到数组中,然后对每个路径进行迭代,并使用matchPath
实用程序函数检查路径。
逻辑可能类似于以下内容:
const patterns = [
"/:appId/:firstKey/:secondKey/:thirdKey",
"/:appId/:firstKey/:secondKey",
"/:appId/:firstKey"
];
const MyComponent = () => {
const { pathname } = useLocation();
const match = patterns.reduce(
(match, pattern) => (match ? match : matchPath(pattern, pathname)),
null
);
if (!match) {
return <Navigate to="/" replace />;
}
const { firstKey } = match.params;
return (
....
);
};
由于可选路径段有效,也许只匹配您希望一开始匹配的路由会更容易。
示例:
const MyComponent = () => {
const { firstKey } = useParams();
return (
...
);
};
<Routes>
....
<Route
path="/:appId/:firstKey/:secondKey?/:thirdKey?" // <-- firstKey required
element={<MyComponent />}
/>
....
<Route path="*" element={<Navigate to="/" replace />} />
</Routes>
英文:
Optional path params are available for the Route
component's path
prop, but for the time being they are incompatible with the useMatch
hook and matchPath
utility function. There is a filed github issue & discussion regarding processing optional path segments in these hooks and utilities.
If you are looking for something a little more DRY than using three separate useMatch
hooks then I'd recommend loading the paths you want to match into an array and iterating over each, checking the path with the matchPath
utility function.
The logic may look similar to the following:
const patterns = [
"/:appId/:firstKey/:secondKey/:thirdKey",
"/:appId/:firstKey/:secondKey",
"/:appId/:firstKey"
];
const MyComponent = () => {
const { pathname } = useLocation();
const match = patterns.reduce(
(match, pattern) => (match ? match : matchPath(pattern, pathname)),
null
);
if (!match) {
return <Navigate to="/" replace />;
}
const { firstKey } = match.params;
return (
....
);
};
Since optional path segments work, it might just be easier to only match the routes you want to begin with.
Example:
const MyComponent = () => {
const { firstKey } = useParams();
return (
...
);
};
<Routes>
....
<Route
path="/:appId/:firstKey/:secondKey?/:thirdKey?" // <-- firstKey required
element={<MyComponent />}
/>
....
<Route path="*" element={<Navigate to="/" replace />} />
</Routes>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论