question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'location' prop not changing in a component with 'withRouter'

See original GitHub issue

Version

4.1.2

Test Case

https://codesandbox.io/s/61m9v82r7n

Steps to reproduce

Navigate between Home and Home + query.

Fix

The problem can be fixed by changing Child from being a PureComponent to Component. This is very weird as it has absolutely no connection to either being a child of the <Route /> component, or being the one that is decorated with withRouter.

Expected Behavior

I expect DeepChild’s location prop to change whenever the URL changes.

Actual Behavior

DeepChild’s prop is not changing despite it being correctly connected to withRouter.

Note

The setup, together with the connect usage, is exactly how I encountered it in my project. I did not test the issue without using connect.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
macrozonecommented, Apr 6, 2018

When a component says it doesn’t need to be updated, it is also speaking for its children, so if a child needs to update when the location changes, the parent shouldn’t be blocking it.

i think that’s not true. A component should never assume whether its children are components or connected containers:

<PureComponent>
   <Component1 />
   <Component2 />
</PureComponent>

Wether Component1 or Component2 should ever rerender is of no concern of PureComponent. Redux handles this case correctly: if Component1 is connected, it rerenders its children whenver the mapped state changes.

But if Component1 uses withRouter hoc, it does not re-render on location changes. I would expect withRouter to behave similar to connect in this regard.

1reaction
oreqizercommented, Sep 12, 2017

I did read it, however I thought that wrapping a component in withRouter gets around any blocked updates

do I understand correctly that I need to wrap every single pure component in the hierarchy in withRouter or pass the location prop from the upmost parent so the changes propagate?

Read more comments on GitHub >

github_iconTop Results From Across the Web

'location' prop not changing in a component with 'withRouter'
The problem can be fixed by changing Child from being a PureComponent to Component . This is very weird as it has absolutely...
Read more >
reactjs - match and location prop not updating after link to
Let's consider a basic Page component, using match.params to link to the next page: import { Link } from 'react-router-dom'; export default ({ ......
Read more >
withRouter - React Router: Declarative Routing for React.js
withRouter does not subscribe to location changes like React Redux's connect does for state changes. Instead, re-renders after location changes propagate out ...
Read more >
The Hooks of React Router - CSS-Tricks
When using the component syntax, route props ( match , location and history ) are implicitly being passed on to the component. But...
Read more >
React router not updating component - Sport Castellano Reports
createElement to render the component passed to the component props. ... Nov 23, 2021 · withRouter is not working after updating react-router-dom to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found