I have a problem that I don\'t know how to solve, I get this error when running npm test
Invariant Violation: You should not use
Always put BrowserRouter in the navegations components, follow the example:
import React, { Component } from 'react'
import { render } from 'react-dom'
import { BrowserRouter, Route, NavLink, Switch } from 'react-router-dom'
var Componente1 = () => (<div>Componente 1</div>)
var Componente2 = () => (<div>Componente 2</div>)
var Componente3 = () => (<div>Componente 3</div>)
class Rotas extends Component {
render() {
return (
<Switch>
<Route exact path='/' component={Componente1}></Route>
<Route exact path='/comp2' component={Componente2}></Route>
<Route exact path='/comp3' component={Componente3}></Route>
</Switch>
)
}
}
class Navegacao extends Component {
render() {
return (
<ul>
<li>
<NavLink to="/">Comp1</NavLink>
</li>
<li>
<NavLink exact to="/comp2">Comp2</NavLink>
</li>
<li>
<NavLink exact to="/comp3">Comp3</NavLink>
</li>
</ul>
)
}
}
class App extends Component {
render() {
return (
<BrowserRouter>
<div>
<Navegacao />
<Rotas />
</div>
</BrowserRouter>
)
}
}
render(<App/>, document.getElementById("root"))
Note: the BrowserRouter accept only one children element.
I was facing the same issue. It's resolved when I "import BrowserRouter from react-router-dom" and write code
<BrowserRouter>
<Switch>
//your routes here
</Switch>
</BrowserRouter>
Error: Invariant failed: You should not use <Link>,<Switch>,<Route> outside a <Router>[enter image description here][1]
[Error: Invariant failed: You should not use <Link>,<Switch>,<Route> outside a <Router> ][1]
1. Open Index.js or Index.jsx
2. add-> import { BrowserRouter } from "react-router-dom";
3.Rap <App /> in <BrowserRouter> and </BrowserRouter>
should look like :
ReactDOM.render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>,
document.getElementById("root")
);
The way I did was updating the dependencies with yarn add react-router-dom
or npm install react-router-dom
and deleting the node_modules folder and running yarn
or npm install
again.
Make sure to have correct imports in all nested components. You might get that error if one of them imports Switch from react-router instead of react-router-dom. Keeping everything consistent with 'react-router-dom' (that reexports react-router components anyway). Checked with:
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
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?)
Make sure you will have same versions