React router v4 not working with Redux

后端 未结 2 1648
迷失自我
迷失自我 2020-12-25 13:31

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 change

2条回答
  •  借酒劲吻你
    2020-12-25 13:54

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

提交回复
热议问题