-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Invariant violation related to react-router / react-router-dom non-peer dependency #3821
Comments
Thanks for the detailed (and very clear) explanation. For the peer dependencies, the fix was already committed in #3763 - I mean, the fix we're willing to accept without making the installation more painful. As for using |
@fzaninotto happy to, but it'll be a week or so as I'm on anotehr project for a little while :) |
OK @fzaninotto PR complete and testing for the imports aspect. It's worth noting that #3763 is not a fix to this issue. It tidies up the imports nicely (and makes it less likely that a version mismatch will occur within RA itself) but the dependency is simply moved into the However, I understand that adding peer dependencies makes installation a little harder, which is not aligned with the "out of the box" philosophy of The options for progressing are either:
The choice is yours as maintainer! I'll totally understand either way. For my $0.02, though, not fixing issues of this nature is a step away from being able to claim "Can be included in another React app" (which is probably a bit much if it makes routing the rest of the app app extremely hard/impossible/flaky). What do you think? |
First of all, thanks for your PR for react-router optimization. I won't go into the peerDependency debate again but just to help @thclark:
We will use strategy (1) for our applications and we will use strategy (2) for our react-admin related and distributed modules. I am still waiting for a release of V3 that include #3763, would you mind doing a beta.1 soon @fzaninotto ? |
Aha, ok thankyou @kopax that's a pretty robust approach, I think. Those ways forward, plus this issue being searchable, should reasonably cover this edge case for other users. |
You are welcome, this should be explained in the documentation at some point. |
fixed by #3825 |
First up. I'm known for concise and helpful issues. But this one gets a bit tortuous. Sorry about that. Hopefully the proposed solution is simple.
Overview
I have an issue closely related to, but not the same as #3018 and its dupe #3019. It's caused by the way in which
react-router
andreact-router-dom
access their internal context (the authors are humming about a better way here but that's too late to save my Monday :( .#3078 is very closely related (almost certainly the same issue), but has been closed without identifying the root cause, and doesn't highlight the case where you actually import
react-router
orreact-router-dom
elsewhere outside<Admin>
. It's left quite a few people hanging, not understanding why they can't havereact-router-dom
in theirpackage.json
.What you were expecting:
To be able to use a
<Link>
component, fromreact-router-dom
, in a page whose<Route>
is outside of<Admin>
, but in a valid router.What happened instead:
I get a variety of errors similar to:
even though things are inside valid routers.
Steps to reproduce:
This seems to be very much an edge case, dependent on npm's rules for resolving package dependencies, but I believe the following steps should reproduce:
react-router-dom
(I used 5.1.2) to your app'spackage.json
<Admin .../>
component in a<Route/>
, which itself resides in a<Switch/>
inside a valid<ConnectedRouter/>
.Admin
alongside other stuff.<Link>
in a different page outside the admin route.react-router-dom
explicitlyI'm sorry, to make this work on a prototype deadline I just ripped out all
<Link>
s and references toreact-router-dom
elsewhere on my site, so it's chunk of work to go back. But I expect I'll encounter this again so the next time, I'll attach mypackage-lock.json
to try and help show what's going on.In depth
In the case of #3018, there was a clash of version between
react-router
andreact-router-dom
- put simply, they couldn't see each other's routes. That was back when RA was using v4.x.x of RR and RRD.This commit back in June increased RA's major version of these dependencies to 5.x.x... meaning they now use a context pair internally to decipher what routes they're on.
The upshot of it is: you need RR/RRD to be using the same context instance throughout the entire app. Multiple contexts arise most commonly from having multiple installations (sometimes even of the same version!).
The same 3018 problem, in effect, is now happening in cases where multiple libraries that provide
<Route>
s import a different instance of react-router's context.Workaround
To break out of this, you either:
CustomApp.md
and completely remove Admin altogether (fully customised app, which kinda defeats the point!)react-router-dom
everywhere else in your library, deletereact-router
andreact-router-dom
from yourpackage.json
,rm -rf node_modules
,rm package-lock
,npm install
. Not fun if you're trying to also render a landing page or similar!Proposed solution
Is pretty simple. Hurrah!
Import from
react-router-dom
, instead ofreact-router
throughout RA. It's essentially identical (it re-exports everything fromreact-router
, with extras). This is in-line with the (admittedly ridiculously hard to find) recommendation of the RR/RRD team that we should almost certainly be using RRD directly, and stops multiple contexts arising where RA imports from RR, but OtherCode has to import from RRD (i.e. stops RA from preventing third party libraries from using RRD components).Alter
react-router-dom
andreact-router
to be peer dependencies, rather than full dependencies, similar to this fix from Samson Crowley, which avoids risk of there being multiple instances of these libraries installed by npm (and therefore multiple contexts) if other libraries are doing the same hard exports, or where users are struggling to migrate to RA from existing apps.Environment
The text was updated successfully, but these errors were encountered: