3

I am learning react and trying to set up routes with react-router-dom. All the routes are working except the default route. The content of the default route component displays on all other components when i navigate to their routes. here is the code and the output below

import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; import One from './one'; import Two from './two'; import Three from './three'; import FourOFour from './404'; import registerServiceWorker from './registerServiceWorker'; import {BrowserRouter, Route} from 'react-router-dom'; ReactDOM.render( <BrowserRouter> <div> <Route exact={true} path="/" component={App}></Route> <Route path="/One" component={One}></Route> <Route path="/Two" component={Two}></Route> <Route path="/Three" component={Three}></Route> <Route path="*" component={FourOFour}></Route> </div> </BrowserRouter>, document.getElementById('root') ); registerServiceWorker(); 

enter image description here

4
  • 2
    Wrap in a <Switch> ? Commented Feb 26, 2018 at 16:35
  • I don't understand. I'm new to react Commented Feb 26, 2018 at 16:37
  • 2
    see reacttraining.com/react-router/web/api/Switch and the example on the right - it will pick the first match. Commented Feb 26, 2018 at 16:38
  • It worked, i just had to import Switch from react-router-dom. Thank you. Please can you provide me with a link to a tutorial apart from the documentation for react 16 so i can learn from there. Thank you Commented Feb 26, 2018 at 16:50

3 Answers 3

4

First thing you need to make use of <Switch> and wrap it around your <Routes>

As described in Here

<Switch> is unique in that it renders a route exclusively. In contrast, every that matches the location renders inclusively.

Firstly to make sure import Switch in your file like this:

import { Switch, Route } from 'react-router' <Switch> <Route exact={true} path="/" component={App}></Route> <Route path="/One" component={One}></Route> <Route path="/Two" component={Two}></Route> <Route path="/Three" component={Three}></Route> <Route path="*" component={FourOFour}></Route> <Switch> 

So your code should be like :

import React from 'react'; import ReactDOM from 'react-dom'; import { Switch, Route } from 'react-router'; // Note this extra line import './index.css'; import App from './App'; import One from './one'; import Two from './two'; import Three from './three'; import FourOFour from './404'; import registerServiceWorker from './registerServiceWorker'; import {BrowserRouter, Route} from 'react-router-dom'; ReactDOM.render( <BrowserRouter> <Switch> // Note this extra Line <Route exact={true} path="/" component={App}></Route> <Route path="/One" component={One}></Route> <Route path="/Two" component={Two}></Route> <Route path="/Three" component={Three}></Route> <Route path="*" component={FourOFour}></Route> <Switch> </BrowserRouter>, document.getElementById('root') ); registerServiceWorker(); 

You can read more about <Switch > Here

Sign up to request clarification or add additional context in comments.

3 Comments

Oh, the sweet karma! Since you are doing 10s of edits, remove the <div> wrap and just leave the <Switch> :)
Literally unusable with the extra <div> tag...
Thanks for pointing out in right direction Dimitar Christoff , finally fixed it :)
1

You need to wrap your Routes in a Switch. A Switch will match the first Route and return it. If you don't use a switch, like you've done here, it will render out each route that matches the current path.

Change it to:

<BrowserRouter> <Switch> <Route exact={true} path="/" component={App}></Route> <Route path="/One" component={One}></Route> <Route path="/Two" component={Two}></Route> <Route path="/Three" component={Three}></Route> <Route path="*" component={FourOFour}></Route> </Switch> </BrowserRouter> 

2 Comments

Returns an error Switch is not defined
@DavidEssien You need to import Switch at the top of your component. import { Switch } from react-router-dom;.
0

The problem arises because all the Routes components that match the location will render. In order to render a single route (first match) you need to wrap your routes in a Switch component.


import { BrowserRouter, Route, Switch } from 'react-router-dom'; ReactDOM.render( <BrowserRouter> <Switch> <Route exact={true} path="/" component={App} /> <Route path="/One" component={One} /> <Route path="/Two" component={Two} /> <Route path="/Three" component={Three} /> <Route component={FourOFour} /> </Switch> </BrowserRouter>, document.getElementById('app') ); 

<Route> responsibility is to render some UI when a location matches the route's path.

<Switch> is unique in that it renders a route exclusively. In contrast, every <Route> that matches the location renders inclusively.

Read more about React-Router's Switch

2 Comments

I mean this is the correct answer, just really low effort. No code examples or links to resources.
I just wanted to answer quickly to provide a fast hint/solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.