@@ -14,6 +14,11 @@ var RouteStore = require('../stores/RouteStore');
1414var URLStore = require ( '../stores/URLStore' ) ;
1515var Promise = require ( 'es6-promise' ) . Promise ;
1616
17+ /**
18+ * A map of <Route> component props that are reserved for use by the
19+ * router. All other props are considered "static" props and are passed
20+ * through to the route handler.
21+ */
1722var RESERVED_PROPS = {
1823 location : true ,
1924 handler : true ,
@@ -22,6 +27,11 @@ var RESERVED_PROPS = {
2227 children : true // ReactChildren
2328} ;
2429
30+ /**
31+ * The ref name that can be used to reference the active route component.
32+ */
33+ var REF_NAME = '__activeRoute__' ;
34+
2535/**
2636 * <Route> components specify components that are rendered to the page when the
2737 * URL matches a given pattern.
@@ -310,6 +320,14 @@ function getRootMatch(matches) {
310320 return matches [ matches . length - 1 ] ;
311321}
312322
323+ function updateMatchComponents ( matches , refs ) {
324+ var i = 0 , component ;
325+ while ( component = refs [ REF_NAME ] ) {
326+ matches [ i ++ ] . component = component ;
327+ refs = component . refs ;
328+ }
329+ }
330+
313331/**
314332 * Runs all transition hooks that are required to get from the current state
315333 * to the state specified by the given transition and updates the current state
@@ -334,6 +352,8 @@ function syncWithTransition(route, transition) {
334352
335353 var fromMatches , toMatches ;
336354 if ( currentMatches ) {
355+ updateMatchComponents ( currentMatches , route . refs ) ;
356+
337357 fromMatches = currentMatches . filter ( function ( match ) {
338358 return ! hasMatch ( nextMatches , match ) ;
339359 } ) ;
@@ -346,7 +366,7 @@ function syncWithTransition(route, transition) {
346366 toMatches = nextMatches ;
347367 }
348368
349- return checkTransitionFromHooks ( fromMatches , route . refs , transition ) . then ( function ( ) {
369+ return checkTransitionFromHooks ( fromMatches , transition ) . then ( function ( ) {
350370 if ( transition . isCancelled )
351371 return ; // No need to continue.
352372
@@ -380,15 +400,15 @@ function syncWithTransition(route, transition) {
380400 * the route's handler, so that the deepest nested handlers are called first.
381401 * Returns a promise that resolves after the last handler.
382402 */
383- function checkTransitionFromHooks ( matches , refs , transition ) {
403+ function checkTransitionFromHooks ( matches , transition ) {
384404 var promise = Promise . resolve ( ) ;
385405
386406 reversedArray ( matches ) . forEach ( function ( match ) {
387407 promise = promise . then ( function ( ) {
388408 var handler = match . route . props . handler ;
389409
390410 if ( ! transition . isCancelled && handler . willTransitionFrom )
391- return handler . willTransitionFrom ( transition , refs [ match . refName ] ) ;
411+ return handler . willTransitionFrom ( transition , match . component ) ;
392412 } ) ;
393413 } ) ;
394414
@@ -429,12 +449,12 @@ function computeHandlerProps(matches, query) {
429449 } ;
430450
431451 var childHandler ;
432- reversedArray ( matches ) . forEach ( function ( match , index ) {
452+ reversedArray ( matches ) . forEach ( function ( match ) {
433453 var route = match . route ;
434454
435455 props = Route . getUnreservedProps ( route . props ) ;
436456
437- props . ref = 'route-' + index ;
457+ props . ref = REF_NAME ;
438458 props . key = Path . injectParams ( route . props . path , match . params ) ;
439459 props . params = match . params ;
440460 props . query = query ;
@@ -447,10 +467,8 @@ function computeHandlerProps(matches, query) {
447467
448468 childHandler = function ( props , addedProps ) {
449469 var children = Array . prototype . slice . call ( arguments , 2 ) ;
450- return route . props . handler . apply ( null , [ mergeProperties ( props , addedProps ) ] . concat ( children ) ) ;
470+ return route . props . handler . apply ( null , [ mergeProperties ( props , addedProps ) ] . concat ( children ) ) ;
451471 } . bind ( this , props ) ;
452-
453- match . refName = props . ref ;
454472 } ) ;
455473
456474 return props ;
0 commit comments