3
3
* @module components
4
4
*/ /** */
5
5
import * as React from 'react' ;
6
- import { Component , Children } from 'react' ;
6
+ import { Component } from 'react' ;
7
7
import * as PropTypes from 'prop-types' ;
8
8
9
- import { UIRouterPlugin , servicesPlugin } from '@uirouter/core' ;
9
+ import { servicesPlugin } from '@uirouter/core' ;
10
10
11
11
import { UIRouterReact , ReactStateDeclaration } from '../index' ;
12
12
@@ -44,21 +44,21 @@ export interface UIRouterState {
44
44
45
45
/** @hidden */
46
46
export const InstanceOrPluginsMissingError = new Error ( `Router instance or plugins missing.
47
- You must either provide a location plugin via the plugins prop:
48
-
49
- <UIRouter plugins={[pushStateLocationPlugin]} states={[···]}>
50
- <UIView />
51
- </UIRouter>
52
-
53
- or initialize the router yourself and pass the instance via props:
54
-
55
- const router = new UIRouterReact();
56
- router.plugin(pushStateLocationPlugin);
57
- ···
58
- <UIRouter router={router}>
59
- <UIView />
60
- </UIRouter>
61
- ` ) ;
47
+ You must either provide a location plugin via the plugins prop:
48
+
49
+ <UIRouter plugins={[pushStateLocationPlugin]} states={[···]}>
50
+ <UIView />
51
+ </UIRouter>
52
+
53
+ or initialize the router yourself and pass the instance via props:
54
+
55
+ const router = new UIRouterReact();
56
+ router.plugin(pushStateLocationPlugin);
57
+ ···
58
+ <UIRouter router={router}>
59
+ <UIView />
60
+ </UIRouter>
61
+ `) ;
62
62
63
63
/** @hidden */
64
64
export const UIRouterInstanceUndefinedError = new Error (
@@ -76,24 +76,27 @@ export class UIRouter extends Component<UIRouterProps, UIRouterState> {
76
76
77
77
router : UIRouterReact ;
78
78
79
- constructor ( props , context ) {
80
- super ( props , context ) ;
81
- // check if a router instance is provided
82
- if ( props . router ) {
83
- this . router = props . router ;
84
- } else if ( props . plugins ) {
85
- this . router = new UIRouterReact ( ) ;
86
- this . router . plugin ( servicesPlugin ) ;
87
- props . plugins . forEach ( plugin => this . router . plugin ( plugin ) ) ;
88
- if ( props . config ) props . config ( this . router ) ;
89
- ( props . states || [ ] ) . forEach ( state => this . router . stateRegistry . register ( state ) ) ;
90
- } else {
91
- throw InstanceOrPluginsMissingError ;
79
+ componentDidMount ( ) {
80
+ if ( ! this . router ) {
81
+ // check if a router instance is provided
82
+ if ( this . props . router ) {
83
+ this . router = this . props . router ;
84
+ } else if ( this . props . plugins ) {
85
+ this . router = new UIRouterReact ( ) ;
86
+ this . router . plugin ( servicesPlugin ) ;
87
+ this . props . plugins . forEach ( plugin => this . router . plugin ( plugin ) ) ;
88
+ if ( this . props . config ) this . props . config ( this . router ) ;
89
+ ( this . props . states || [ ] ) . forEach ( state => this . router . stateRegistry . register ( state ) ) ;
90
+ } else {
91
+ throw InstanceOrPluginsMissingError ;
92
+ }
93
+
94
+ this . router . start ( ) ;
95
+ this . forceUpdate ( ) ;
92
96
}
93
- this . router . start ( ) ;
94
97
}
95
98
96
99
render ( ) {
97
- return < UIRouterProvider value = { this . router } > { this . props . children } </ UIRouterProvider > ;
100
+ return this . router ? < UIRouterProvider value = { this . router } > { this . props . children } </ UIRouterProvider > : null ;
98
101
}
99
102
}
0 commit comments