Skip to content
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

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

Merged
merged 9 commits into from
Jul 14, 2017
11 changes: 6 additions & 5 deletions packages/react-router/modules/Route.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ 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,
Expand All @@ -53,10 +49,15 @@ class Route extends React.Component {
match: this.computeMatch(this.props, this.context.router)
}

computeMatch({ computedMatch, location, path, strict, exact }, { route }) {
computeMatch({ computedMatch, location, path, strict, exact }, router) {
if (computedMatch)
return computedMatch // <Switch> already computed the match for us

if (!router) {
throw new Error('You should not use <Route> or withRoute() outside a valid <Router>')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

withRouter not withRoute.

Also, shouldn't this be done with warning or invariant?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, yes, I would do this via an invariant. We can optimize those calls out for production builds that way.

}

const { route } = router
const pathname = (location || route.location).pathname

return path ? matchPath(pathname, { path, strict, exact }) : route.match
Expand Down