Changing from useRouteMatch to useMatch / useLocation

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

Changing from useRouteMatch to useMatch / useLocation

问题

根据React Router 6的文档,useRouteMatch钩子已被移除,取而代之的是useMatch。由于在v5中,useRouteMatch要么不带参数并返回当前<Route>的匹配对象,要么带一个参数。

这是否意味着当没有参数使用useRouteMatch时,我可以直接使用useLocation中的pathname?我是否仍然需要将它传递给useMatch?

我试图将版本6的代码转换成如下所示(v5代码):

const match = useRouteMatch();
if (!match) return null;
return (
  <Switch>
    <Route path={`${match.path}/:Id?`} component={ImportedComponent} />
  </Switch>
);

将其更改为版本6:

const match = useMatch(); // 这个函数需要传入哪个参数?
if (!match) return null;
return (
  <Routes>
    <Route path={`${match.path}/:Id?`} element={<ImportedComponent />} />
  </Routes>
);
英文:

Looking at the documentation for react-router 6, the useRouteMatch hook was removed in favor of useMatch. Since the in v5 useRouteMatch either

> takes no argument and returns the match object of the current &lt;Route&gt; or takes a single argument

Does that mean that when useRouteMatch is used without an argument I could just use the pathname from useLocation? And should I still pass that to useMatch?

Example of what I'm trying to convert to version 6 (v5 code):

const match = useRouteMatch();
if (!match) return null;
return (
  &lt;Switch&gt;
    &lt;Route path={`${match.path}/:Id?`} component={ImportedComponent} /&gt;
  &lt;/Switch&gt;
);

Changing this to v6:

const match = useMatch(); // which argument does this take?
if (!match) return null;
return (
  &lt;Routes&gt;
    &lt;Route path={`${match.path}/:Id?`} element={&lt;ImportedComponent /&gt;} /&gt;
  &lt;/Routes&gt;
);

答案1

得分: 1

useMatch Hook(useMatch 钩子)

useMatch 接受一个路径模式参数,并返回一个 PathMatch 对象,如果当前位置与之匹配,则返回该对象,否则返回 null,例如当前 URL 路径。

示例:

假设当前位置是 &quot;/test/foo/bar&quot;

  • useMatch(&quot;/test/foo/bar&quot;); 返回:

    {
      params: {},
      pathname: &quot;/test/foo/bar&quot;,
      pathnameBase: &quot;/test/foo/bar&quot;,
      pattern: {
        path: &quot;/test/foo/bar&quot;,
        caseSensitive: false,
        end: true,
      },
    }
    
  • useMatch(&quot;/test/:segment1/:segment2&quot;); 返回:

    {
      params: {
        segment1: &quot;foo&quot;,
        segment2: &quot;bar&quot;,
      },
      pathname: &quot;/test/foo/bar&quot;,
      pathnameBase: &quot;/test/foo/bar&quot;,
      pattern: {
        path: &quot;/test/:segment1/:segment2&quot;,
        caseSensitive: false,
        end: true,
      },
    }
    
  • useMatch(&quot;/bizzbuzz&quot;); 返回:

    null
    

渲染子路由(Rendering Descendent Routes)

react-router-dom@6 中,Routes 组件将在其父级 Route 组件的路径的基础上“构建”它所管理的路径。不需要手动构建这些路径。如果父路径当前不匹配,那么组件不会被渲染,并且 Routes 和子路由也不会被渲染。

示例:

父组件

<Routes>
  ...
  <Route path=&quot;/child/*&quot; element={<Child />} />
  ...
</Routes>

子组件

return (
  <Routes>
    <Route path=&quot;/:Id?&quot; element={<ImportedComponent />} />
  </Routes>
);

在这里,只有当 &quot;/child/*&quot; 匹配时,Child 组件才会被渲染。path=&quot;/:Id?&quot; 上的路由将在匹配时渲染其 element 内容。由于 Id 是可选参数,因此该路由将匹配 &quot;/child&quot;&quot;/child/123&quot;&quot;/child&quot; 的任何其他子路由。

示例(Demo)

Changing from useRouteMatch to useMatch / useLocation

英文:

The useMatch Hook

useMatch takes a single path pattern argument, and returns a PathMatch object or null if there is a match to the current location, e.g. the current URL path.

Example:

Given the current location is &quot;/test/foo/bar&quot;.

  • useMatch(&quot;/test/foo/bar&quot;); returns:

    {
      params: {},
      pathname: &quot;/test/foo/bar&quot;,
      pathnameBase: &quot;/test/foo/bar&quot;,
      pattern: {
        path: &quot;/test/foo/bar&quot;,
        caseSensitive: false,
        end: true,
      },
    }
    
  • useMatch(&quot;/test/:segment1/:segment2&quot;); returns:

    {
      params: {
        segment1: &quot;foo&quot;,
        segment2: &quot;bar&quot;,
      },
      pathname: &quot;/test/foo/bar&quot;,
      pathnameBase: &quot;/test/foo/bar&quot;,
      pattern: {
        path: &quot;/test/:segment1/:segment2&quot;,
        caseSensitive: false,
        end: true,
      },
    }
    
  • useMatch(&quot;/bizzbuzz&quot;); returns:

    null
    

Rendering Descendent Routes

In react-router-dom@6 the Routes component will "build" the paths it manages all relative to it's parent's Route component's path. There's no need to build these paths yourself manually. If the parent path is not currently matched then the component is not rendered and the Routes and descendent routes are not rendered.

Example:

Parent

&lt;Routes&gt;
  ...
  &lt;Route path=&quot;/child/*&quot; element={&lt;Child /&gt;} /&gt;
  ...
&lt;/Routes&gt;

Child

return (
  &lt;Routes&gt;
    &lt;Route path=&quot;/:Id?&quot; element={&lt;ImportedComponent /&gt;} /&gt;
  &lt;/Routes&gt;
);

Here the Child component will be rendered whenever &quot;/child/*&quot; is the match. The route on path=&quot;/:Id?&quot; will be matched and render its element content when it's a match. Since Id is an optional parameter this route will match &quot;/child&quot;, &quot;/child/123&quot;, and any other sub-route of &quot;/child&quot;.

Demo

Changing from useRouteMatch to useMatch / useLocation

huangapple
  • 本文由 发表于 2023年6月29日 19:19:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580542.html
匿名

发表评论

匿名网友

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

确定