8

Hey everyone I dont know whats going on. I have the following routes:

<BrowserRouter>
  <div>
    <Switch>
      <Route path="/patient/:id/" component={PatientWrapper} />
      <Route path="/patient/:id/patient_profile/admission_form" component={PatientAdmission} />
      <Route path="/patient/:id/patient_profile/discharge_form" component={PatientDischarge} />
      <Route path="/patient/:id/patient_profile/encounter_details" component={PatientEncounterDetails} />
      <Route path="/" component={App} />
    </Switch>
  </div>
</BrowserRouter>

Only Route with path="/" and Route with path="/patient/:id" are the ones working, the other 3 routes are just not showing the component that corresponds to the path.

This is how I access to the Route. I have a Header Component with the proper links on it. See below

<ul className="dropdown-menu dropdown-messages">
    <li><Link to={"/patient/" + this.props.id +"/patient_profile/admission_form"} id="admission-link" >Admission</Link></li>
     <li><Link to={"/patient/" + this.props.id +"/patient_profile/discharge_form"} id="discharge-link">Discharge</Link></li>
     <li className="divider"></li>
     <li><Link to={"/patient/" + this.props.id +"/patient_profile/encounter_details"} id="encounter-details">Encounter Details</Link></li>
</ul>

In the Header component I import { Link } from 'react-router-dom'; and in the file where I declare the routes I import { BrowserRouter, Route, Switch } from 'react-router-dom';

What am I doing wrong?

1
  • A loose guess: try reordering the first patient route to underneath the ones with more complex paths Commented May 26, 2017 at 1:32

2 Answers 2

3

The problem here is you are not using exact prop for your parent routes. By default, Route doesn't do an exact match. As an example for path /, both / and /patient are considered as matches. So even in your case, /patient/:id/ Route matches for all other routes path which starts from /patient/:id/. Since Switch only renders the first match, it always renders PatientWrapper even for other paths like /patient/:id/patient_profile/admission_form.

Using exact prop as follows you can explicitly instruct Route to match exactly.

<BrowserRouter>
  <div>
    <Switch>
      <Route exact path="/" component={App} />
      <Route path="/patient/:id/" exact component={PatientWrapper} />
      <Route path="/patient/:id/patient_profile/admission_form" component={PatientAdmission} />
      <Route path="/patient/:id/patient_profile/discharge_form" component={PatientDischarge} />
      <Route path="/patient/:id/patient_profile/encounter_details" component={PatientEncounterDetails} />
    </Switch>
  </div>
</BrowserRouter>
Sign up to request clarification or add additional context in comments.

2 Comments

You're welcome. I updated the answer with a better explanation.
Thank you for your explanation, there is alot I still need to learn
1

Make sure you wrap routes in when you render them:

<BrowserRouter>
    <Switch>
       <Route path="/patient/:id/" component={PatientWrapper} />
      <Route path="/patient/:id/patient_profile/admission_form" component={PatientAdmission} />
      <Route path="/patient/:id/patient_profile/discharge_form" component={PatientDischarge} />
      <Route path="/patient/:id/patient_profile/encounter_details" component={PatientEncounterDetails} />
      <Route path="/" component={App} />
    </Switch>
</BrowserRouter>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.