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
Prev Previous commit
Next Next commit
Switch throw to invariant for <Router> check
  • Loading branch information
eXon committed Apr 11, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit f6b0325ab7ae4ceb7557fb48111acdcfe9313c61
8 changes: 5 additions & 3 deletions packages/react-router/modules/Route.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import warning from 'warning'
import invariant from 'invariant';
import React from 'react'
import PropTypes from 'prop-types'
import matchPath from './matchPath'
@@ -53,9 +54,10 @@ class Route extends React.Component {
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>')
}
invariant(
router,
'You should not use <Route> or withRouter() outside a valid <Router>'
)

const { route } = router
const pathname = (location || route.location).pathname
2 changes: 1 addition & 1 deletion packages/react-router/modules/__tests__/Route-test.js
Original file line number Diff line number Diff line change
@@ -96,7 +96,7 @@ describe('A <Route>', () => {
ReactDOM.render((
<Route path="/" render={() => null} />
), node)
}).toThrow(/You should not use <Route> or withRoute\(\) outside a valid <Router>/)
}).toThrow(/You should not use <Route> or withRouter\(\) outside a valid <Router>/)
})
})