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.
Write router in place of Main in render (last line in the code). Like this ReactDOM.render(router, document.getElementById('root'));
For JEST users
if you use Jest for testing and this error happen just wrap your component with
<BrowserRouter>
Enclose Link component inside BrowserRouter component