-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Relative <Route> and <Link> #4355
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,40 @@ | ||
import React from 'react' | ||
import React, { PropTypes } from 'react' | ||
|
||
const ExampleRouter = ({ children }) => ( | ||
children ? React.Children.only(children) : null | ||
) | ||
class ExampleRouter extends React.Component { | ||
|
||
static contextTypes = { | ||
router: PropTypes.object.isRequired | ||
} | ||
|
||
static childContextTypes = { | ||
router: PropTypes.object.isRequired | ||
} | ||
|
||
getChildContext() { | ||
return { | ||
router: this.router | ||
} | ||
} | ||
|
||
componentWillMount() { | ||
this.router = { | ||
...this.context.router, | ||
match: null | ||
} | ||
|
||
this.unlisten = this.router.listen(() => { | ||
this.router.location = this.context.router.location | ||
}) | ||
} | ||
|
||
componentWillUnmount() { | ||
this.unlisten() | ||
} | ||
|
||
render() { | ||
const { children } = this.props | ||
return children ? React.Children.only(children) : null | ||
} | ||
} | ||
|
||
export default ExampleRouter |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,12 @@ import React, { PropTypes } from 'react' | |
import matchPath from './matchPath' | ||
|
||
const computeMatch = (router, { computedMatch, path, exact, strict }) => | ||
computedMatch || matchPath(router.location.pathname, path, { exact, strict }) | ||
computedMatch || matchPath( | ||
router.location.pathname, | ||
path, | ||
{ exact, strict }, | ||
router.match | ||
) | ||
|
||
/** | ||
* The public API for matching a single path and rendering. | ||
|
@@ -51,7 +56,8 @@ class Route extends React.Component { | |
|
||
static contextTypes = { | ||
router: PropTypes.shape({ | ||
listen: PropTypes.func.isRequired | ||
listen: PropTypes.func.isRequired, | ||
match: PropTypes.object | ||
}).isRequired | ||
} | ||
|
||
|
@@ -98,7 +104,7 @@ class Route extends React.Component { | |
|
||
componentWillReceiveProps(nextProps) { | ||
Object.assign(this.router, { | ||
match: computeMatch(this.router, nextProps) | ||
match: computeMatch(this.context.router, nextProps) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to use the parent There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change makes me nervous. If it's needed here, it feels like the kind of thing that should be needed more generally. If it's not needed outside the context of this PR, it's probably going to cause a bug. |
||
}) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import expect from 'expect' | ||
import React from 'react' | ||
import ReactDOM from 'react-dom' | ||
import createMemoryHistory from 'history/createMemoryHistory' | ||
import Router from '../Router' | ||
import Redirect from '../Redirect' | ||
|
||
describe('Redirect', () => { | ||
const createHistoryMock = () => { | ||
const pushes = [] | ||
const replaces = [] | ||
const history = createMemoryHistory() | ||
history.push = (loc) => pushes.push(loc) | ||
history.replace = (loc) => replaces.push(loc) | ||
history.getResults = () => ({ replaces, pushes }) | ||
return history | ||
} | ||
|
||
it('works with relative paths', () => { | ||
const div = document.createElement('div') | ||
const REDIRECTED = 'REDIRECTED' | ||
const history = createHistoryMock() | ||
ReactDOM.render(( | ||
<Router history={history}> | ||
<Redirect to={REDIRECTED} push={true}/> | ||
</Router> | ||
), div, () => { | ||
const { pushes, replaces } = history.getResults() | ||
expect(pushes.length).toEqual(1) | ||
expect(pushes[0]).toEqual('/' + REDIRECTED) | ||
expect(replaces.length).toEqual(0) | ||
}) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to modify
<ExampleRouter>
in order for it to work with relative routes. The<FakeBrowser>
renders everything inside of a pathless<Route>
, so without this relative<Route>
s and<Link>
s would resolve using that<Route>
'smatch
. This just sets the<ExampleRouter>
'smatch
tonull
. It also has to listen for location changes because any of its children that listen for location changes would be re-rendering via thelistener
prior to<ExampleRouter>
'sgetChildContext
being called on navigation.