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)
@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: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
<Router>https://github.com/gildata/RAIO/issues/41
@mjackson
Please verify that you have version
4.0.0ofreact-routerandreact-router-dominstalled in yournode_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
LAYOUT
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-domto4.0.0now that it is out of beta.I’ve seen other people have context issues, but that was typically with earlier versions of
react-routerwhere 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.