Skip to content
This repository was archived by the owner on Feb 25, 2020. It is now read-only.

Commit b9389f3

Browse files
committed
Support default params in the rest of StackRouter
1 parent d08670a commit b9389f3

1 file changed

Lines changed: 33 additions & 12 deletions

File tree

src/routers/StackRouter.js

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ export default (routeConfigs, stackConfig = {}) => {
8484
})
8585
);
8686
}
87-
const params = (routeConfigs[initialRouteName].params || route.params || action.params || initialRouteParams) && {
87+
const params = (routeConfigs[initialRouteName].params ||
88+
route.params ||
89+
action.params ||
90+
initialRouteParams) && {
8891
...(routeConfigs[initialRouteName].params || {}),
8992
...(route.params || {}),
9093
...(action.params || {}),
@@ -254,11 +257,11 @@ export default (routeConfigs, stackConfig = {}) => {
254257
}
255258
}
256259

257-
// Handle explicit push navigation action. This must happen after the
258-
// focused child router has had a chance to handle the action.
260+
// Handle push and navigate actions. This must happen after the focused
261+
// child router has had a chance to handle the action.
259262
if (
260263
behavesLikePushAction(action) &&
261-
childRouters[action.routeName] !== undefined
264+
childRouters[action.routeName] !== undefined // undefined means it's not a childRouter or a screen
262265
) {
263266
const childRouter = childRouters[action.routeName];
264267
let route;
@@ -278,6 +281,7 @@ export default (routeConfigs, stackConfig = {}) => {
278281
}
279282
});
280283

284+
// An instance of this route exists already and we're dealing with a navigate action
281285
if (action.type !== StackActions.PUSH && lastRouteIndex !== -1) {
282286
// If index is unchanged and params are not being set, leave state identity intact
283287
if (state.index === lastRouteIndex && !action.params) {
@@ -311,18 +315,25 @@ export default (routeConfigs, stackConfig = {}) => {
311315
}
312316

313317
if (childRouter) {
318+
// Delegate to the child router with the given action, or init it
314319
const childAction =
315-
action.action || NavigationActions.init({ params: action.params });
320+
action.action ||
321+
NavigationActions.init({
322+
params: getParamsForRouteAndAction(action.routeName, action),
323+
});
316324
route = {
317-
params: action.params,
318-
// merge the child state in this order to allow params override
325+
params: getParamsForRouteAndAction(action.routeName, action),
326+
// note(brentvatne): does it make sense to wipe out the params
327+
// here? or even to add params at all? need more info about what
328+
// this solves
319329
...childRouter.getStateForAction(childAction),
320330
routeName: action.routeName,
321331
key: action.key || generateKey(),
322332
};
323333
} else {
334+
// Create the route from scratch
324335
route = {
325-
params: action.params,
336+
params: getParamsForRouteAndAction(action.routeName, action),
326337
routeName: action.routeName,
327338
key: action.key || generateKey(),
328339
};
@@ -417,12 +428,14 @@ export default (routeConfigs, stackConfig = {}) => {
417428
if (childRouter) {
418429
const childAction =
419430
action.action ||
420-
NavigationActions.init({ params: action.params });
431+
NavigationActions.init({
432+
params: getParamsForRouteAndAction(action.routeName, action),
433+
});
421434
childState = childRouter.getStateForAction(childAction);
422435
}
423436
const routes = [...state.routes];
424437
routes[routeIndex] = {
425-
params: action.params,
438+
params: getParamsForRouteAndAction(action.routeName, action.params),
426439
// merge the child state in this order to allow params override
427440
...childState,
428441
routeName: action.routeName,
@@ -483,13 +496,21 @@ export default (routeConfigs, stackConfig = {}) => {
483496
if (router) {
484497
const childAction =
485498
newStackAction.action ||
486-
NavigationActions.init({ params: newStackAction.params });
499+
NavigationActions.init({
500+
params: getParamsForRouteAndAction(
501+
newStackAction.routeName,
502+
newStackAction
503+
),
504+
});
487505

488506
childState = router.getStateForAction(childAction);
489507
}
490508

491509
return {
492-
params: newStackAction.params,
510+
params: getParamsForRouteAndAction(
511+
newStackAction.routeName,
512+
newStackAction
513+
),
493514
...childState,
494515
routeName: newStackAction.routeName,
495516
key: newStackAction.key || generateKey(),

0 commit comments

Comments
 (0)