可以使用React Router Dom 6匹配可能的路由匹配吗?

huangapple go评论101阅读模式
英文:

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 = () =&gt; {
  const firstMatch = useMatch(&#39;/:appId/:firstKey&#39;);
  const secondMatch = useMatch(&#39;/:appId/:firstKey/:secondKey&#39;);
  const thirdMatch = useMatch(&#39;/:appId/:firstKey/:secondKey/:thirdKey&#39;);

  if (firstMatch == null &amp;&amp; secondMatch == null &amp;&amp; thirdMatch == null) {
    return &lt;Navigate to=&quot;/&quot; replace={true} /&gt;;
  }
  const firstKey =
    (firstMatch?.params.firstKey) ||
    (secondMatch?.params.firstKey) ||
    (thirdMatch?.params.firstKey);

  return ....

pseudo-code should be..

const firstMatch = useMatch(&#39;/:appId/:firstKey?/:secondKey?/:thirdKey&#39;);

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 (
    ....
  );
};

可以使用React Router Dom 6匹配可能的路由匹配吗?

由于可选路径段有效,也许只匹配您希望一开始匹配的路由会更容易。

示例:

const MyComponent = () => {
  const { firstKey } = useParams();

  return (
    ...
  );
};
<Routes>
  ....
  <Route
    path="/:appId/:firstKey/:secondKey?/:thirdKey?" // <-- firstKey required
    element={<MyComponent />}
  />
  ....
  <Route path="*" element={<Navigate to="/" replace />} />
</Routes>

可以使用React Router Dom 6匹配可能的路由匹配吗?

英文:

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 = [
  &quot;/:appId/:firstKey/:secondKey/:thirdKey&quot;,
  &quot;/:appId/:firstKey/:secondKey&quot;,
  &quot;/:appId/:firstKey&quot;
];

const MyComponent = () =&gt; {
  const { pathname } = useLocation();

  const match = patterns.reduce(
    (match, pattern) =&gt; (match ? match : matchPath(pattern, pathname)),
    null
  );

  if (!match) {
    return &lt;Navigate to=&quot;/&quot; replace /&gt;;
  }

  const { firstKey } = match.params;

  return (
    ....
  );
};

可以使用React Router Dom 6匹配可能的路由匹配吗?

Since optional path segments work, it might just be easier to only match the routes you want to begin with.

Example:

const MyComponent = () =&gt; {
  const { firstKey } = useParams();

  return (
    ...
  );
};
&lt;Routes&gt;
  ....
  &lt;Route
    path=&quot;/:appId/:firstKey/:secondKey?/:thirdKey?&quot; // &lt;-- firstKey required
    element={&lt;MyComponent /&gt;}
  /&gt;
  ....
  &lt;Route path=&quot;*&quot; element={&lt;Navigate to=&quot;/&quot; replace /&gt;} /&gt;
&lt;/Routes&gt;

可以使用React Router Dom 6匹配可能的路由匹配吗?

huangapple
  • 本文由 发表于 2023年3月8日 17:20:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定