From 7919b33faa3d139d66379b280b87bd495a80ea83 Mon Sep 17 00:00:00 2001 From: Eric Vicenti Date: Thu, 5 Apr 2018 11:25:04 -0700 Subject: [PATCH] Enhance replace action creator (#3906) --- .../src/routers/StackRouter.js | 82 +++++++++++-------- 1 file changed, 50 insertions(+), 32 deletions(-) diff --git a/packages/react-navigation/src/routers/StackRouter.js b/packages/react-navigation/src/routers/StackRouter.js index 3722329c..53a5e8dc 100644 --- a/packages/react-navigation/src/routers/StackRouter.js +++ b/packages/react-navigation/src/routers/StackRouter.js @@ -158,38 +158,56 @@ export default (routeConfigs, stackConfig = {}) => { return { ...getNavigationActionCreators(route, navStateKey), ...(getActionCreators ? getActionCreators(route, navStateKey) : {}), - pop: (n, params) => ({ - type: StackActions.POP, - n, - ...params, - }), - popToTop: params => ({ - type: StackActions.POP_TO_TOP, - ...params, - }), - push: (routeName, params, action) => ({ - type: StackActions.PUSH, - routeName, - params, - action, - }), - replace: (routeName, params, action) => ({ - type: StackActions.REPLACE, - routeName, - params, - action, - key: route.key, - }), - reset: (actions, index) => ({ - type: StackActions.RESET, - actions, - index: index == null ? actions.length - 1 : index, - key: navStateKey, - }), - dismiss: () => ({ - type: NavigationActions.BACK, - key: navStateKey, - }), + pop: (n, params) => + StackActions.pop({ + n, + ...params, + }), + popToTop: params => StackActions.popToTop(params), + push: (routeName, params, action) => + StackActions.push({ + routeName, + params, + action, + }), + replace: (replaceWith, params, action, newKey) => { + if (typeof replaceWith === 'string') { + return StackActions.replace({ + routeName: replaceWith, + params, + action, + key: route.key, + newKey, + }); + } + invariant( + typeof replaceWith === 'object', + 'Must replaceWith an object or a string' + ); + invariant( + params == null, + 'Params must not be provided to .replace() when specifying an object' + ); + invariant( + action == null, + 'Child action must not be provided to .replace() when specifying an object' + ); + invariant( + newKey == null, + 'Child action must not be provided to .replace() when specifying an object' + ); + return StackActions.replace(replaceWith); + }, + reset: (actions, index) => + StackActions.reset({ + actions, + index: index == null ? actions.length - 1 : index, + key: navStateKey, + }), + dismiss: () => + NavigationActions.back({ + key: navStateKey, + }), }; },