I use react-router-dom
for routing in my React
application. Part of my app extracted in another package. List of dependencies looks like this:
./app/dashboard/package.json
{ "dependencies": { "@app/components": "^1.0.0", "react": "^16.8.5", "react-dom": "^16.8.5", "react-router-dom": "^5.0.0" } }
./app/components/package.json
{ "peerDependencies": { "react-router-dom": "^5.0.0" } }
When I use components from @app/components
which require components from react-router-dom
I getting this errors:
Uncaught Error: Invariant failed: You should not use <Route> outside a <Router> The above error occurred in the <Context.Consumer> component: Uncaught (in promise) Error: Invariant failed: You should not use <Route> outside a <Router>
Why throws this error? In App.js
I use BrowserRouter
import React, { Suspense } from 'react'; import { Switch, Route } from 'react-router-dom'; import { Placeholder } from '@app/components'; const Auth = React.lazy(() => import(/* webpackPrefetch: true */ './pages/Auth')); const Index = React.lazy(() => import(/* webpackPrefetch: true */ './pages/Index')); const App = () => ( <Suspense fallback={<Placeholder />}> <Switch> <Route path="/auth" component={Auth} /> <Route path="/" component={Index} /> </Switch> </Suspense> ); export default App;
client.jsx
import React from 'react'; import { render } from 'react-dom'; import { BrowserRouter } from 'react-router-dom'; import App from './App'; render( <BrowserRouter> <App /> </BrowserRouter>, document.getElementById('root'), );
To fix the 'You should not use Route or withRouter() outside a Router' error with React Router v4, we should wrap our app with the BrowserRouter component. import { BrowserRouter } from "react-router-dom"; ReactDOM. render( <BrowserRouter> <App /> </BrowserRouter>, document. getElementById("root") );
You should not use route or withrouter() or Link outside a router. React router throws the error, “you should not use route or withrouter() outside a router” when you have not defined BrowserRouter and still using Link or NavLink or Router components.
React Router has an higher-order component called withRouter with which we can pass in the React Router's history, location, and match objects to our React components as props. To use withRouter , you should wrap your App component inside withRouter() as a parameter.
Route render methodsThere are, however, a few other methods you can use to render something with a <Route> . These are provided mostly for supporting apps that were built with earlier versions of the router before hooks were introduced. You should use only one of these props on a given <Route> .
I solved this problem by changing:
import {Route, Switch} from "react-router";
to
import {Route, Switch} from "react-router-dom";
just add -dom.
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 .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With