33 * @module components
44 */ /** */
55import * as React from 'react' ;
6- import { Component , Children } from 'react' ;
6+ import { Component } from 'react' ;
77import * as PropTypes from 'prop-types' ;
88
9- import { UIRouterPlugin , servicesPlugin } from '@uirouter/core' ;
9+ import { servicesPlugin } from '@uirouter/core' ;
1010
1111import { UIRouterReact , ReactStateDeclaration } from '../index' ;
1212
@@ -44,21 +44,21 @@ export interface UIRouterState {
4444
4545/** @hidden */
4646export 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+ `) ;
6262
6363/** @hidden */
6464export const UIRouterInstanceUndefinedError = new Error (
@@ -76,24 +76,27 @@ export class UIRouter extends Component<UIRouterProps, UIRouterState> {
7676
7777 router : UIRouterReact ;
7878
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 ( ) ;
9296 }
93- this . router . start ( ) ;
9497 }
9598
9699 render ( ) {
97- return < UIRouterProvider value = { this . router } > { this . props . children } </ UIRouterProvider > ;
100+ return this . router ? < UIRouterProvider value = { this . router } > { this . props . children } </ UIRouterProvider > : null ;
98101 }
99102}
0 commit comments