4.0.0-beta.8
Node: 6.6.9
Ping me if you really want one. I can probably deploy something on heroku since this test depends on the browser url and I don't have access to it in jsbin.
{ this.props.location.pathname } in the html.I expect to see the component rerender on a history state change, but this doesn't seem to happen.
Component does not rerender. You can see this by adding a console.log in the render or using the jsx method described above.
This seems to work when rolling back react-router and react-router-dom to 4.0.0-beta.6. The withRouter then does have history context and works as intended. I'm not sure but it seems as if beta.8 introduced a bug.
You are either using shouldComponentUpdate (directly or indirectly through something like react-redux or mobx-react) or React.PureComponent. These are blocking your application from fully updating on location changes. Please see the blocked updates guide for more information.
@pshrmn can we reopen this and check out PR #4684. I'd love to add to the documentation about this issue. It was pretty non-intuitive but after your explanation, I think it would be worth adding to the docs.
This is really confusing.
I'm constantly hitting this issue and the workarounds seem to be ugly. I often have an intermediate component which implements shouldComponentUpdate via PureComponent and I have go and make it aware of the router even if it does not use the router at all.
Why the withRouter HOC or the Route component cannot directly listen to the location changes? Bit like connect() in react-redux listens to the store changes via context. It does not break when put as a child of an pure component.
I also find the blocked-updates.md documentation weird because the proposed solution is to add withRouter but it is itself affected the by the issue.
It's impossible for me to make every pure component aware of the react-router so I made following workaround:
class ListeningRoute extends React.Component {
componentWillMount() {
this.unlisten = this.props.history.listen(location => {
this.setState({location});
});
}
componentWillUnmount() {
this.unlisten();
}
render() {
return <Route {...this.props} location={this.state.location} />;
}
}
ListeningRoute = withRouter(ListeningRoute);
This ListeningRoute component works exactly like the default Route but it does render when it's a child of an pure component.
This works because the Route component can get the location pathname from the location prop as well as from the router context:
UPDATE: Let's continue in https://github.com/ReactTraining/react-router/issues/4671
Most helpful comment
This is really confusing.
I'm constantly hitting this issue and the workarounds seem to be ugly. I often have an intermediate component which implements
shouldComponentUpdatevia PureComponent and I have go and make it aware of the router even if it does not use the router at all.Why the
withRouterHOC or theRoutecomponent cannot directly listen to the location changes? Bit likeconnect()in react-redux listens to the store changes via context. It does not break when put as a child of an pure component.I also find the blocked-updates.md documentation weird because the proposed solution is to add
withRouterbut it is itself affected the by the issue.