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

Cannot read property 'listen' of undefined

Open reggi opened this issue 10 years ago • 10 comments

Unsure whats happening here. Just dropped in the example code from the readme

  unsubscribeFromHistory = history.listen(handleLocationChange);
                                  ^

TypeError: Cannot read property 'listen' of undefined
import React from 'react'
import { createStore, combineReducers, applyMiddleware } from 'redux'
import { Provider } from 'react-redux'
import { Router, Route, browserHistory } from 'react-router'
import { syncHistoryWithStore, routerReducer } from 'react-router-redux'

import Layout from './views/Layout'
import Guts, { reducer } from './views/Guts'
import GutsDev from './views/GutsDev'

const store = createStore(
  combineReducers({
    reducer,
    routing: routerReducer
  })
)

const history = syncHistoryWithStore(browserHistory, store)

module.exports = (
  <Provider store={store}>
    <Router history={history}>
      <Route path='/' component={Layout}>
        <Route path='/main' component={Guts} />
        <Route path='/main/dev' component={GutsDev} />
      </Route>
    </Router>
  </Provider>
)

reggi avatar Apr 05 '16 22:04 reggi

Do you have the appropriate versions of stuff?

Please use Stack Overflow or Reactiflux for support. This is a bug tracker.

timdorr avatar Apr 06 '16 14:04 timdorr

fyi I got this error when using ReactRouter.History.hashHistory

switching to ReactRouter.browserHistory or ReactRouter.hashHistory fixed it

an old "solution" was to make sure that you don't have an old (1.x) version of the History module

jasonswearingen avatar Oct 12 '16 23:10 jasonswearingen

I've got the same issue. I believe this is a universal rendering issue, but I could be wrong. The various StackOverflow answers to this problem have not been terribly helpful thus far. While this might not be a bug, I think it could be useful to have documentation regarding how to use (or that you can't use) react-router-redux in a universally rendered application.

kyleshevlin avatar Oct 27 '16 20:10 kyleshevlin

Just incase it helps anyone else with an isomorphic app, wrapping history.listen in an if block that checks if the code is running on the client works well:

const isClient = () => (
	typeof window !== 'undefined' && window.document;
);

if (isClient()) {
	const history = syncHistoryWithStore(browserHistory, store);

	history.listen(location => {
             // do stuff
	});
}

jamieallen59 avatar Dec 02 '16 10:12 jamieallen59

I had this issue too.

Just deleted node_models and reinstalled everything from scratch - fixed it. I guess the issue was a clash between the history in my dependencies and in the deps of react-router.

advance512 avatar Mar 11 '17 16:03 advance512

had the same problem.

    "react-router": "^4.0.0"
    "react-router-redux": "^4.0.8"

eldyvoon avatar Mar 12 '17 14:03 eldyvoon

it is working with this Combination: "react-router": "^3.0.2", "react-router-redux": "^4.0.8",

Isarstyle avatar Mar 13 '17 03:03 Isarstyle

You can also use the latest react-router-redux@^5.0.0 and react-router@^4.0.0, but it means you'll never to modify your usage a bit - using Switch, ConnectedRouter etc.

advance512 avatar Mar 13 '17 03:03 advance512

You can fix it by importing createBrowserHistory from 'history':

import { createBrowserHistory } from 'history';
const store = createStore(...);
const history = syncHistoryWithStore(createBrowserHistory(), store);

leftdevel avatar Mar 15 '17 07:03 leftdevel

Had this problem as well. Seems as though react-router 4.0.0 throws it when used with react-router-redux 4.0.8. Downgraded to react-router 3.0.2 and all works 100%.

mattfaircliff avatar Mar 16 '17 09:03 mattfaircliff