Skip to content
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 after update to react-router 4.4.0 #6634

Closed
ghost opened this issue Mar 16, 2019 · 24 comments
Closed

Invariant violation after update to react-router 4.4.0 #6634

ghost opened this issue Mar 16, 2019 · 24 comments

Comments

@ghost
Copy link

ghost commented Mar 16, 2019

Version

4.4.0

Steps to reproduce

webpack build
run with wepback-dev-server
Get Invariant violation: cannot use Route or wirhRouter() outside of Router

Expected Behavior

No error, as nothing else changed but version of react-router and react-router-dom packages

Actual Behavior

index.js:19 Uncaught Invariant Violation: You should not use <Route> or withRouter() outside a <Router>
    at e.exports (http://localhost:3026/bundle.js:60529:55135)
    at a.computeMatch (http://localhost:3026/bundle.js:60607:26236)
    at new a (http://localhost:3026/bundle.js:60607:25553)
    at constructClassInstance (http://localhost:3026/bundle.js:208481:18)
    at updateClassComponent (http://localhost:3026/bundle.js:211807:5)
    at beginWork (http://localhost:3026/bundle.js:212764:16)
    at performUnitOfWork (http://localhost:3026/bundle.js:216432:12)
    at workLoop (http://localhost:3026/bundle.js:216472:24)
    at HTMLUnknownElement.callCallback (http://localhost:3026/bundle.js:197315:14)
    at Object.invokeGuardedCallbackDev (http://localhost:3026/bundle.js:197365:16)
@darkadept
Copy link

I just discovered the exact same problem with my code.

@pshrmn
Copy link
Contributor

pshrmn commented Mar 16, 2019

That error should happen when you're using the <Route> in the wrong place.

Assuming that the <Route> is in the right place (e.g. you're updating a package that worked fine in 4.3), it is possible that you currently have multiple versions of dependencies installed, which could cause context issues. Can you try doing a full re-install (deleting node_modules first).

@joeltaylor
Copy link

It may be worth double checking your versions. I got surprised by an unintended upgraded to 4.4.0 (expected 4.2.2) due to how react-router requires its dependencies that caused a similar error.

@darkadept
Copy link

My problem could be that I have multiple versions of react-router-dom. I will investigate further.

@StringEpsilon
Copy link
Contributor

StringEpsilon commented Mar 16, 2019

Using multiple versions of react-router and or react-router-dom will definitely cause issues like that.

You can't use react-router 4.3 with react-router-dom 4.4 or vice versa. (Edit: writing it out like that: Why isn't that considered a breaking change?)

Your build process might also cause some issues in that regard. Particularly if you have imports pointing at both "react-router" and "react-router-dom" in your codebase. Compare with #6581.

@ghost
Copy link
Author

ghost commented Mar 17, 2019

I have react-router and react-router-dom in my project and two packages used, npm update makes this dependency updated everywhere but I think that problem actually in built packages, those bundles built on previous version therefore indeed it can be version conflict.

@kostozy
Copy link

kostozy commented Mar 17, 2019

React router dom (v4.3.1) requires react-router v4.4.0. But in the main package react-router v is 4.3.1. So, my solution is to change version of react-router in shrinkwrap to 4.3.1. Please check dependency versions

@nick
Copy link

nick commented Mar 17, 2019

I fixed this by ensuring I imported withRouter from 'react-router-dom' everywhere in my code. Before I had been importing from 'react-router' in some places and 'react-router-dom' in others.

@leoyli
Copy link

leoyli commented Mar 18, 2019

I tried to search the changelog, but looks the doc haven't catch up?!

@zaklampert
Copy link

browser.js:38 Uncaught Invariant Violation: You should not use <Link> outside a <Router>
...
The above error occurred in the <Context.Consumer> component:
    in Link
    in div
    in Qc (created by App)
    in Router (created by BrowserRouter)
    in BrowserRouter (created by App)
...

Link is a child of BrowserRouter in my app, as you can see from the second error. Using react-router and react-router-dom 4.4.0, react and react-dom 16.8.4

@ghost
Copy link
Author

ghost commented Mar 18, 2019

@zaklampert

npm list react-router react-router-dom 

and then, if by chance you reside on *nix

 cd mySource; grep -rnw . -e 'withRouter' 

@mjackson
Copy link
Member

I'm pretty sure this is a duplicate of #6630. Your react-router-dom package is probably pulling in react-router 4.4.0. I have already described some workarounds that you can take for now, but we are working on a more permanent fix.

Let's follow up there.

@mccambridge
Copy link

I'm getting this error when I import a component from one of our internal packages that contains a Route or Link component. Though this component is located externally, it's always a descendant of our component tree way under the Router component. If I move the component to a local file in the project, suddenly it works without a single code change. This is causing me all kinds of problems. Not sure why the Invariant violation would fire in this case.

@alexanderturinske
Copy link

alexanderturinske commented Mar 19, 2019

I am seeing the same thing as @mccambridge; Upgrading to 5.0.0 did not fix the issue; locking to 4.3.1 fixes the issue. Verified that I was not mixing import statements, as described in the 4.4.0-beta.1 release notes

@ghost
Copy link
Author

ghost commented Mar 27, 2019

Same as @mccambridge experience same kind of error, after updating both packages and main project react-router and react-router-dom version to 5.0.0. Returning back to 4.3.1 make things working.

@taschetto
Copy link

@mccambridge @alexanderturinske @Cherviakov I've fixed it by deleting node_modules and doing a yarn (or npm install if you will). Using v5.0.0 of react-router-dom.

@ghost
Copy link
Author

ghost commented Apr 26, 2019

@taschetto you using ReactRotuer in your main project and in written by you packages, which you use in your project? It is working normally in single project, but when you add packages then seems somehting goes wrong between reactRouterv.@5.0.0 in main project and in package. This is why in our case we have to fallback to v4.3.1

@simoncoulton
Copy link

I still had this occurring on v5, had to downgrade to 4.3.1 to get it working again as well.

@ViggoV
Copy link

ViggoV commented May 7, 2019

I am having this issue after updating to react-router@5.0.0. It happens when I have a subroute in a project which is imported from a submodule. Both projects are using the same react (16.8.6) and react-router-dom (5.0.0) versions. @taschetto your solution didn't work but did appear to change the error message so it says rather than , which is more correct in this case so progress I guess..

@josephmcasey
Copy link

Ditto here, upgrading from 4.3.1 to 5.0.0 without making any changes to the project caused:
Invariant failed: You should not use <Switch> outside a <Router>

I'm using connected-react-router in place of my <Router/>.

@ViggoV
Copy link

ViggoV commented May 13, 2019

I ended up using npm locally instead of submodules and then added react-router as a peer dependency in my sub-project, if that helps anyone.

@chulanovskyi
Copy link

For everyone, who are trying to use the exact versions (4.3.1) as a workaround and still have their builds failing - please make sure you are NOT using npm to install packages. Use yarn.
Just to clarify, while having the exact "react-router-dom": "4.3.1" and the:

"resolutions": {
    "react-router-dom": "4.3.1",
    "react-router": "4.3.1"
  },

in my *package.json`, I've tried both options:

  • when you use npm i it will end up installing the react-router 4.4 version, disregarding the resolutions config. Because in the package.json of the react-router-dom it has a caret near the react-router dependency - "^4.3.1". Here is the resulted package.json of the resolved react-router

  • when you use yarn - the proper version that is described in the resolutions sections is installed.

@crobinson42
Copy link

crobinson42 commented Jun 7, 2019

@mccambridge I'm having the same issue when upgrading from 4.3.1 to 5.0.1 when a common components package is imported that has a component that contains a NavLink it breaks everything with Uncaught Invariant Violation: You should not use <Route> or withRouter() outside a <Router> - did you resolve this in any way?

I opened #6769

@WeiGrand
Copy link

WeiGrand commented Jun 8, 2019

For everyone, who are trying to use the exact versions (4.3.1) as a workaround and still have their builds failing - please make sure you are NOT using npm to install packages. Use yarn.
Just to clarify, while having the exact "react-router-dom": "4.3.1" and the:

"resolutions": {
    "react-router-dom": "4.3.1",
    "react-router": "4.3.1"
  },

in my *package.json`, I've tried both options:

  • when you use npm i it will end up installing the react-router 4.4 version, disregarding the resolutions config. Because in the package.json of the react-router-dom it has a caret near the react-router dependency - "^4.3.1". Here is the resulted package.json of the resolved react-router
  • when you use yarn - the proper version that is described in the resolutions sections is installed.

Works for me, thanks

@lock lock bot locked as resolved and limited conversation to collaborators Aug 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests