0

Please am finding it difficult to create a route in react router

I want to create a route like this <Route path="/:storeId" component={StorePage} /> But I also have a route like this /stores Any time I go to the /:storeId page it loads the /users page Am just confuse

Here is the code

<Switch>
  <ScrollToTop>
    <MainLayout>
      <Route exact path="/">
        <Home />
      </Route>
      <Route exact path="/stores">
        <Stores />
      </Route>
      <Route exact path="/:storeId">
        <StorePage />
      </Route>
    </MainLayout>
  </ScrollToTop>
</Switch>

Any help please.

7
  • Please share your code so we can help you. Commented Apr 19, 2021 at 22:08
  • Okay <Switch> <ScrollToTop> <MainLayout> <Route exact path="/"> <Home /> </Route> <Route exact path="/stores"> <Stores /> </Route> <Route exact path="/:storeId"> <StorePage /> </Route> </MainLayout> </ScrollToTop> </Switch> Please I intentionally don't want <Route path="/stores/:storeId" /> Commented Apr 19, 2021 at 22:16
  • Please edit your question and add your code there Commented Apr 19, 2021 at 22:17
  • Okay . . . Done Commented Apr 19, 2021 at 22:22
  • Your code is missing the /user case now. Maybe have a read of reactrouter.com/web/api/Switch and see if that helps. Specifically about the order of things Commented Apr 19, 2021 at 22:26

1 Answer 1

1

You should do something like this

<Switch>
  <Route exact path="/users">
        <Users/>
      </Route>
      <Route path="/users/:id">
        <UserById/>
      </Route>
</Switch>
Sign up to request clarification or add additional context in comments.

4 Comments

So there is no way to user /:id instead of /users/:id
you can use that too but i feel /users/:id is a better representation. It seems like /:id would give you more headache since all links would technically qualify for that route
Okay, I was just try to see if I could get it done like this /:id cause thats the approach I wanted. I wonder why is heard in react router
do what i did above. Just replace /:id but dont use exact with /:id

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.