mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-06-18 07:28:54 +08:00
fix: drop dangerously prefix from getState and getParent
BREAKING CHANGE
This commit is contained in:
@@ -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}
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -213,8 +213,8 @@ const BaseNavigationContainer = React.forwardRef(
|
||||
dispatch,
|
||||
canGoBack,
|
||||
getRootState,
|
||||
dangerouslyGetState: () => state,
|
||||
dangerouslyGetParent: () => undefined,
|
||||
getState: () => state,
|
||||
getParent: () => undefined,
|
||||
getCurrentRoute,
|
||||
getCurrentOptions,
|
||||
}));
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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<
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user