1

I'm new to react redux and building my first web app.

So I created Router and added the specific pages:

   <Switch>
                <Route path="/">
                    <Home />
                </Route>
                <Route path="/Info/:id/:name/:type">
                    <Info />
                </Route>
            </Switch>

When I have path="/" and path="/Info/:id/:name/:type", it does not work. But when I have something like path="/Home", path="/Info/:id/:name/:type", it works very well. Am I doing something wrong?

1
  • by not working, you mean you cannot navigate to the home page? Commented Apr 5, 2020 at 6:59

3 Answers 3

1

Pass 'exact' as a prop:

<Route path="/" exact><Home /></Route>
Sign up to request clarification or add additional context in comments.

Comments

0

So by the nature of it, route is matched with the first path. what you should do is use 'exact' in your first path.

 <Switch>
            <Route exact path="/">
                <Home />
            </Route>
            <Route path="/Info/:id/:name/:type">
                <Info />
            </Route>
        </Switch>

1 Comment

hey @Abhishek.. Switch and exact are basically alternatives to each other. They don't actually need to work together in this case. even though it would work
0

Like i always remind my students.. <Switch /> and exact are basically alternatives for each other..

If you have more than 2 or 3 routes.. go for <Switch/>. exact prop is only good enough for one time use only.

I have 2 answers for you.

<Route exact path="/">
    <Home />
</Route>
<Route path="/Info/:id/:name/:type">
    <Info />
</Route>

Just change the order of routes.. because Switch only takes the first match.

<Switch>
    <Route path="/Info/:id/:name/:type">
        <Info />
    </Route>
    <Route path="/">
        <Home />
    </Route>
</Switch>

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.