redux-router icon indicating copy to clipboard operation
redux-router copied to clipboard

Uncaught TypeError: Cannot read property 'location' of undefined

Open stefensuhat opened this issue 7 years ago • 30 comments

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?

stefensuhat avatar Dec 01 '16 15:12 stefensuhat

Your version of history (3.x) is unsupported. Downgrading to 2.x should work. See https://github.com/acdlite/redux-router/issues/259

mjrussell avatar Dec 01 '16 15:12 mjrussell

@mjrussell I've downgrade my history "history": "^2.0.0", but still show same error.

stefensuhat avatar Dec 01 '16 16:12 stefensuhat

Hm I dont know then, I would have to see a runnable example. Most likely a configuration error though. Are you doing server side rendering? Hash history doesn't work in SSR.

mjrussell avatar Dec 05 '16 04:12 mjrussell

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.

hobenkr88 avatar Dec 06 '16 19:12 hobenkr88

Thanks for creating the example. I'll try to look this week/weekend.

mjrussell avatar Dec 06 '16 21:12 mjrussell

@ssuhat @hobenkr88 Try downgrading react-router from 3.x to 2.x with history version 2.x.

supasate avatar Dec 09 '16 17:12 supasate

@supasate sorry can't try that. I can't do any major changes anymore.

stefensuhat avatar Dec 10 '16 01:12 stefensuhat

Technically React Router 3 is API backwards compatible with React Router 2. Redux-Router relies on some internals of React Router and this may be the source of the issue. But downgrading React Router from 3.x to 2.x shouldnt affect your code

mjrussell avatar Dec 10 '16 01:12 mjrussell

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

csillag avatar Dec 16 '16 16:12 csillag

v3.0.0 is working no issues

jai1255 avatar Mar 15 '17 07:03 jai1255

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

thanhnhan2tn avatar Mar 16 '17 07:03 thanhnhan2tn

@thanhnhan2tn use v3.0.0 it will work

jai1255 avatar Mar 16 '17 07:03 jai1255

npm install [email protected]

jonatassaraiva avatar Apr 13 '17 15:04 jonatassaraiva

Thanks for the help! I wonder if there's an open issue for this problem

dmsergeev avatar Apr 14 '17 00:04 dmsergeev

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?

KalpanaPagariya avatar Apr 17 '17 11:04 KalpanaPagariya

I'm having the same issue as @KalpanaPagariya. This is my store.jsx file:

import { createStore, applyMiddleware, compose } from 'redux';
import { routerMiddleware } from 'react-router-redux';
import thunk from 'redux-thunk';
import createHistory from 'history/createBrowserHistory';
import rootReducer from './reducers/index';

// Create a history of your choosing (we're using a browser history in this case)
const history = createHistory({ basename: '/lighthouse' });

// Build the middleware for intercepting and dispatching navigation actions
const middleware = routerMiddleware(history);

const store = createStore(rootReducer, compose(applyMiddleware(middleware), applyMiddleware(thunk), window.devToolsExtension
  ? window.devToolsExtension()
  : f => f));

export default store;

And my routes:

ReactDOM.render(
  <Provider store={store}>
    <Router
      onUpdate={() => {
        window.scrollTo(0, 0);
        logPageView();
      }} history={history}
    >
      <Route path="/" component={App}>
        <IndexRoute component={Home} />
        <Route path="/favourites" component={Cards} type="favourites" />
        <Route path="/inbox" component={Cards} type="inbox" />
        <Route path="/insights" component={Insight} />
        <Route path="*" component={NotFound} />
      </Route>
    </Router>
  </Provider>, document.getElementById('react-root'));

screen shot 2017-04-24 at 11 51 20

screen shot 2017-04-24 at 12 09 58

Can anyone provide the correct working versions for react-router, react-router-redux and history?

gazpachu avatar Apr 24 '17 09:04 gazpachu

If I apply the middleware like this const store = createStore(rootReducer, compose(applyMiddleware(middleware, thunk), then I get this error:

Router.js?a4aa:87 Uncaught TypeError: Cannot read property 'getCurrentLocation' of undefined

screen shot 2017-04-24 at 12 15 16

gazpachu avatar Apr 24 '17 10:04 gazpachu

@mjrussell @Scarysize any idea of how can I fix the issue? thanks

gazpachu avatar Apr 27 '17 10:04 gazpachu

I've fixed the location of undefined issue by adding export to the history const. But then I had to downgrade the history library to 3.3.0 and then now I get this error: Uncaught Error: Cannot find module "history/createBrowserHistory". So I think I will revert everything back to the old versions :/

gazpachu avatar Apr 28 '17 10:04 gazpachu

csillag thanks a lot :+1: actually downgrade to 2.8.1 and it worked

gabouh avatar Jul 03 '17 05:07 gabouh

Downgrading to [email protected] and [email protected] works but the following warning occurs Warning: [react-router] Router no longer defaults the history prop to hash history. Please use the hashHistory singleton instead. http://tiny.cc/router-defaulthistory

adithyamaheshb avatar Oct 08 '17 02:10 adithyamaheshb

Downgrading won't work with React 16 as prop-types is no longer included in React

jesster2k10 avatar Oct 31 '17 01:10 jesster2k10

@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 [email protected].

mojave27 avatar May 06 '18 16:05 mojave27

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",

mklaudia avatar Jun 07 '18 21:06 mklaudia

'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>
)

sky-gg avatar Nov 20 '18 08:11 sky-gg

For me the the problem was specifying "Component" instead of "component" (with a lower case )

nateshmbhat avatar Nov 23 '18 07:11 nateshmbhat

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 :)

krish5 avatar Nov 28 '18 11:11 krish5

thanks @sky-gg your solution work for me!!

enieber avatar Apr 10 '19 06:04 enieber

image image dont know where is wrong,who can find reason..thanks

406250613 avatar Aug 22 '19 06:08 406250613

could nt find solution .Plz let me know if anyone has solution

guptshruti2003 avatar Sep 25 '20 23:09 guptshruti2003