-
-
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
Error: Invariant failed: You should not use <Route> outside a <Router> #3078
Comments
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. |
Hi, how can i get rid of this ? I having similar issue when trying to add a custom route. |
Check your |
Besides, do remember:
|
Just remember: The correct way to invoke should like that:
|
How are you using the |
Because react-admin itself has a |
@christiaanwesterbeek if in your source, you are importing |
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
|
import React, { Component } from "react";
import { BrowserRouter, Switch, Route } from "react-router-dom";
import SearchComponent from "./search";
import LoginForm from "./loginform";
class App extends Component {
render() {
return (
<React.Fragment>
<BrowserRouter>
<div>
<Route path="/search" component={SearchComponent} />
<Route path="/loginform" component={LoginForm} />
</div>
</BrowserRouter>
</React.Fragment>
);
}
}
export default App; Remove react-router-dom from package .json file and do npm install |
I fix it by: checking the version in then, change your version of type DONE!!! |
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';
|
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 import React from 'react'; ReactDOM.render( |
For me react-router and react-router-dom just dont work the same way, check your packages version and compatibility. |
Reinforcing what @kimkablitz wrote. |
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):
|
i dont have devDependencies |
This was the only thing that worked for me as well. Had to change it to |
it worked |
go to index.js and wrap your in Browse router like this and import import {BrowserRouter} from 'react-router-dom'; before that make sure you've installed 'react-router-dom' |
For those who get Switch issue after December 2021 :- |
What worked for me: |
When installing react-admin and including dependency
"react-router-dom": "5.0.0"
to package.json, an error occurs when usingcustomRoutes
.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.The text was updated successfully, but these errors were encountered: