From 227f133536af85dc5ff85eeb269b76ed80cd3f05 Mon Sep 17 00:00:00 2001 From: Satyajit Sahoo Date: Fri, 13 Nov 2020 04:42:27 +0100 Subject: [PATCH] fix: drop dangerously prefix from getState and getParent BREAKING CHANGE --- example/src/Screens/ModalPresentationStack.tsx | 16 +--------------- .../compat/src/createCompatNavigationProp.tsx | 8 ++++---- packages/compat/src/types.tsx | 2 +- packages/core/src/BaseNavigationContainer.tsx | 4 ++-- packages/core/src/__tests__/index.test.tsx | 4 ++-- .../core/src/__tests__/useNavigation.test.tsx | 4 ++-- packages/core/src/types.tsx | 9 ++++----- packages/core/src/useNavigationHelpers.tsx | 4 ++-- packages/core/src/useNavigationState.tsx | 6 ++---- packages/native/src/useLinkBuilder.tsx | 4 ++-- packages/native/src/useLinkTo.tsx | 2 +- packages/native/src/useScrollToTop.tsx | 6 +++--- 12 files changed, 26 insertions(+), 43 deletions(-) diff --git a/example/src/Screens/ModalPresentationStack.tsx b/example/src/Screens/ModalPresentationStack.tsx index 2bbd162a58..f277129075 100644 --- a/example/src/Screens/ModalPresentationStack.tsx +++ b/example/src/Screens/ModalPresentationStack.tsx @@ -85,21 +85,7 @@ export default function SimpleStackScreen({ navigation, options }: Props) { }, [navigation]); return ( - ({ - // ...TransitionPresets.ModalPresentationIOS, - // cardOverlayEnabled: true, - // gestureEnabled: true, - // headerStatusBarHeight: - // navigation - // .dangerouslyGetState() - // .routes.findIndex((r: any) => r.key === route.key) > 0 - // ? 0 - // : undefined, - // })} - {...options} - > + | undefined >(): T extends NavigationProp ? CompatNavigationProp diff --git a/packages/core/src/BaseNavigationContainer.tsx b/packages/core/src/BaseNavigationContainer.tsx index 96766b2ccf..9fff32746c 100644 --- a/packages/core/src/BaseNavigationContainer.tsx +++ b/packages/core/src/BaseNavigationContainer.tsx @@ -213,8 +213,8 @@ const BaseNavigationContainer = React.forwardRef( dispatch, canGoBack, getRootState, - dangerouslyGetState: () => state, - dangerouslyGetParent: () => undefined, + getState: () => state, + getParent: () => undefined, getCurrentRoute, getCurrentOptions, })); diff --git a/packages/core/src/__tests__/index.test.tsx b/packages/core/src/__tests__/index.test.tsx index f2ced4835e..dbb0c9e0c9 100644 --- a/packages/core/src/__tests__/index.test.tsx +++ b/packages/core/src/__tests__/index.test.tsx @@ -550,7 +550,7 @@ it('updates route params with setParams applied to parent', () => { let setParams: (params: object) => void = () => undefined; const FooScreen = (props: any) => { - const parent = props.navigation.dangerouslyGetParent(); + const parent = props.navigation.getParent(); if (parent) { setParams = parent.setParams; } @@ -1306,7 +1306,7 @@ it('gives access to internal state', () => { const Test = () => { const navigation = useNavigation(); - state = navigation.dangerouslyGetState(); + state = navigation.getState(); return null; }; diff --git a/packages/core/src/__tests__/useNavigation.test.tsx b/packages/core/src/__tests__/useNavigation.test.tsx index 7fe3ebe080..12296c1caa 100644 --- a/packages/core/src/__tests__/useNavigation.test.tsx +++ b/packages/core/src/__tests__/useNavigation.test.tsx @@ -44,7 +44,7 @@ it("gets navigation's parent from context", () => { const Test = () => { const navigation = useNavigation(); - expect(navigation.dangerouslyGetParent()).toBeDefined(); + expect(navigation.getParent()).toBeDefined(); return null; }; @@ -75,7 +75,7 @@ it("gets navigation's parent's parent from context", () => { const Test = () => { const navigation = useNavigation(); - const parent = navigation.dangerouslyGetParent(); + const parent = navigation.getParent(); expect(parent).toBeDefined(); if (parent !== undefined) { diff --git a/packages/core/src/types.tsx b/packages/core/src/types.tsx index c84a7cd2da..4388edf713 100644 --- a/packages/core/src/types.tsx +++ b/packages/core/src/types.tsx @@ -200,17 +200,16 @@ type NavigationHelpersCommon< /** * Returns the parent navigator, if any. Reason why the function is called - * dangerouslyGetParent is to warn developers against overusing it to eg. get parent + * getParent is to warn developers against overusing it to eg. get parent * of parent and other hard-to-follow patterns. */ - dangerouslyGetParent | undefined>(): T; + getParent | undefined>(): T; /** - * Returns the navigator's state. Reason why the function is called - * dangerouslyGetState is to discourage developers to use internal navigation's state. + * Returns the navigator's state. * Note that this method doesn't re-render screen when the result changes. So don't use it in `render`. */ - dangerouslyGetState(): State; + getState(): State; } & PrivateValueStore; export type NavigationHelpers< diff --git a/packages/core/src/useNavigationHelpers.tsx b/packages/core/src/useNavigationHelpers.tsx index 011073127a..4402e85f76 100644 --- a/packages/core/src/useNavigationHelpers.tsx +++ b/packages/core/src/useNavigationHelpers.tsx @@ -83,8 +83,8 @@ export default function useNavigationHelpers< false ); }, - dangerouslyGetParent: () => parentNavigationHelpers as any, - dangerouslyGetState: getState, + getParent: () => parentNavigationHelpers as any, + getState: getState, } as NavigationHelpers & (NavigationProp | undefined) & ActionHelpers; diff --git a/packages/core/src/useNavigationState.tsx b/packages/core/src/useNavigationState.tsx index 35d415cc02..1f0b812e9f 100644 --- a/packages/core/src/useNavigationState.tsx +++ b/packages/core/src/useNavigationState.tsx @@ -14,9 +14,7 @@ export default function useNavigationState(selector: Selector): T { // We don't care about the state value, we run the selector again at the end // The state is only to make sure that there's a re-render when we have a new value - const [, setResult] = React.useState(() => - selector(navigation.dangerouslyGetState()) - ); + const [, setResult] = React.useState(() => selector(navigation.getState())); // We store the selector in a ref to avoid re-subscribing listeners every render const selectorRef = React.useRef(selector); @@ -33,5 +31,5 @@ export default function useNavigationState(selector: Selector): T { return unsubscribe; }, [navigation]); - return selector(navigation.dangerouslyGetState()); + return selector(navigation.getState()); } diff --git a/packages/native/src/useLinkBuilder.tsx b/packages/native/src/useLinkBuilder.tsx index 0d5e991773..246865d38e 100644 --- a/packages/native/src/useLinkBuilder.tsx +++ b/packages/native/src/useLinkBuilder.tsx @@ -21,10 +21,10 @@ const getRootStateForNavigate = ( navigation: NavigationObject, state: MinimalState ): MinimalState => { - const parent = navigation.dangerouslyGetParent(); + const parent = navigation.getParent(); if (parent) { - const parentState = parent.dangerouslyGetState(); + const parentState = parent.getState(); return getRootStateForNavigate(parent, { index: 0, diff --git a/packages/native/src/useLinkTo.tsx b/packages/native/src/useLinkTo.tsx index 28539bfc52..0f4327d7e1 100644 --- a/packages/native/src/useLinkTo.tsx +++ b/packages/native/src/useLinkTo.tsx @@ -33,7 +33,7 @@ export default function useLinkTo() { let current; // Traverse up to get the root navigation - while ((current = root.dangerouslyGetParent())) { + while ((current = root.getParent())) { root = current; } diff --git a/packages/native/src/useScrollToTop.tsx b/packages/native/src/useScrollToTop.tsx index e5d56a149d..2924baf91a 100644 --- a/packages/native/src/useScrollToTop.tsx +++ b/packages/native/src/useScrollToTop.tsx @@ -53,8 +53,8 @@ export default function useScrollToTop( // The screen might be inside another navigator such as stack nested in tabs // We need to find the closest tab navigator and add the listener there - while (current && current.dangerouslyGetState().type !== 'tab') { - current = current.dangerouslyGetParent(); + while (current && current.getState().type !== 'tab') { + current = current.getParent(); } if (!current) { @@ -74,7 +74,7 @@ export default function useScrollToTop( // So we should scroll to top only when we are on first screen const isFirst = navigation === current || - navigation.dangerouslyGetState().routes[0].key === route.key; + navigation.getState().routes[0].key === route.key; // Run the operation in the next frame so we're sure all listeners have been run // This is necessary to know if preventDefault() has been called