user image

Jo Micheal
Published in : 2022-04-02

Export 'Switch' (imported as 'Switch') was not found in 'react-router-dom'

React js

Hello everyone, when I created a new React js app using React V18.0.0 my routes component is not working any more

import React from 'react';import ReactDOM from 'react-dom';import { Route, Switch, Router} from 'react-router-dom';ReactDOM.render( <React.StrictMode> <Router> <Switch> <Route path="/" exact={true}> <p>Hello</p> </Route> </Switch> </Router> </React.StrictMode>, document.getElementById('root'));

I still getting error called 

export 'Switch' (imported as 'Switch') was not found in 'react-router-dom'

What should i do?


Mohamed Atef Date : 2022-04-02

Best answers


Best answers


If you are using react-router-dom with version above V6.0.0 you will not be able to use this way again, so you can change the version of the react-router-dom to be below V6 in the package.json file and then run 

npm update

or you can upgrade your code to be like 

import React from 'react';import ReactDOM from 'react-dom';import { BrowserRouter as Router, Route, Routes, } from 'react-router-dom';import Home from './Views/Home';ReactDOM.render( <React.StrictMode> <Router> <Routes> <Route path="/" exact={true} element={<Home />} /> </Routes> </Router> </React.StrictMode>, document.getElementById('root'));

as you see Router is exported as BrowserRouter and Routes became instead of Switch
That's all, let me know if you have another questions

Jo Micheal Date : 2022-04-02

Thank you so much

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

How to update React router without Re-render?
Publish date: 2021-11-22 | Comments: 2

Tag: React js

How to use Datepick of Mui with Formik
Publish date: 2022-02-14 | Comments: 7

Tag: React js

React Component setState doesn't update my property
Publish date: 2022-03-29 | Comments: 2

Tag: React js

Could not be processed as an image because it has an invalid content type
Publish date: 2021-12-08 | Comments: 0

Tag: React js

[solved] Reactjs is not updating the props
Publish date: 2022-02-04 | Comments: 2

Tag: React js

[solved] Expected a conditional expression and instead saw an assignment
Publish date: 2021-11-29 | Comments: 0

Tag: React js

TypeError: Cannot read properties of null (reading 'scrollTop') in jest
Publish date: 2022-09-08 | Comments: 1

Tag: React js