简体   繁体   English

React-router-dom v6 参数

[英]React-router-dom v6 params

How do I create a route in react using react-router-dom v6?如何使用 react-router-dom v6 在 React 中创建路由?

Currently I'm trying:目前我正在尝试:

<Route path="/registration:id" element={<Registration />} />

With a url of "http://localhost:3000/registration?code=testCode", I get the error:使用“http://localhost:3000/registration?code=testCode”的 url,我得到错误:

No routes matched location "/registration?code=testCode

I've tried manipulating the route path in many different ways and the only way it works is if the path doesn't have the query part ":id".我试过以多种不同的方式操纵路由路径,唯一有效的方法是路径没有查询部分“:id”。

Is this the new syntax for v6 or am I doing something wrong?这是 v6 的新语法还是我做错了什么?

React-router-dom generally only deals with the path part of the URL and not the querystring, and in RRDv6 there exists a useSearchParams hook you can use to access the querystring params. React-router-dom 通常只处理 URL 的路径部分而不是查询字符串,并且在 RRDv6 中存在一个useSearchParams挂钩,您可以使用它来访问查询字符串参数。 You won't specify any path match params though as this should be for matching the path part of the URL.您不会指定任何路径匹配参数,因为这应该用于匹配 URL 的路径部分。

Given:鉴于:

  • path "/registration?code=testCode"路径"/registration?code=testCode"
  • <Route path="/registration" element={<Registration />} />

Example:例子:

const [searchParams] = useSearchParams();
const code = searchParams.get('code'); // "testCode"

编辑 react-router-dom-v6-params

Put "/" after registration注册后放“/”

<Route path="/registration/:id" element={<Registration />} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM