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

[solved] Version code 1 has already been used React native
Publish date: 2023-03-13 | Comments: 1

Tag: React js

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

Tag: React js

Unexpected mix of '&&' and '||' Reactjs
Publish date: 2021-10-30 | Comments: 0

Tag: React js

How to use SetFieldValue from outside render function? Formik
Publish date: 2021-07-03 | Comments: 2

Tag: React js

[Solved] Cannot read properties of null (reading 'getBoundingClientRect')
Publish date: 2021-12-02 | Comments: 2

Tag: React js