1

I need to set a dynamic route with query params -> http://localhost:3000/reset-password/?id=2

<Route path="/reset-password/?id=:id" component={ResetPassword} />

But this isn't working and it's showing as page not found. Please help on this.

SajZ
  • 212
  • 2
  • 12

5 Answers5

2
  1. No need for the ?id=:id, thats part of react-router
  2. You probably need the exact keyword;
<Route exact path={"/reset-password/:id"} component={ResetPassword} />

Then, in the ResetPassword component, use the following prop to get the :id;

this.props.match.params.id

React : difference between <Route exact path="/" /> and <Route path="/" />

0stone0
  • 21,605
  • 3
  • 29
  • 49
1

Get rid of the query string.

<Route path="/reset-password/:id" component={ResetPassword} />

aolivera
  • 372
  • 2
  • 21
0

Found a solution here for query strings, but i'd recommend using params and useParams() hook which don't need any setup and has the same usability as shown below

function useQuery() {
  return new URLSearchParams(useLocation().search);
}


const { id } = useQuery();
Kakiz
  • 529
  • 4
  • 13
0

<Route path="/reset-password" exact render={(props) => <ResetPassword {...props} />} />

Darda
  • 31
  • 1
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 02 '22 at 10:58
-2
<Route path="/reset-password/:id" component={ResetPassword} />

Url to visit: http://localhost:3000/reset-password/2

TokaLazy
  • 138
  • 6