From d6501d3e5d845dcf243e30d41e26d516832183da Mon Sep 17 00:00:00 2001 From: "satyajit.happy" Date: Wed, 24 Jul 2019 10:56:41 +0200 Subject: [PATCH] fix: make rehydration keys stable --- example/StackNavigator.tsx | 4 ++-- example/TabNavigator.tsx | 4 ++-- src/__tests__/__fixtures__/MockRouter.tsx | 4 ++-- src/__tests__/index.test.tsx | 2 +- src/__tests__/useOnAction.test.tsx | 4 ++-- src/types.tsx | 1 + src/useNavigationBuilder.tsx | 2 ++ 7 files changed, 12 insertions(+), 9 deletions(-) diff --git a/example/StackNavigator.tsx b/example/StackNavigator.tsx index bc5dac85..8c86d86c 100644 --- a/example/StackNavigator.tsx +++ b/example/StackNavigator.tsx @@ -91,7 +91,7 @@ function StackRouter(options: DefaultRouterOptions) { }; }, - getRehydratedState({ routeNames, partialState }) { + getRehydratedState({ key, routeNames, partialState }) { let state = partialState; if (state.stale) { @@ -99,7 +99,7 @@ function StackRouter(options: DefaultRouterOptions) { ...state, stale: false, routeNames, - key: `stack-${shortid()}`, + key, }; } diff --git a/example/TabNavigator.tsx b/example/TabNavigator.tsx index 47d42849..6dffeb96 100644 --- a/example/TabNavigator.tsx +++ b/example/TabNavigator.tsx @@ -75,7 +75,7 @@ function TabRouter(options: DefaultRouterOptions) { }; }, - getRehydratedState({ routeNames, partialState }) { + getRehydratedState({ key, routeNames, partialState }) { let state = partialState; if (state.stale) { @@ -83,7 +83,7 @@ function TabRouter(options: DefaultRouterOptions) { ...state, stale: false, routeNames, - key: `tab-${shortid()}`, + key, }; } diff --git a/src/__tests__/__fixtures__/MockRouter.tsx b/src/__tests__/__fixtures__/MockRouter.tsx index b129b81b..de2404cc 100644 --- a/src/__tests__/__fixtures__/MockRouter.tsx +++ b/src/__tests__/__fixtures__/MockRouter.tsx @@ -32,7 +32,7 @@ export default function MockRouter(options: DefaultRouterOptions) { }; }, - getRehydratedState({ routeNames, partialState }) { + getRehydratedState({ key, routeNames, partialState }) { let state = partialState; if (state.stale) { @@ -40,7 +40,7 @@ export default function MockRouter(options: DefaultRouterOptions) { ...state, stale: false, routeNames, - key: String(MockRouterKey.current++), + key, }; } diff --git a/src/__tests__/index.test.tsx b/src/__tests__/index.test.tsx index 4bee3d1e..a70f55dc 100644 --- a/src/__tests__/index.test.tsx +++ b/src/__tests__/index.test.tsx @@ -99,7 +99,7 @@ it('rehydrates state for a navigator on navigation', () => { expect(onStateChange).lastCalledWith({ index: 1, - key: '2', + key: '0', routeNames: ['foo', 'bar'], routes: [{ key: 'foo', name: 'foo' }, { key: 'bar', name: 'bar' }], stale: false, diff --git a/src/__tests__/useOnAction.test.tsx b/src/__tests__/useOnAction.test.tsx index 59a31651..a880bdc9 100644 --- a/src/__tests__/useOnAction.test.tsx +++ b/src/__tests__/useOnAction.test.tsx @@ -195,7 +195,7 @@ it("lets children handle the action if parent didn't", () => { expect(onStateChange).lastCalledWith({ stale: false, index: 0, - key: '7', + key: '0', routeNames: ['foo', 'bar', 'baz'], routes: [ { @@ -204,7 +204,7 @@ it("lets children handle the action if parent didn't", () => { state: { stale: false, index: 0, - key: '6', + key: '1', routeNames: ['qux', 'lex'], routes: [{ key: 'lex', name: 'lex' }, { key: 'qux', name: 'qux' }], }, diff --git a/src/types.tsx b/src/types.tsx index c9dfd012..fbe53ddb 100644 --- a/src/types.tsx +++ b/src/types.tsx @@ -103,6 +103,7 @@ export type Router< * @param options.partialState Navigation state to rehydrate from. */ getRehydratedState(options: { + key: string; routeNames: string[]; partialState: State | PartialState; }): State; diff --git a/src/useNavigationBuilder.tsx b/src/useNavigationBuilder.tsx index cf5839d5..e815507e 100644 --- a/src/useNavigationBuilder.tsx +++ b/src/useNavigationBuilder.tsx @@ -103,6 +103,7 @@ export default function useNavigationBuilder< } = React.useContext(NavigationStateContext); let state = router.getRehydratedState({ + key: initialState.key, routeNames, partialState: currentState as any, }); @@ -139,6 +140,7 @@ export default function useNavigationBuilder< const getState = React.useCallback( (): State => router.getRehydratedState({ + key: initialState.key, routeNames, partialState: (getCurrentState() as any) || state, }),