react-admin: Error: Invariant failed: You should not use outside a
When installing react-admin and including dependency "react-router-dom": "5.0.0"
to package.json, an error occurs when using customRoutes
.
Error: Invariant failed: You should not use
<Route>
outside a<Router>
Here’s the show case. https://codesandbox.io/s/n0wq5lxkp
I just burned a few hours to narrow down the react-admin app only to find out that even the bare minimum revealed the same error. I only needed to remove dependency "react-router-dom": "5.0.0"
from package.json to fix the error.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 42
- Comments: 22 (4 by maintainers)
I’m closing this issue immediately. If somebody else has the same problem in the future, a search engine and this issue will be their friend.
@daominhsangvn
Check your
package.json
at thedependencies
ordevDependencies
section. Do you see"react-router-dom": ...
in there? If so, remove that line, callyarn
(which is shorthand foryarn install
ornpm install
if you use npm) and re-run your app.Just remember: Before using {Route} from ‘react-router-dom’ library, just make sure you put
<Route>
inside of<BrowserRouter>
. If not you will get the same Error.The correct way to invoke <Route> should like that:
I would not recommend deleting react-router-dom from the package.json. unless you are not using it at all. its wiser to
and wrap Link inside of BrowserRouter
This could also happen specifically when you have not imported the BrowserRouter class from ‘react-router-dom’ inside of your index.js file. Ideally, you wrap your root component
<App>
inside of BrowserRouter inside of index.js file in your react project like so –import React from ‘react’; import ReactDOM from ‘react-dom’; import { BrowserRouter } from ‘react-router-dom’; import ‘./index.css’; import App from ‘./App’;
ReactDOM.render(
<BrowserRouter><App/></BrowserRouter>
, document.getElementById(‘root’));The error is correct. You need to wrap the Switch with BrowserRouter or other alternatives like HashRouter, MemoryRouter. This is because BrowserRouter and alternatives are the common low-level interface for all router components and they make use of the HTML 5 history API, and you need this to navigate back and forth between your routes.
Try doing this rather
import { BrowserRouter, Switch, Route } from ‘react-router-dom’; And then wrap everything like this
<BrowserRouter> <Switch> //your routes here </Switch> </BrowserRouter>
Remove react-router-dom from package .json file and do npm install
@christiaanwesterbeek
How are you using the
Route
component without having thereact-router-dom
depdendency? Am I missing something here?Besides, do remember:
Hi, how can i get rid of this ? I having similar issue when trying to add a custom route.
This was the only thing that worked for me as well. Had to change it to
"react-router-dom": "5.1.2"
inpackage.json
. Wrapping the main<App>
in a<BrowserRouter>
wasn’t necessary. It seems that react-admin is using its own connected-router somewhere deeper in the Admin component tree.This error could be caused, when you use npm link somepackage to locally link a package w/ react-router-dom installed too.
Possible fix (w/ reat-app-rewired):
Some more info.
I fix it by:
checking the version in
react-admin
byyarn list react-router-dom
then, change your version of
react-router-dom
to the same asreact-admin
one by editing yourpackage.json
type
yarn install
DONE!!!
Reinforcing what @kimkablitz wrote. DO NOT use both (react-router and react-router-dom) choose one. I particular prefer react-router-dom because I use Link component.
For those who get Switch issue after December 2021 :- Alternate solution : 1). Uninstall Previous Version- npm uninstall react-router-dom 2). Install Older version- npm install react-router-dom@5.0.0
go to index.js and wrap your <App /> in Browse router like this <BrowserRouter><App/></BrowserRouter> and import import {BrowserRouter} from ‘react-router-dom’;
before that make sure you’ve installed ‘react-router-dom’
For me react-router and react-router-dom just dont work the same way, check your packages version and compatibility.