-1

When the user signups into the application the data is saved to DB using an API that returns an access token. After this step user needs to be redirected to another page (Signup Details). I am using the below-attached code to do the same. Once I submit the button the token is returned and the URL is changed to the new one. But the page is not displayed. Kindly help me fix this issue.

enter image description here

The new page to which the user should be redirected.

enter image description here

App.js

enter image description here

Drew Reese
  • 165,259
  • 14
  • 153
  • 181

2 Answers2

0

You need to make the first route's url like this "/" instead of "*", because now it matches all routes.

<Route exact path="/" component={SignupForm} />
Lahcen
  • 1,231
  • 11
  • 15
  • please find here the discussion with more details https://stackoverflow.com/questions/43456656/react-router-v4-routes-not-working – Lahcen Feb 06 '22 at 15:58
0

The path="*" matches literally any route, and since you are rendering the Route components into a Switch, only the first matching Route or Redirect is rendered.

Remember that within a Switch path order and specificity matters! If ordered correctly there is almost zero need to use the exact prop. Order the routes by path in inverse path specificity, i.e. both "/SignupDetails" and "/Signup" are more specific than "/" and should appear higher in the Switch.

<Router>
  <Switch>
    <Route path="/SignupDetails" component={SignupDetailsForm} />
    <Route path="/Signup" component={SignupForm} />
    <Route path="/" component={SignupForm} />
  </Switch>
</Router>

react-router-dom v6 Route path prop can also take an array of paths, so you can make your code a bit more DRY if you like. Keep in mind that path order and specificity also still matters in the array.

<Router>
  <Switch>
    <Route path="/SignupDetails" component={SignupDetailsForm} />
    <Route path={["/Signup", "/"]} component={SignupForm} />
  </Switch>
</Router>
Drew Reese
  • 165,259
  • 14
  • 153
  • 181