2

I want to achieve this(V3)

<Router> <Route path="/" component={App}/> <Route path="*" component={Whoops404}/> </Router> 

with V4 of react-router using react-router-dom but i'm unable to do that, when i try this

<BrowserRouter> <div> <Route path="/" component={App}/> <Route path="*" component={Whoops404}/> </div> </BrowserRouter> 

it renders all the routes in the block instead. am i doing wrong?

****EDIT****
Thank you guys.I have wrapped the routes with a Switch component,and its working fine but when i navigate to localhost:3000/jjj or other routes aside the http://localhost:3000/ the component App still displays.

0

2 Answers 2

3

You need to wrap your routes with Switch component. So it can match one of the routes. You can also omit 'path="*".

<Switch> <Route exact path="/" component={App}/> <Route component={Whoops404}/> </Switch> 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you.I have wrapped the routes with a Switch component,and its working fine but when i navigate to localhost:3000/jjj or other routes aside the http://localhost:3000/ the component App still displays.which is not supposed to be.
because path "/" is included in '/jjj' you need to specify exact property for Route. So only when "/" exactly is matching - you render the component.
2

it seems the "/" route is included in "*".

Try:

To render the / and not the others below, in case you trying to render only "/" and getting this side effect.

(Edit: include commment here:) If does not solve, try to make render only single route with Switch:

<BrowserRouter> <Switch> <Route exact path="/" component={Home} /> <Route path="/about" render={ () => <About title='About' /> } /> <Route path="/costumers" component={Costumers} /> <Route component={NotFound} /> </Switch> </BrowserRouter /> 

4 Comments

If does not solve, try to make render only single route with Switch: <BrowserRouter> <Switch> <Route exact path="/" component={Home} /> <Route path="/about" render={ () => <About title='About' /> } /> <Route path="/costumers" component={Costumers} /> <Route component={NotFound} /> </Switch> </div> </BrowserRouter />
Thank you.I have wrapped the routes with a Switch component,and its working fine but when i navigate to localhost:3000/jjj or other routes aside the localhost:3000 the component App still displays.which is not supposed to be.
did you tried the exact props as would be in <Route exact path="/" component={App}/>
@ dpetrini thanks.its working fine now

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.