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.

Add a warning about using different builds

See original GitHub issue

Before 4.4, you could do this w/out problems:

import MemoryRouter from 'react-router-dom/MemoryRouter';
import { Link } from 'react-router-dom';

<MemoryRouter>
  <Link /> // Error!
</MemoryRouter>

It’s not very obvious, but those two import statements may actually reference different builds. The first one is using our CommonJS build, and the second one may be using the ES modules or the CommonJS build, depending on how your build is setup and where you told it to find files from the react-router-dom package. If you’re using webpack (which you probably are) then it’s probably pulling from the ES modules build.

In the case where <Link> is coming from a different build (or any other component, e.g. <Route>), you will currently run into an error like this:

You should not render a <Link> outside a <Router>

This is because of the context object mismatch, which is a problem introduced by our using the new context API. Basically, each build has its own context object, and components from different builds can’t be used together.

For this reason, I think it’d be nice to provide a better warning for folks who might be pulling the files from different builds accidentally, like I was doing (see 6460fe0862c40b7f726cc3d47d03cc9bbde74ff4). Maybe something like:

You are using two different builds of React Router in the same application. To fix this, please make sure you always import from the same build. [link to a gist or some docs page that explains the issue more thoroughly].

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
jimthedevcommented, Oct 24, 2018

Is this also a concern for libraries that import {Link} from "react-router-dom"; and then are imported into an application which has its own router?

More specifically if I have a library of more complex components that contain various <Link> and <NavLinks inside them, how can an application import components from my library and use the correct context (the application router context) even though the code lives in a separate bundle?

2reactions
timdorrcommented, Oct 1, 2018

We had originally added the transform-imports function in #5095. The motivation was bundle sizes from duplicate modules, but this has an overlapping concern that’s now exposed as a header.

Regardless of the approach taken, we’ll want to make sure we keep the bundle size in mind. Fixing one will likely fix the other. It’ll be annoying, but I’d suggest setting up a test repo, npm linking in RR and RRD, and checking output sizes as you try various things.

Rollup has got good DCE built-in nowadays and Webpack respects the sideEffects: false key we added to the package.json’s. So, I think it should be easy enough to solve by just following best practices.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add a warning about using different builds #6361 - GitHub
You are using two different builds of React Router in the same application. To fix this, please make sure you always import from...
Read more >
How to access build warnings in a post-build event in Visual ...
There's another way to turn that warning into a hard error btw: add a post-build which runs your program :]. But again, thatdoesn't...
Read more >
How to add warnings and errors to your code using #warning ...
Think of #warning as being like a FIXME comment that automatically shows up in your build output. #error is mainly useful if you...
Read more >
Suppress compiler warnings - Visual Studio - Microsoft Learn
Choose Build, and go to the Errors and warnings subsection. In the Suppress warnings or Suppress specific warnings box, specify the error codes ......
Read more >
Build Errors and Warnings - Revenera Product Documentation
To resolve this build warning, ensure that at least one package is associated with the feature.
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