Skip to content

Commit 99e686c

Browse files
committed
Recreate this.router when the state changes
1 parent 7c3b469 commit 99e686c

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

modules/Router.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,41 +55,49 @@ const Router = React.createClass({
5555
}
5656
},
5757

58-
componentWillMount() {
59-
const { transitionManager, router } = this.createRouterObjects()
58+
createRouterObject(state) {
59+
const { matchContext } = this.props
60+
if (matchContext) {
61+
return matchContext.router
62+
}
6063

61-
this._unlisten = transitionManager.listen((error, state) => {
62-
if (error) {
63-
this.handleError(error)
64-
} else {
65-
router.location = state.location
66-
router.params = state.params
67-
this.setState(state, this.props.onUpdate)
68-
}
69-
})
64+
const { history } = this.props
65+
const router = createRouterObject(history, this.transitionManager)
7066

71-
this.router = router
67+
return {
68+
...router,
69+
location: state.location,
70+
params: state.params
71+
}
7272
},
7373

74-
createRouterObjects() {
74+
createTransitionManager() {
7575
const { matchContext } = this.props
7676
if (matchContext) {
77-
return matchContext
77+
return matchContext.transitionManager
7878
}
7979

80-
let { history } = this.props
80+
const { history } = this.props
8181
const { routes, children } = this.props
8282

83-
const transitionManager = createTransitionManager(
83+
return createTransitionManager(
8484
history,
8585
createRoutes(routes || children)
8686
)
87+
},
8788

88-
const router = createRouterObject(history, transitionManager)
89-
router.location = null
90-
router.params = null
89+
componentWillMount() {
90+
this.transitionManager = this.createTransitionManager()
91+
this.router = this.createRouterObject(this.state)
9192

92-
return { transitionManager, router }
93+
this._unlisten = this.transitionManager.listen((error, state) => {
94+
if (error) {
95+
this.handleError(error)
96+
} else {
97+
this.router = this.createRouterObject(state)
98+
this.setState(state, this.props.onUpdate)
99+
}
100+
})
93101
},
94102

95103
/* istanbul ignore next: sanity check */

0 commit comments

Comments
 (0)