fix: drop dangerously prefix from getState and getParent

BREAKING CHANGE
This commit is contained in:
Satyajit Sahoo
2020-11-13 04:42:27 +01:00
parent 8773dcb80f
commit 227f133536
12 changed files with 26 additions and 43 deletions

View File

@@ -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}

View File

@@ -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
);
}

View File

@@ -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>

View File

@@ -213,8 +213,8 @@ const BaseNavigationContainer = React.forwardRef(
dispatch,
canGoBack,
getRootState,
dangerouslyGetState: () => state,
dangerouslyGetParent: () => undefined,
getState: () => state,
getParent: () => undefined,
getCurrentRoute,
getCurrentOptions,
}));

View File

@@ -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;
};

View File

@@ -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) {

View File

@@ -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<

View File

@@ -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;

View File

@@ -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);
@@ -33,5 +31,5 @@ export default function useNavigationState<T>(selector: Selector<T>): T {
return unsubscribe;
}, [navigation]);
return selector(navigation.dangerouslyGetState());
return selector(navigation.getState());
}

View File

@@ -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,

View File

@@ -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;
}

View File

@@ -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