21

Developing a React application using React router v4. All worked well until I introduced Redux in my app. Since then on click of links to change route the browser url changes but the component corresponding to the route is not getting loaded. It works well if I comment out Redux code. What could be causing this? Here is my code for routing:

import React, { Component } from 'react';
import { Switch, Route, Link } from 'react-router-dom';
import LeftSubDefault from './../components/left-sub-default.component';
import LeftSubOne from './../components/left-sub-one.component';
import LeftSubTwo from './../components/left-sub-two.component';
import { withRouter } from 'react-router-dom';
import { connect } from "react-redux";
import { goToLeftDefault, goToLeftOne, goToLeftTwo } from "./../actions/leftRouteActions.js";

class LeftComponent extends Component {
  render() {
    return (
      <div className="col-xs-6">
          <p>
            Current sub route: {this.props.currentRoute}
          </p>
          <ul>
            <li onClick={this.props.goToDefault}><Link to={'/'}>Go To Default</Link></li>
            <li onClick={this.props.goToSub1}><Link to={'/left-sub1'}>Go To One</Link></li>
            <li onClick={this.props.goToSub2}><Link to={'/left-sub2'}>Go To Two</Link></li>
          </ul>
          <Switch>
            <Route exact path='/' component={LeftSubDefault} />
            <Route exact path='/left-sub1' component={LeftSubOne} />
            <Route exact path='/left-sub2' component={LeftSubTwo} />
          </Switch>
      </div>
    );
  }
}
const mapStateToProps = (store) => {
  return {
    currentRoute: store.routes.currentRoute
  };
}
const mapDispatchToProps = (dispatch) => {
  return {
    goToDefault: () => {
      dispatch(goToLeftDefault())
    },
    goToSub1: () => {
      dispatch(goToLeftOne())
    },
    goToSub2: () => {
      dispatch(goToLeftTwo())
    }
  };
}
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(LeftComponent));

PS: I get no error in console. The code runs clean just components don't load. Here is a similar thread on github: 4671. I have seen lot of threads on various sites but none has the solution for this issue.

JS dev
  • 8,456
  • 12
  • 68
  • 111
  • 1
    See [This Answer](https://stackoverflow.com/questions/43895805/react-router-4-does-not-update-view-on-link-but-does-on-refresh#answer-45036930), Exactly can help you. – AmerllicA Jul 05 '18 at 23:56
  • [A Redux binding for React Router v4](https://github.com/supasate/connected-react-router) – vsync Aug 31 '18 at 12:49

2 Answers2

57

Hah, now I'm making a project with react-router and redux too =).

Look at the official documentation about redux integration https://reacttraining.com/react-router/web/guides/redux-integration.

I think the main point is order of withRouter and connect Hocs.

The problem is that Redux implements shouldComponentUpdate and there’s no indication that anything has changed if it isn’t receiving props from the router. This is straightforward to fix. Find where you connect your component and wrap it in withRouter.

From the official docs.

UPD

import { withRouter } from 'react-router-dom';
import { connect } from 'react-redux';

class Home extends React.Component {...}

export default withRouter(
    connect(mapStateToPropsFunc)(Home)
);
Ivan Burnaev
  • 2,460
  • 13
  • 24
3

I'm using react-router-dom v4.1.1. It is working for me. Here is my Demo

import React from 'react';

import Reducer1 from 'yourReducer1';
import Reducer2 from 'yourReducer2';


import {
    Route,
    Switch as RouterSwitch
} from 'react-router-dom';

const App =()=> (
<RouterSwitch>
    <Route path="/link1" exact component={Reducer1}/>
    <Route path="/link2" exact component={Reducer2}/>     
</RouterSwitch>
);

export default App;

Hope it is helpful for you ^^

Voi Mập
  • 661
  • 2
  • 6
  • 21
  • what should i do to upgrade to 4.1.1? Any syntax changes in this version? – JS dev Jul 12 '17 at 11:36
  • just try: "npm update all". I have not used this :D. Hier ist Link [Update npm](https://www.npmjs.com/package/npm-update-all) – Voi Mập Jul 12 '17 at 11:46
  • Could yuu share your connect syntax please. That is where the issue lies. Please see my question updated. – JS dev Jul 12 '17 at 12:23
  • Here is my index^^ ReactDOM.render( , document.getElementById('root')); registerServiceWorker();' – Voi Mập Jul 12 '17 at 12:56
  • 2
    withRouter worked, thanks much! I was previously doing it in Leftcomponent where routing did not work. However I had to do it where the parent component was imported. LeftComponent was used in app.js so in app.js I had to use withRouter in app.js and not in LeftComponent js. – JS dev Jul 13 '17 at 04:07
  • okay :)). Congratulations to find out :P. Can you give me one vote :)). I have only 2 points to free comment :P – Voi Mập Jul 13 '17 at 06:17