react-router: The context `router` is marked as required in `Link`, but its value is `undefined`.

Guys, I need your help!

I already searched about this bug in many sites, including documentation for react-router.

I was implementing likewise the documentation recommends. But, this error persists.

Imports

import { 
    BrowserRouter as Router,
    Switch,
    Route,
    Link
} from 'react-router-dom';

Menu

<Link to="/" className="pure-menu-link">Home</Link>
<Link to="/author" className="pure-menu-link">Author</Link>
<Link to="/book" className="pure-menu-link">Book</Link>

Routes definition

<Router>
    <Switch>
	<Route exact path="/" component={Home} />
	<Route path="/author" component={AuthorBox} />
        <Route path="/book" component={BookBox} />
    </Switch>
</Router>

Error The context router is marked as required in Link, but its value is undefined.

Thanks for your attention.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 15 (5 by maintainers)

Most upvoted comments

@lucascavalcante Just make sure you’re rendering your <Link>s somewhere in the tree underneath your <Router> (i.e. in one of your <Route component> components). This won’t work:

<div>
  {/* This link needs to be a descendant of the <Router>. To fix, move the <Router> to the root! */}
  <Link></Link>
  <Router>
    ...
  </Router>
</div>

Anyone have a solution to this in terms of testing? Rendering your components within the context of your application Router is no issue when running the application, but is more difficult to do when you are writing tests for components. We shouldn’t have to render out the whole app to run a unit test on a component that contains a Link, no?

It works for me by the following lines of code

import {MemoryRouter} from `'react-router-dom'
<MemoryRouter>
 <MyComponent />
</MemoryRouter>

<Router>

https://github.com/gildata/RAIO/issues/41

<Router> must be the top parent of all <Link>s.

@mjackson

You did a Good Job!

Please verify that you have version 4.0.0 of react-router and react-router-dom installed in your node_modules.

Basically,

BrowserRouter must be the top parent of all Links. In new versions seems you need a layout where to put your menu with your links Example

<BrowserRouter>
	<Layout>
		<Switch>
			<Route exact path="/" component={Homepage} />
			<Route path="/resources" component={Resources} />
			<Route component={Error404} />
		</Switch>
	</Layout>
</BrowserRouter>`

LAYOUT

const Layout = props => ({
	render() {
		return (
			<div className="o-container">
				<Header />
				<main>{props.children}</main>
				<Footer />
			</div>
		);
	}
});
export default Layout;`

HEADER

import React, { Component } from 'react';
import { Link } from 'react-router-dom';
class Header extends Component {
	authButton() {
		return <button> Sign In </button>;
	}

	render() {
		return (
			<nav className="navbar navbar-light">
				<ul className="nav navbar-nav">
					<li className="nav-item">
						<Link to="/">Home</Link>
					</li>
					<li className="nav-item">
						<Link to="/resources">Resources</Link>
					</li>
					<li className="nav-item"> {this.authButton()} </li>
				</ul>
			</nav>
		);
	}
}
export default Header;

@lucascavalcante What doesn’t work? Going back to beta versions isn’t really a solution now that final is released.

Well I would recommend updating react-router-dom to 4.0.0 now that it is out of beta.

I’ve seen other people have context issues, but that was typically with earlier versions of react-router where the context was different.

If updating the dom package doesn’t fix the issue, I would reset everything with a rm node_modules && npm install. Sometimes that folder seems to have a chaotic mind of its own.