Invariant failed: You should not use <Route> outside a <Router>
Trouble with 2 instance of React
I had this problem whilst testing and solved it by wrapping my test component with Routers.
import React from 'react';
import ReactDom from 'react-dom';
import Header from '../components/Header/Header';
import { BrowserRouter } from 'react-router-dom';
it('renders Header without crashing', () => {
const div = document.createElement('div');
ReactDom.render(
<BrowserRouter>
<Header />
</BrowserRouter>,
div);
ReactDom.unmountComponentAtNode(div);
});
Jest, Enzyme: Invariant Violation: You should not use or , To test a component (with Jest) that contains and withRouter you need to import Router in you test, not in your component import { BrowserRouter as Invariant Violation: You should not use or withRouter() outside a According to react router 4 docs, the components are considered valid, where I can create components composed of s, then import them into another component and place inside a .
I solved this problem by changing:
import {Route, Switch} from "react-router";
to
import {Route, Switch} from "react-router-dom";
just add -dom.
I fixed that problem just importing the BrowserRouter
from react-router-dom
in index.js
and adding:
<BrowserRouter>
<App>
</BrowserRouter>
within:
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root'));