@@ -10,7 +10,6 @@ export interface Props {
1010 [ propName : string ] : any ;
1111}
1212export interface State {
13- location ?: any ,
1413 Component ?: React . ComponentClass < Props > ,
1514 props ?: any ;
1615}
@@ -32,16 +31,19 @@ export default class Router extends React.Component<Props, State> {
3231 private router : any ;
3332 private history : any ;
3433 private unlistenHistroy : any ;
34+ private subscriber : any ;
35+ private location : any ;
3536 constructor ( props ) {
3637 super ( ) ;
3738 this . state = {
3839 Component : props . Component ,
39- location : props . history . location ,
4040 props : props . props
4141 } ;
4242
43+ this . location = props . history . location ;
4344 this . router = props . router ;
4445 this . history = props . history ;
46+ this . subscriber = null ;
4547 }
4648 static async init ( { path, routes, hooks, silent = false , ctx = new Context ( ) } ) {
4749 const plainRoutes = Router . buildRoutes ( routes ) ;
@@ -93,9 +95,6 @@ export default class Router extends React.Component<Props, State> {
9395 router : this
9496 } ;
9597 }
96- get location ( ) {
97- return this . state . location ;
98- }
9998 async navigate ( path , ctx = new Context ( ) ) {
10099 try {
101100 const { redirect } = await this . router . match ( { path, ctx } ) ;
@@ -112,6 +111,19 @@ export default class Router extends React.Component<Props, State> {
112111 }
113112 }
114113 }
114+ subscribe ( callback : Function ) {
115+ this . subscriber = callback ;
116+ }
117+ changeComponent ( Component , props , renderCallback ) {
118+ if ( this . subscriber ) {
119+ this . subscriber ( Component , props , renderCallback ) ;
120+ } else {
121+ this . setState ( {
122+ Component,
123+ props
124+ } , renderCallback ) ;
125+ }
126+ }
115127 private _locationChanged = async ( location , action ) => {
116128 try {
117129 const { path, route, status, params, redirect, result, ctx } = await this . router . run ( { path : location . pathname } ) ;
@@ -123,11 +135,9 @@ export default class Router extends React.Component<Props, State> {
123135 redirect,
124136 ctx
125137 } ;
126- this . setState ( {
127- Component : result ,
128- location,
129- props
130- } , Router . makeCallback ( this . router , { path, route, status, params, redirect, result, ctx } ) ) ;
138+ this . location = location ;
139+ let renderCallback = Router . makeCallback ( this . router , { path, route, status, params, redirect, result, ctx } ) ;
140+ this . changeComponent ( result , props , renderCallback ) ;
131141 } catch ( error ) {
132142 if ( this . props . errorHandler ) {
133143 this . props . errorHandler ( error , this ) ;
@@ -144,6 +154,10 @@ export default class Router extends React.Component<Props, State> {
144154 this . unlistenHistroy ( ) ;
145155 }
146156 render ( ) {
147- return < this . state . Component router = { this . state . props } />
157+ if ( this . props . children ) {
158+ return React . Children . only ( this . props . children )
159+ } else {
160+ return < this . state . Component router = { this . state . props } />
161+ }
148162 }
149163}
0 commit comments