redux-router: Uncaught TypeError: Cannot read property 'location' of undefined

Version Latest Version

History: ^2.1.2 React Router: ^3.0.0

Steps to reproduce

    const createStoreWithMiddleware = compose(applyMiddleware(reduxThunk, logger),
    reduxReactRouter({ routes, createHistory }));

const store = createStoreWithMiddleware(createStore)(rootReducer);

ReactDOM.render(
    <Provider store={store}>
        <ReduxRouter>
            <Route path="/">
                <Route path="login" component={Login} />
                <Route path="/" component={App}>
                    <IndexRoute component={Welcome} onEnter={RequireAuth} />
                    <Route path='logout' component={Logout} onEnter={RequireAuth} />
                </Route>
            </Route>
        </ReduxRouter>
    </Provider>
    , document.querySelector('.main')
);

Actual Behavior

When I ran my code it return me Uncaught TypeError: Cannot read property 'location' of undefined

any solution?

About this issue

  • Original URL
  • State: open
  • Created 8 years ago
  • Comments: 30 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Try to import BrowserRouter: import {BrowserRouter as Router, Route} from 'react-router-dom'; //“react”: “^16.2.0”, //“react-dom”: “^16.2.0” //“react-router-dom”: “^4.2.2”,

npm install react-router@3.0.5

‘BrowserRouter as Router’ It has to exist,or else will be error.

import React from 'react'
import { Switch, BrowserRouter as Router, Route } from "react-router-dom";
import LoginComp from '../pages/pageA'
import RegistComp from '../pages/pageB'

export default () => (
    <Router>
        <Switch>
            <Route path="/m" component={LoginComp}></Route>
            <Route path="/" component={RegistComp}></Route>
        </Switch>
    </Router>
)

+1 image I have same issue from react-router v4.0

@thanhnhan2tn use v3.0.0 it will work

I have upgraded my react router to 4.1.1, and history 4.6.1 and I am facing same issue

Uncaught TypeError: Cannot read property ‘location’ of undefined at new Router (Router.js:31)

Without changing version, is it possible to fix in the code?

v3.0.0 is working no issues

As sky-gg said, We have to include ->‘BrowserRouter as Router’ It has to exist,or else will be error.

The error got cleared and page loaded successfully 😃

I’ve run into this issue with history@3 as well. I tried reverting to history@2 but that then produces the error history.getCurrentLocation is not a function. Tried to work out what was happening myself, but too many libraries I’m not very familiar with. I created a repo based on the “basic example” to try and get anyone else up and running who might be more capable of looking into this.

I can confirm that using react-router v2.8.1 fixes this problem.

@gazpachu I haven’t tried adding redux yet, but to get basic routing to work, I had to use the following versions:

    "react": "^15.0.1",
    "react-dom": "^15.0.1",
    "react-router": "^2.0.0",

Those were downgraded from react@16, react-dom@16, and react-router@4.x.