Skip to content

Explicit error on <Route> outside <Router> #4939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 14, 2017
Next Next commit
Explicit error on <Route> outside <Router>
When using <Route> outside a valid <Router>, the message is not that explicit. It throws:

`Cannot read property 'route' of undefined`

Even though it's not that bad, I propose we make it even more explicit. Can't be bad to know where the error is coming from. If you made a typo when importing the router, it's not obvious otherwise.

This will throw an error rather than a warning because the library code would crash otherwise.
eXon authored Apr 9, 2017

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
commit b210ae5bb4e508ad939ccdcbcbb80a38ac1e8885
4 changes: 4 additions & 0 deletions packages/react-router/modules/Route.js
Original file line number Diff line number Diff line change
@@ -33,6 +33,10 @@ class Route extends React.Component {
}

getChildContext() {
if (!this.context.router) {
throw new Error('You should not use <Route> or withRoute() outside a valid <Router>');
}

return {
router: {
...this.context.router,