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

Error: Invariant failed: You should not use <Route> outside a <Router> #3078

Closed
christiaanwesterbeek opened this issue Apr 1, 2019 · 22 comments

Comments

@christiaanwesterbeek
Copy link
Contributor

christiaanwesterbeek commented Apr 1, 2019

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.

@christiaanwesterbeek
Copy link
Contributor Author

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
Copy link

Hi, how can i get rid of this ? I having similar issue when trying to add a custom route.

@christiaanwesterbeek
Copy link
Contributor Author

christiaanwesterbeek commented Apr 3, 2019

@daominhsangvn

Check your package.json at the dependencies or devDependencies section. Do you see "react-router-dom": ... in there? If so, remove that line, call yarn (which is shorthand for yarn install or npm install if you use npm) and re-run your app.

@talentAN
Copy link

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.

Besides, do remember:

  1. remove your lock file like (yarn.lock or npm.lock) and node_modules;
  2. run the yarn install or npm insall;
  3. recompile your codes, it will totaly work~ .

@NiMaGangFour
Copy link

NiMaGangFour commented May 8, 2019

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 should like that:

<BrowserRouter>
  <div>
   <Route path="/" component={} />
  </div>
</BrowserRouter>

@mbchoa
Copy link

mbchoa commented May 23, 2019

@christiaanwesterbeek

How are you using the Route component without having the react-router-dom depdendency? Am I missing something here?

@christiaanwesterbeek
Copy link
Contributor Author

Because react-admin itself has a react-router-dom dependency. Are you using react-admin? If so, your own app should depend on react-admin. react-admin depends on react-router-dom.

@kopax
Copy link
Contributor

kopax commented Jun 3, 2019

@christiaanwesterbeek if in your source, you are importing react-router-dom, then it should appear in your package.json, other way around is to let react-admin expose `react-router-dom itself. This is due to dependency management, that expect to have valid import listed within the package.json.

@cucumberslice
Copy link

cucumberslice commented Jul 11, 2019

I would not recommend deleting react-router-dom from the package.json. unless you are not using it at all. its wiser to

import { BrowserRouter, Link} from 'react-router-dom

and wrap Link inside of BrowserRouter

<BrowserRouter>
       <Link to="/" className="item">Cats</Link>
</BrowserRouter>

@jeyadharanigithub
Copy link

jeyadharanigithub commented Aug 20, 2019

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

@warren922
Copy link

I fix it by:

checking the version in react-admin by
yarn list react-router-dom

then, change your version of react-router-dom to the same as react-admin one by editing your package.json

type yarn install

DONE!!!

@vishnukgcherupuzha
Copy link

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>

@anil-mirge
Copy link

anil-mirge commented Nov 23, 2019

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'));

@kimkablitz
Copy link

For me react-router and react-router-dom just dont work the same way, check your packages version and compatibility.

@xtfs
Copy link

xtfs commented Jan 12, 2020

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.

@josias-r
Copy link

josias-r commented Jan 28, 2020

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):

const path = require("path");
const { override, addWebpackAlias } = require("customize-cra");

module.exports = override(
  addWebpackAlias({
    react: path.resolve("./node_modules/react"),
    "react-router": path.resolve("./node_modules/react-router"),
    "react-router-dom": path.resolve("./node_modules/react-router-dom"),
  }),
);

Some more info.

@kambleaa007
Copy link

i dont have devDependencies

@timotgl
Copy link

timotgl commented Jun 3, 2020

I fix it by:

checking the version in react-admin by
yarn list react-router-dom

then, change your version of react-router-dom to the same as react-admin one by editing your package.json

type yarn install

DONE!!!

This was the only thing that worked for me as well. Had to change it to "react-router-dom": "5.1.2" in package.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.

@williamlisci
Copy link

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'));

it worked

@aniketmarwade
Copy link

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'

@asura02
Copy link

asura02 commented Jan 6, 2022

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

@IChowdhury01
Copy link

What worked for me:
Find all instances of importing withRouter, BrowserRouter, or Route.
Make sure you're importing from react-router-dom and not react-router.
Remove react-router from package.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests