英文:
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 <Route>
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 (
<Switch>
<Route path={`${match.path}/:Id?`} component={ImportedComponent} />
</Switch>
);
Changing this to v6:
const match = useMatch(); // which argument does this take?
if (!match) return null;
return (
<Routes>
<Route path={`${match.path}/:Id?`} element={<ImportedComponent />} />
</Routes>
);
答案1
得分: 1
useMatch
Hook(useMatch
钩子)
useMatch
接受一个路径模式参数,并返回一个 PathMatch
对象,如果当前位置与之匹配,则返回该对象,否则返回 null,例如当前 URL 路径。
示例:
假设当前位置是 "/test/foo/bar"
。
-
useMatch("/test/foo/bar");
返回:{ params: {}, pathname: "/test/foo/bar", pathnameBase: "/test/foo/bar", pattern: { path: "/test/foo/bar", caseSensitive: false, end: true, }, }
-
useMatch("/test/:segment1/:segment2");
返回:{ params: { segment1: "foo", segment2: "bar", }, pathname: "/test/foo/bar", pathnameBase: "/test/foo/bar", pattern: { path: "/test/:segment1/:segment2", caseSensitive: false, end: true, }, }
-
useMatch("/bizzbuzz");
返回:null
渲染子路由(Rendering Descendent Routes)
在 react-router-dom@6
中,Routes
组件将在其父级 Route
组件的路径的基础上“构建”它所管理的路径。不需要手动构建这些路径。如果父路径当前不匹配,那么组件不会被渲染,并且 Routes
和子路由也不会被渲染。
示例:
父组件
<Routes>
...
<Route path="/child/*" element={<Child />} />
...
</Routes>
子组件
return (
<Routes>
<Route path="/:Id?" element={<ImportedComponent />} />
</Routes>
);
在这里,只有当 "/child/*"
匹配时,Child
组件才会被渲染。path="/:Id?"
上的路由将在匹配时渲染其 element
内容。由于 Id
是可选参数,因此该路由将匹配 "/child"
、"/child/123"
和 "/child"
的任何其他子路由。
示例(Demo)
英文:
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 "/test/foo/bar"
.
-
useMatch("/test/foo/bar");
returns:{ params: {}, pathname: "/test/foo/bar", pathnameBase: "/test/foo/bar", pattern: { path: "/test/foo/bar", caseSensitive: false, end: true, }, }
-
useMatch("/test/:segment1/:segment2");
returns:{ params: { segment1: "foo", segment2: "bar", }, pathname: "/test/foo/bar", pathnameBase: "/test/foo/bar", pattern: { path: "/test/:segment1/:segment2", caseSensitive: false, end: true, }, }
-
useMatch("/bizzbuzz");
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
<Routes>
...
<Route path="/child/*" element={<Child />} />
...
</Routes>
Child
return (
<Routes>
<Route path="/:Id?" element={<ImportedComponent />} />
</Routes>
);
Here the Child
component will be rendered whenever "/child/*"
is the match. The route on path="/:Id?"
will be matched and render its element
content when it's a match. Since Id
is an optional parameter this route will match "/child"
, "/child/123"
, and any other sub-route of "/child"
.
Demo
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论