Skip to content

Commit

Permalink
fix: drop dangerously prefix from getState and getParent
Browse files Browse the repository at this point in the history
BREAKING CHANGE
  • Loading branch information
satya164 committed Nov 13, 2020
1 parent 8773dcb commit 227f133
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 43 deletions.
16 changes: 1 addition & 15 deletions example/src/Screens/ModalPresentationStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,7 @@ export default function SimpleStackScreen({ navigation, options }: Props) {
}, [navigation]);

return (
<ModalPresentationStack.Navigator
mode="modal"
// screenOptions={({ route, navigation }) => ({
// ...TransitionPresets.ModalPresentationIOS,
// cardOverlayEnabled: true,
// gestureEnabled: true,
// headerStatusBarHeight:
// navigation
// .dangerouslyGetState()
// .routes.findIndex((r: any) => r.key === route.key) > 0
// ? 0
// : undefined,
// })}
{...options}
>
<ModalPresentationStack.Navigator mode="modal" {...options}>
<ModalPresentationStack.Screen
name="Article"
component={ArticleScreen}
Expand Down
8 changes: 4 additions & 4 deletions packages/compat/src/createCompatNavigationProp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,17 @@ export default function createCompatNavigationProp<
return isFirstRouteInParent;
}

const { routes } = navigation.dangerouslyGetState();
const { routes } = navigation.getState();

return routes[0].key === state.key;
},
dangerouslyGetParent() {
const parent = navigation.dangerouslyGetParent();
getParent() {
const parent = navigation.getParent();

if (parent) {
return createCompatNavigationProp(
parent,
navigation.dangerouslyGetState(),
navigation.getState(),
context.parent
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/compat/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type CompatNavigationProp<
defaultValue?: ParamList[RouteName][T]
): ParamList[RouteName][T];
isFirstRouteInParent(): boolean;
dangerouslyGetParent<
getParent<
T = NavigationProp<ParamListBase> | undefined
>(): T extends NavigationProp<ParamListBase>
? CompatNavigationProp<T>
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/BaseNavigationContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ const BaseNavigationContainer = React.forwardRef(
dispatch,
canGoBack,
getRootState,
dangerouslyGetState: () => state,
dangerouslyGetParent: () => undefined,
getState: () => state,
getParent: () => undefined,
getCurrentRoute,
getCurrentOptions,
}));
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -1306,7 +1306,7 @@ it('gives access to internal state', () => {

const Test = () => {
const navigation = useNavigation();
state = navigation.dangerouslyGetState();
state = navigation.getState();
return null;
};

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/__tests__/useNavigation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 4 additions & 5 deletions packages/core/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = NavigationProp<ParamListBase> | undefined>(): T;
getParent<T = NavigationProp<ParamListBase> | 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<ParamList, keyof ParamList, {}>;

export type NavigationHelpers<
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/useNavigationHelpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ export default function useNavigationHelpers<
false
);
},
dangerouslyGetParent: () => parentNavigationHelpers as any,
dangerouslyGetState: getState,
getParent: () => parentNavigationHelpers as any,
getState: getState,
} as NavigationHelpers<ParamListBase, EventMap> &
(NavigationProp<ParamListBase, string, any, any, any> | undefined) &
ActionHelpers;
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/useNavigationState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ export default function useNavigationState<T>(selector: Selector<T>): 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);
Expand All @@ -33,5 +31,5 @@ export default function useNavigationState<T>(selector: Selector<T>): T {
return unsubscribe;
}, [navigation]);

return selector(navigation.dangerouslyGetState());
return selector(navigation.getState());
}
4 changes: 2 additions & 2 deletions packages/native/src/useLinkBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/native/src/useLinkTo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/native/src/useScrollToTop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down

0 comments on commit 227f133

Please sign in to comment.