Skip to content

Commit 5f3387a

Browse files
committed
Update router state properties directly (#3446)
* Update router state properties directly. * Reuse assignRouterState in creating the router. * Add a bonus props.routes to withRouter
1 parent 17a3625 commit 5f3387a

File tree

5 files changed

+18
-9
lines changed

5 files changed

+18
-9
lines changed

modules/Router.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import createTransitionManager from './createTransitionManager'
44
import { routes } from './InternalPropTypes'
55
import RouterContext from './RouterContext'
66
import { createRoutes } from './RouteUtils'
7-
import { createRouterObject } from './RouterUtils'
7+
import { createRouterObject, assignRouterState } from './RouterUtils'
88
import warning from './routerWarning'
9-
import assign from 'object-assign'
109

1110
const { func, object } = React.PropTypes
1211

@@ -91,7 +90,7 @@ const Router = React.createClass({
9190
} else {
9291
// Keep the identity of this.router because of a caveat in ContextUtils:
9392
// they only work if the object identity is preserved.
94-
assign(this.router, this.createRouterObject(state))
93+
assignRouterState(this.router, state)
9594
this.setState(state, this.props.onUpdate)
9695
}
9796
})

modules/RouterUtils.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
export function createRouterObject(history, transitionManager, state) {
2-
return {
2+
const router = {
33
...history,
44
setRouteLeaveHook: transitionManager.listenBeforeLeavingRoute,
5-
isActive: transitionManager.isActive,
6-
location: state.location,
7-
params: state.params
5+
isActive: transitionManager.isActive
86
}
7+
8+
return assignRouterState(router, state)
9+
}
10+
11+
export function assignRouterState(router, { location, params, routes }) {
12+
router.location = location
13+
router.params = params
14+
router.routes = routes
15+
16+
return router
917
}

modules/__tests__/withRouter-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ describe('withRouter', function () {
1515
expect(this.props.params).toBe(this.props.router.params)
1616
expect(this.props.location).toExist()
1717
expect(this.props.location).toBe(this.props.router.location)
18+
expect(this.props.routes).toExist()
19+
expect(this.props.routes).toBe(this.props.router.routes)
1820
return <h1>{this.props.router.location.pathname}</h1>
1921
}
2022
}

modules/withRouter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ export default function withRouter(WrappedComponent) {
1515

1616
render() {
1717
const { router } = this.context
18-
const { params, location } = router
18+
const { params, location, routes } = router
1919
return (
2020
<WrappedComponent
2121
{...this.props}
2222
router={router}
2323
params={params}
2424
location={location}
25+
routes={routes}
2526
/>
2627
)
2728
}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
"history": "^2.0.1",
3636
"hoist-non-react-statics": "^1.0.5",
3737
"invariant": "^2.2.1",
38-
"object-assign": "^4.1.0",
3938
"warning": "^2.1.0"
4039
},
4140
"peerDependencies": {

0 commit comments

Comments
 (0)