Skip to content

Commit

Permalink
Add initialRouteKey for StackRouter (#3540)
Browse files Browse the repository at this point in the history
* use initialRouteName as key when initializing StackRouter

* fix null headerLeft

* merge back

* fixed tests

* use config flag

* fixed snapshots

* implemented requested changes
  • Loading branch information
nico1510 authored and brentvatne committed Mar 3, 2018
1 parent 4569ad4 commit c6301ab
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions flow/react-navigation.js
Expand Up @@ -362,6 +362,7 @@ declare module 'react-navigation' {
initialRouteParams?: NavigationParams,
paths?: NavigationPathsConfig,
navigationOptions?: NavigationScreenConfig<*>,
initialRouteKey?: string,
|};

declare export type NavigationStackViewConfig = {|
Expand Down
3 changes: 2 additions & 1 deletion src/routers/StackRouter.js
Expand Up @@ -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',
Expand Down
17 changes: 17 additions & 0 deletions src/routers/__tests__/StackRouter-test.js
Expand Up @@ -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: () => <div /> },
bar: { screen: () => <div /> },
},
{ 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: () => <div /> },
Expand Down

0 comments on commit c6301ab

Please sign in to comment.