React router Link not causing component to update within nested routes

后端 未结 5 1403
栀梦
栀梦 2021-02-07 14:46

This is driving me crazy. When I try to use React Router\'s Link within a nested route, the link updates in the browser but the view isn\'t changing. Yet if I refresh the page t

5条回答
  •  误落风尘
    2021-02-07 14:51

    Could not get to the bottom of this, but I was able to achieve my goals with ComponentWillRecieveProps:

    componentWillReceiveProps(nextProps){
        if (nextProps.params.slug !== this.props.params.slug) {
            const {dispatch, params} = nextProps;
            PortfolioDetail.readyOnActions(dispatch, params, true);
        }
    }
    

    In other words, for whatever reason when I use React Router Link to link to a page with the SAME PARENT COMPONENT, it doesn't fire componentWillUnMount/componentWillMount. So I'm having to manually trigger my actions. It does work as I expect whenever I link to Routes with a different parent component.

    Maybe this is as designed, but it doesn't seem right and isn't intuitive. I've noticed that there are many similar questions on Stackoverflow about Link changing the url but not updating the page so I'm not the only one. If anyone has any insight on this I would still love to hear it!

提交回复
热议问题