0

The react app was working until I added React Router it showes this error

TypeError: Cannot read properties of undefined (reading 'location')

React App

import { Router, Switch, Route } from "react-router-dom";
function App() {
 return (
   <Router>
     <div className="App">
       <Header />
       <Switch>
         <Route path="/" component={MainPage}></Route>
       </Switch>
     </div>
   </Router>
 );
}

export default App;

1 Answer 1

1

react-router-dom doesn't export Router instead it exports BrowserRouter so the code should be one of the following

First

import { BrowserRouter, Switch, Route } from "react-router-dom";
      <div className="App">
        <Header />
        <Switch>
          <Route path="/" component={MainPage}></Route>
        </Switch>
      </div>
    </BrowserRouter>

Second

import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
      <div className="App">
        <Header />
        <Switch>
          <Route path="/" component={MainPage}></Route>
        </Switch>
      </div>
    </Router>
Sign up to request clarification or add additional context in comments.

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.