I'm trying to set up react-router in an example application, and I'm getting the following error:
You should not use <Link> outside a <Router>
My app is set up like so:
Parent component
const router = (
<div className="sans-serif">
<Router histpry={browserHistory}>
<Route path="/" component={Main}>
<IndexRoute component={PhotoGrid}></IndexRoute>
<Route path="/view/:postId" component={Single}></Route>
</Route>
</Router>
</div>
);
render(<Main />, document.getElementById('root'));
Child/Main
component
export default () => (
<div>
<h1>
<Link to="/">Redux example</Link>
</h1>
</div>
)
Any idea what I'm doing wrong here?
Here's a Sandbox link to demonstrate the problem.
Make it simple:
and don't forget:
import { BrowserRouter } from "react-router-dom";
I was getting this error because I was importing a reusable component from an npm library and the versions of
react-router-dom
did not match.So make sure you use the same version in both places!
I'm assuming that you are using React-Router V4, as you used the same in the original Sandbox Link.
You are rendering the
Main
component in the call toReactDOM.render
that renders aLink
and Main component is outside ofRouter
, that's why it is throwing the error:Changes:
Use any one of these Routers, BrowserRouter/HashRouter etc..., because you are using React-Router V4.
Router can have only one child, so wrap all the routes in a
div
or Switch.React-Router V4, doesn't have the concept of nested routes, if you wants to use nested routes then define those routes directly inside that component.
Check this working example with each of these changes.
Parent Component:
Main
component from routeEtc.
Also check this answer: Nested routes with react router v4
I kinda come up with this code :
I think the error was because you were rendering the
Main
component, and theMain
component didn't know anything aboutRouter
, so you have to render its father component.If you don't want to change much, use below code inside onClick()method.
Whenever you try to show a
Link
on a page thats outside theBrowserRouter
you will get that error.This error message is essentially saying that any component that is not a child of our
<Router>
cannot contain any React Router related components.You need to migrate your component hierarchy to how you see it in the first answer above. For anyone else reviewing this post who may need to look at more examples.
Let's say you have a
Header.js
component that looks like this:And your
App.js
file looks like this:Notice that the
Header.js
component is making use of theLink
tag fromreact-router-dom
but the componet was placed outside the<BrowserRouter>
, this will lead to the same error as the one experience by the OP. In this case, you can make the correction in one move:Please review carefully and ensure you have the
<Header />
or whatever your component may be inside of not only the<BrowserRouter>
but also inside of the<div>
, otherwise you will also get the error that a Router may only have one child which is referring to the<div>
which is the child of<BrowserRouter>
. Everything else such asRoute
and components must go within it in the hierarchy.So now the
<Header />
is a child of the<BrowserRouter>
within the<div>
tags and it can successfully make use of theLink
element.