-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
withRouter: Change of location doesn't trigger a re-render #5037
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
React Router intentionally does not use subscribers. There is a somewhat long article I wrote about why here https://medium.com/@pshrmn/ditching-subscriptions-in-react-router-6496c71ce4ec, but the gist is that subscriptions end up making the code base overly complicated without much benefit. You end up having to write your own scheduler to ensure that you don't get duplicate re-renders for one location change. Context usage also ends up a bit hacky because you have to manipulate the same object ( Instead, React Router relies on updates propagating from the router to all of its children. If you are using update blockers ( |
When a deep component uses
withRouter
, because of how React handles context (facebook/react#2517), the component is not updated when the location changed.The main reason is because the
location
value is directly provided by the context. When this value change on the top level component, the deep component doesn't re-render if one component of the stack has ashouldComponentUpdate
.One solution may be to provide a constant subscribe function in the context instead of the value it self.
Current solution:
react-router/modules/Route.js#L36
Proposed solution
Instead of having
router
in the context, provide asubscribeRouter
function. This is a solution similar to whatreact-sticky
is doing.Here is an example:
If you agree with the proposed solution, I can try a PR.
The text was updated successfully, but these errors were encountered: