reactjs/react-router-redux

Error:cannot read property dispatch of undefined

adnan1naeem opened this issue · 5 comments

I am writing test cases for my app but as soon as i try to access the store for my app I am facing above error in file ConnectedRouter.js in react-router-redux library.The following code return statement
this.store.dispatch causes error.I am getting error inside ConnectedRouter function 38 line number: Error:cannot read property dispatch of undefined

CODE SNIPPET

functionConnectedRouter() {
 var _temp, _this, _ret;
 
    _classCallCheck(this, ConnectedRouter);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.handleLocationChange = function (location) {
      _this.store.dispatch({  // I am getting error over here 
        type: _reducer.LOCATION_CHANGE,
        payload: location
      });
    }, _temp), _possibleConstructorReturn(_this, _ret); }

My test is passed when i add following if condition :

if(_this.store){   
return  _this.store.dispatch({    
   type: _reducer.LOCATION_CHANGE,
        payload: location
      });} 

This is a bug tracker, not a support system. For usage questions, please use Stack Overflow or Reactiflux where there are a lot more people ready to help you out. Thanks!

@timdorr let me know if i can implement this issue and send you a PR

@timdorr It seems a bug to me in the code as I mentioned above; if it is not a bug, could you just guide me in the right direction, I also put the question on reactiflux but did not get any answer.

You would only get that error if you're not providing a store to ConnectedRouter, either as a prop or via Provider. The error is on your end.

Solution for this is likely here: apollographql/react-apollo#1360

Check if you are running react-apollo and ConnectedRouter. There are some version differences.

You may need:

// note that store is in child context
<ApolloProvider client={client}>
    <ConnectedRouter store={store} history={history}>
        <MyApp />
    </ConnectedRouter>
</ApolloProvider>