question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot read property 'pathname' of undefined

See original GitHub issue

Looks like this is a regression of https://github.com/supasate/connected-react-router/issues/54, and potentially related to https://github.com/supasate/connected-react-router/issues/183

But I am getting a TypeError: Cannot read property 'pathname' of undefined error when simply following the docs.

// reducers.ts
import { combineReducers } from 'redux'
import { connectRouter } from 'connected-react-router'

export const createRootReducer = (history) => combineReducers({
  routing: connectRouter(history)
})
// store.ts
import { createBrowserHistory } from 'history'
import { createStore, applyMiddleware } from 'redux'
import { routerMiddleware } from 'connected-react-router'
import { createRootReducer } from './reducers'

export const history = createBrowserHistory()

export const store = createStore(
  createRootReducer(history),
  applyMiddleware(routerMiddleware(history))
)
// app.tsx
import * as React from 'react'
import * as ReactDOM from 'react-dom'
import { Provider } from 'react-redux'
import { Route } from 'react-router-dom'
import { ConnectedRouter } from 'connected-react-router'
import { store, history } from './store'

const App = () => (
  <Provider store={store}>
    <ConnectedRouter history={history}>
      <React.Fragment>
        <Route exact path="/" component={HomePage} />
        <Route path="/about" component={AboutPage} />
        <Route path="/topics" component={TopicsPage} />
      </React.Fragment>
    </ConnectedRouter>
  </Provider>
)

ReactDOM.render(<App />, document.getElementById('app'))
// versions
"react": "^16.6.0"
"react-dom": "^16.6.0"
"react-redux": "^5.1.0"
"react-router-dom": "^4.3.1"
"redux": "^4.0.1"
"connected-react-router": "^5.0.1"
"history": "^4.7.2"

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:32
  • Comments:30 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
apsrcreatixcommented, Apr 16, 2020

For me, this arrived when I had not passed the value of prop “to” in NavLink. I hope this may work for a few people over here. 😄

6reactions
miestrcommented, Dec 3, 2018

Found a solution. Do not define a router props when combining reducers, but pass it in the createStore call as connectRouter(history)(rootReducer), f.e.

root-reducer.js

export default combineReducers({
    user: userReducer,
    products: productsReducer,
    // whatever reducers you have
});

store.js

import { createStore, compose, applyMiddleware } from 'redux';
import { connectRouter, routerMiddleware, RouterState } from 'connected-react-router';
import { createBrowserHistory} from 'history';
import createRootReducer from './root-reducer';

instrumenter = window.__REDUX_DEVTOOLS_EXTENSION__();
export const history = createBrowserHistory();
export const rootReducer = createRootReducer;
const initialState: any = {};

export const store = createStore(
    connectRouter(history)(rootReducer),
    initialState,
    compose(
        applyMiddleware(routerMiddleware(history)),
        instrumenter,
    ),
);

Worked like charm for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Router: Cannot read property 'pathname' of undefined
I've just started learning React and got stuck at this error. Uncaught TypeError: Cannot read property ...
Read more >
Cannot read properties of undefined (reading 'pathname')
The error "Cannot read properties of undefined (reading 'pathname')" occurs when we don't set the to prop on a Link component in React...
Read more >
Cannot read properties of undefined (reading 'pathname')
TypeError : Cannot read properties of undefined (reading 'pathname'). Do anyone have experience with this error?
Read more >
typeerror: cannot read properties of undefined ... - You.com
pathname is undefined because you've not correctly accessed it from the (presumably) passed location prop from route props. You've missed the props part....
Read more >
React Router: Cannot read property 'pathname' of undefined ...
[Solved]-React Router: Cannot read property 'pathname' of undefined-Reactjs · score:29. Accepted answer. The error is because the api in React Router v4 is ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found