diff --git a/flow/react-navigation.js b/flow/react-navigation.js index df4e7166..4354d541 100644 --- a/flow/react-navigation.js +++ b/flow/react-navigation.js @@ -362,6 +362,7 @@ declare module 'react-navigation' { initialRouteParams?: NavigationParams, paths?: NavigationPathsConfig, navigationOptions?: NavigationScreenConfig<*>, + initialRouteKey?: string, |}; declare export type NavigationStackViewConfig = {| diff --git a/src/routers/StackRouter.js b/src/routers/StackRouter.js index d720f3b9..9d86ca53 100644 --- a/src/routers/StackRouter.js +++ b/src/routers/StackRouter.js @@ -91,11 +91,12 @@ export default (routeConfigs, stackConfig = {}) => { ...(action.params || {}), ...(initialRouteParams || {}), }; + const { initialRouteKey } = stackConfig; route = { ...route, ...(params ? { params } : {}), routeName: initialRouteName, - key: action.key || generateKey(), + key: action.key || (initialRouteKey || generateKey()), }; return { key: 'StackRouterRoot', diff --git a/src/routers/__tests__/StackRouter-test.js b/src/routers/__tests__/StackRouter-test.js index 5211a871..9fcc675f 100644 --- a/src/routers/__tests__/StackRouter-test.js +++ b/src/routers/__tests__/StackRouter-test.js @@ -576,6 +576,23 @@ describe('StackRouter', () => { expect(state2.routes[1].routes[1].routes[1].routeName).toEqual('Corge'); }); + test('Navigate to initial screen is possible', () => { + const TestRouter = StackRouter( + { + foo: { screen: () =>
}, + bar: { screen: () =>
}, + }, + { initialRouteKey: 'foo' } + ); + const initState = TestRouter.getStateForAction(NavigationActions.init()); + const pushedState = TestRouter.getStateForAction( + NavigationActions.navigate({ routeName: 'foo', key: 'foo' }), + initState + ); + expect(pushedState.index).toEqual(0); + expect(pushedState.routes[0].routeName).toEqual('foo'); + }); + test('Navigate with key is idempotent', () => { const TestRouter = StackRouter({ foo: { screen: () =>
},