React Router: Cannot read property 'pathname' of undefined
Asked Answered
B

20

74

I've just started learning React and got stuck at this error.

Uncaught TypeError: Cannot read property 'pathname' of undefined at new Router

Here is my code:

var React = require('react');
var ReactDOM = require('react-dom');
var { Route, Router, IndexRoute } = require('react-router');
var hashHistory = require('react-router-redux')

var Main = require('./components/Main');

ReactDOM.render(
    <Router history={hashHistory}>
        <Route path="/" component={Main}>

        </Route>
    </Router>,
  document.getElementById('app')
);

The tutorial I was following uses React-Router 2.0.0, but on my desktop I'm using 4.1.1. I tried searching for changes but was unsuccessful in finding a solution that worked.

"dependencies": {
"express": "^4.15.2",
"react": "^15.5.4",
"react-dom": "^15.5.4",
"react-router": "^4.1.1",
"react-router-dom": "^4.1.1",
"react-router-redux": "^4.0.8"
Bushy answered 25/4, 2017 at 20:23 Comment(1)
You want to use the BrowserRouter component not Router.Cobbs
C
42

The error is because the api in React Router v4 is totally different. You can try this:

import React from 'react';
import ReactDOM from 'react-dom';
import {
  BrowserRouter as Router,
  Route
} from 'react-router-dom';

const Main = () => <h1>Hello world</h1>;

ReactDOM.render(
  <Router>
    <Route path='/' component={Main} />
  </Router>,
  document.getElementById('app')
);

You can review the documentation to learn how it works now.

Here I have a repo with routing animation.

And here you can find a live demo.

Competitor answered 25/4, 2017 at 21:1 Comment(1)
Using BrowserRouter instead of Router - this change alone solved my issue of " Uncaught TypeError: Cannot read properties of undefined (reading 'pathname') at Router.render". My versions of related npm packages: "react-router": "^5.1.2", "react-router-dom": "^5.1.2", "react-router-redux": "^4.0.8",Inky
J
124

I got the above error message and it was extremely cryptic. I tried the other solutions mentioned and it didn't work.

Turns out I accidentally forgot to include the to prop in one of the <Link /> components.

Even if the <Link /> does not require a to prop, you simply just add and write it with empty quotes -> e.g. <Link to="" />. That will do the trick!

Wish the error message was clearer. A simple required prop "to" not found or something similar would have been helpful. Hopefully, this saves someone else who has encountered the same problem some time.

Jeramey answered 28/4, 2018 at 2:8 Comment(1)
This is not the first time React shows me cryptic error messages. Same when you don't pass an object to inline style attributes... There is no way to understand what's going on unless you already know. Thanks!Flight
C
42

The error is because the api in React Router v4 is totally different. You can try this:

import React from 'react';
import ReactDOM from 'react-dom';
import {
  BrowserRouter as Router,
  Route
} from 'react-router-dom';

const Main = () => <h1>Hello world</h1>;

ReactDOM.render(
  <Router>
    <Route path='/' component={Main} />
  </Router>,
  document.getElementById('app')
);

You can review the documentation to learn how it works now.

Here I have a repo with routing animation.

And here you can find a live demo.

Competitor answered 25/4, 2017 at 21:1 Comment(1)
Using BrowserRouter instead of Router - this change alone solved my issue of " Uncaught TypeError: Cannot read properties of undefined (reading 'pathname') at Router.render". My versions of related npm packages: "react-router": "^5.1.2", "react-router-dom": "^5.1.2", "react-router-redux": "^4.0.8",Inky
G
16

It turns out that the order of import matter and Router should come first

Incorrect

import { BrowserRouter as Route, Router, Routes } from "react-router-dom";

Correct

import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
Gromwell answered 7/10, 2022 at 21:8 Comment(1)
It's not the order. In the first one, you are using BrowserRouter as Route and importing Router from react-router-dom which is incorrect. You should import Route from react-router-dom and BrowserRouter as Router.Tugman
N
13

Just make sure that you've added to props to <Link> otherwise you would also get the same error.

after adding it should look something like this:-

<Link to="#">
Negation answered 27/9, 2018 at 20:19 Comment(0)
N
7

I had the same error and I resolved it by updating history from 2.x.x to 4.x.x

npm install history@latest --save

Nauseate answered 19/7, 2017 at 9:4 Comment(0)
A
7

Just use BrowserRouter as Router, instead of Router

Apodal answered 25/5, 2022 at 17:40 Comment(0)
H
6

I had a different cause, but the same error and came across this question because of it. Putting this here in case the same happens for others and it may help. The issue for me was that I had updated to react-router-4 and accessing pathname has changed.

Change: (React-Router-3 & earlier)

this.props.location.pathname

To: (React-Router-4)

this.props.history.location.pathname

Henrietta answered 3/8, 2018 at 15:11 Comment(0)
B
6

Use BrowserRouter instead of Router as a wrapper, and don't forget to import BrowserRouter

<BrowserRouter>
    // Rest of the app.
</BrowserRouter>

This error also arises when u are using Link and forget to mention to="/" prop.

Bronchiole answered 27/6, 2022 at 18:10 Comment(0)
N
5

The API in React Router v6 has changed from that of v5.

See the accepted answer in the following: "Cannot read properties of undefined (reading 'pathname')" when testing pages in the v6 React Router

Norther answered 18/11, 2021 at 3:32 Comment(0)
R
5

Check all the Link or NavLink components in your application some of them might be missing the 'to' property which responsible to redirect the user to another path

Romanaromanas answered 24/11, 2021 at 18:54 Comment(0)
D
5

This error still happen if you pass undefined or null value to props to of <Link />.

Dextroglucose answered 22/5, 2022 at 15:36 Comment(0)
O
3

You are giving an undefined value to the "to" props of Link element

import { Link } from "react-router-dom";

const path = undefined;

function Home() {
  return (
        <Link to={path}>Home</Link> 
  );
}
Ought answered 19/12, 2021 at 11:52 Comment(0)
E
2

In react-router-dom 6+ I'm getting this error

Error:

   <Routes>
      <Route path="/about">
        <About />
      </Route>
      <Route path="/users">
        <Users />
      </Route>
      <Route path="/">
        <Home />
      </Route>
   </Routes>

Solution:

<Routes>
   <Route exact path="/" element={<Home />} />
   <Route exact path="/about" element={<About />} />
   <Route exact path="/user" element={<User />} />
</Routes>
Extemporaneous answered 11/4, 2022 at 7:41 Comment(2)
writing exact is not necessary in react-router-dom v6Corpora
Correction: <Route exact> is gone. Instead, routes with descendant routes (defined in other components) use a trailing * in their path to indicate they match deeply. reactrouter.com/docs/en/v6/upgrading/…Fluoric
F
0

had a similar error showing up when I was trying to use history.props('./somelink'). I kept getting the error that the pathname was undefined. Turns out that I was also was using a <Link></Link> in my JSX below. This was what was causing the error. Hope this helps for anyone making the same mistake

Funnyman answered 21/10, 2021 at 9:10 Comment(0)
Q
0

For react-router v6 you should do this (use element and enclose all routes in `: https://mcmap.net/q/134150/-matched-leaf-route-at-location-quot-quot-does-not-have-an-element

Quicken answered 1/2, 2022 at 17:22 Comment(0)
Z
0

This might helps you in react-router-dom v6

import {  BrowserRouter as Router,  Routes,  Route} from "react-router-dom"
Zoochemistry answered 23/7, 2022 at 3:56 Comment(0)
F
0

in some cases you need to define the location props on the main Router like this

ReactDOM.render(
<Router location={history.location} history={hashHistory}>
    <Route path="/" component={Main}>

    </Route>
</Router>, document.getElementById('app'));

Edit: history has to be defined with createBrowserHistory() from 'history'

Friary answered 15/4, 2023 at 12:49 Comment(0)
P
0

Go to index.js file and wrap <App/> inside <BrowerRoute>

<BrowserRouter>
    <App/>                                                   
</BrowserRouter>

and you are done :0

Permanency answered 18/3 at 7:14 Comment(0)
I
0

it was not working and giving me this type error: TypeError: Cannot read properties of undefined (reading 'pathname') unfortunately, i was typing mistake the to attribute when i was using NavLink. you can solve this try to remember write "to" attribute in the correct way.

Indigene answered 26/3 at 5:34 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Ricercar
A
0

you can Correct import:-

import { BrowserRouter as Router, Routes, Route } from "react-router-dom";

Attract answered 24/4 at 20:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.