Skip to content

Commit

Permalink
Update router state properties directly (#3446)
Browse files Browse the repository at this point in the history
* Update router state properties directly.

* Reuse assignRouterState in creating the router.

* Add a bonus props.routes to withRouter
  • Loading branch information
timdorr committed May 9, 2016
1 parent 17a3625 commit 5f3387a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
5 changes: 2 additions & 3 deletions modules/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import createTransitionManager from './createTransitionManager'
import { routes } from './InternalPropTypes'
import RouterContext from './RouterContext'
import { createRoutes } from './RouteUtils'
import { createRouterObject } from './RouterUtils'
import { createRouterObject, assignRouterState } from './RouterUtils'
import warning from './routerWarning'
import assign from 'object-assign'

const { func, object } = React.PropTypes

Expand Down Expand Up @@ -91,7 +90,7 @@ const Router = React.createClass({
} else {
// Keep the identity of this.router because of a caveat in ContextUtils:
// they only work if the object identity is preserved.
assign(this.router, this.createRouterObject(state))
assignRouterState(this.router, state)
this.setState(state, this.props.onUpdate)
}
})
Expand Down
16 changes: 12 additions & 4 deletions modules/RouterUtils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
export function createRouterObject(history, transitionManager, state) {
return {
const router = {
...history,
setRouteLeaveHook: transitionManager.listenBeforeLeavingRoute,
isActive: transitionManager.isActive,
location: state.location,
params: state.params
isActive: transitionManager.isActive
}

return assignRouterState(router, state)
}

export function assignRouterState(router, { location, params, routes }) {
router.location = location
router.params = params
router.routes = routes

return router
}
2 changes: 2 additions & 0 deletions modules/__tests__/withRouter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ describe('withRouter', function () {
expect(this.props.params).toBe(this.props.router.params)
expect(this.props.location).toExist()
expect(this.props.location).toBe(this.props.router.location)
expect(this.props.routes).toExist()
expect(this.props.routes).toBe(this.props.router.routes)
return <h1>{this.props.router.location.pathname}</h1>
}
}
Expand Down
3 changes: 2 additions & 1 deletion modules/withRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ export default function withRouter(WrappedComponent) {

render() {
const { router } = this.context
const { params, location } = router
const { params, location, routes } = router
return (
<WrappedComponent
{...this.props}
router={router}
params={params}
location={location}
routes={routes}
/>
)
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"history": "^2.0.1",
"hoist-non-react-statics": "^1.0.5",
"invariant": "^2.2.1",
"object-assign": "^4.1.0",
"warning": "^2.1.0"
},
"peerDependencies": {
Expand Down

0 comments on commit 5f3387a

Please sign in to comment.