Skip to content

Commit 1bafa32

Browse files
committed
Merge pull request #3442 from gaearon/pass-params
Attach current location and params to context.router
2 parents dcb1b51 + a76efdd commit 1bafa32

File tree

6 files changed

+54
-27
lines changed

6 files changed

+54
-27
lines changed

modules/Router.js

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,36 +55,43 @@ const Router = React.createClass({
5555
}
5656
},
5757

58-
componentWillMount() {
59-
const { transitionManager, router } = this.createRouterObjects()
60-
61-
this._unlisten = transitionManager.listen((error, state) => {
62-
if (error) {
63-
this.handleError(error)
64-
} else {
65-
this.setState(state, this.props.onUpdate)
66-
}
67-
})
58+
createRouterObject(state) {
59+
const { matchContext } = this.props
60+
if (matchContext) {
61+
return matchContext.router
62+
}
6863

69-
this.router = router
64+
const { history } = this.props
65+
return createRouterObject(history, this.transitionManager, state)
7066
},
7167

72-
createRouterObjects() {
68+
createTransitionManager() {
7369
const { matchContext } = this.props
7470
if (matchContext) {
75-
return matchContext
71+
return matchContext.transitionManager
7672
}
7773

78-
let { history } = this.props
74+
const { history } = this.props
7975
const { routes, children } = this.props
8076

81-
const transitionManager = createTransitionManager(
77+
return createTransitionManager(
8278
history,
8379
createRoutes(routes || children)
8480
)
85-
const router = createRouterObject(history, transitionManager)
81+
},
8682

87-
return { transitionManager, router }
83+
componentWillMount() {
84+
this.transitionManager = this.createTransitionManager()
85+
this.router = this.createRouterObject(this.state)
86+
87+
this._unlisten = this.transitionManager.listen((error, state) => {
88+
if (error) {
89+
this.handleError(error)
90+
} else {
91+
this.router = this.createRouterObject(state)
92+
this.setState(state, this.props.onUpdate)
93+
}
94+
})
8895
},
8996

9097
/* istanbul ignore next: sanity check */

modules/RouterUtils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
export function createRouterObject(history, transitionManager) {
1+
export function createRouterObject(history, transitionManager, state) {
22
return {
33
...history,
44
setRouteLeaveHook: transitionManager.listenBeforeLeavingRoute,
5-
isActive: transitionManager.isActive
5+
isActive: transitionManager.isActive,
6+
location: state.location,
7+
params: state.params
68
}
79
}

modules/__tests__/Router-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,12 @@ describe('Router', function () {
297297
const assertProps = (props) => {
298298
expect(props.routes).toEqual([ route ])
299299
expect(props.components).toEqual([ MyComponent ])
300+
301+
expect(props.params).toEqual({})
302+
expect(props.location.pathname).toEqual('/')
303+
expect(props.router.params).toEqual({})
304+
expect(props.router.location.pathname).toEqual('/')
305+
300306
expect(props.foo).toBe('bar')
301307
expect(props.render).toNotExist()
302308
done()

modules/__tests__/RouterContext-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('RouterContext', () => {
3131
isActive: expect.createSpy().andReturn(isActiveSentinel)
3232
}
3333

34-
router = createRouterObject(history, transitionManager)
34+
router = createRouterObject(history, transitionManager, {})
3535

3636
class Component extends React.Component {
3737
constructor(props, ctx) {

modules/__tests__/serverRendering-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@ describe('server rendering', function () {
152152
})
153153
})
154154

155+
it('includes params and location in the props', function (done) {
156+
match({ routes, location: '/dashboard' }, function (error, redirectLocation, renderProps) {
157+
expect(renderProps.params).toEqual({})
158+
expect(renderProps.router.params).toEqual({})
159+
160+
expect(renderProps.location.pathname).toEqual('/dashboard')
161+
expect(renderProps.router.location.pathname).toEqual('/dashboard')
162+
done()
163+
})
164+
})
165+
155166
it('accepts a basename option', function (done) {
156167
match({ routes, location: '/dashboard', basename: '/nasebame' }, function (error, redirectLocation, renderProps) {
157168
const string = renderToString(

modules/match.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,19 @@ function match({ history, routes, location, ...options }, callback) {
3939
})
4040
}
4141

42-
const router = createRouterObject(history, transitionManager)
43-
4442
transitionManager.match(location, function (error, redirectLocation, nextState) {
45-
callback(
46-
error,
47-
redirectLocation,
48-
nextState && {
43+
let renderProps
44+
45+
if (nextState) {
46+
const router = createRouterObject(history, transitionManager, nextState)
47+
renderProps = {
4948
...nextState,
5049
router,
5150
matchContext: { transitionManager, router }
5251
}
53-
)
52+
}
53+
54+
callback(error, redirectLocation, renderProps)
5455

5556
// Defer removing the listener to here to prevent DOM histories from having
5657
// to unwind DOM event listeners unnecessarily, in case callback renders a

0 commit comments

Comments
 (0)