简体   繁体   中英

React-router-dom v6 params

How do I create a route in react using react-router-dom v6?

Currently I'm trying:

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

With a url of "http://localhost:3000/registration?code=testCode", I get the error:

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".

Is this the new syntax for v6 or am I doing something wrong?

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. You won't specify any path match params though as this should be for matching the path part of the URL.

Given:

  • path "/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 />} />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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