Cannot read property 'listen' of undefined
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>
)
Do you have the appropriate versions of stuff?
Please use Stack Overflow or Reactiflux for support. This is a bug tracker.
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
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.
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
});
}
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.
had the same problem.
"react-router": "^4.0.0"
"react-router-redux": "^4.0.8"
it is working with this Combination:
"react-router": "^3.0.2",
"react-router-redux": "^4.0.8",
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.
You can fix it by importing createBrowserHistory from 'history':
import { createBrowserHistory } from 'history';
const store = createStore(...);
const history = syncHistoryWithStore(createBrowserHistory(), store);
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%.