mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-03-06 22:39:41 +08:00
fix: support legacy goBack method
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { CommonActions } from '@react-navigation/core';
|
||||
import { CommonActions, NavigationState } from '@react-navigation/core';
|
||||
|
||||
export function navigate({
|
||||
routeName,
|
||||
@@ -24,14 +24,14 @@ export function navigate({
|
||||
});
|
||||
}
|
||||
|
||||
export function back(options?: { key: null | never }): CommonActions.Action {
|
||||
if (options !== undefined && options.key != null) {
|
||||
throw new Error(
|
||||
"The legacy `back` action with a key is not supported. To go back from a specific route, you need to specify both route key and the navigator's state key in the action: `{ ...CommonActions.goBack(), source: route.key, target: state.key }`."
|
||||
);
|
||||
}
|
||||
|
||||
return CommonActions.goBack();
|
||||
export function back(options?: { key?: null | string }) {
|
||||
return options && options.key != null
|
||||
? (state: NavigationState) => ({
|
||||
...CommonActions.goBack(),
|
||||
source: options.key,
|
||||
target: state.key,
|
||||
})
|
||||
: CommonActions.goBack();
|
||||
}
|
||||
|
||||
export function setParams({
|
||||
|
||||
@@ -26,16 +26,24 @@ export default function createCompatNavigationProp<
|
||||
) {
|
||||
return {
|
||||
...navigation,
|
||||
...Object.entries(helpers).reduce<{ [key: string]: Function }>(
|
||||
(acc, [name, method]: [string, Function]) => {
|
||||
if (name in navigation) {
|
||||
acc[name] = (...args: any[]) => navigation.dispatch(method(...args));
|
||||
}
|
||||
...Object.entries(helpers).reduce<{
|
||||
[key: string]: (...args: any[]) => void;
|
||||
}>((acc, [name, method]) => {
|
||||
if (name in navigation) {
|
||||
acc[name] = (...args: any[]) => {
|
||||
// @ts-ignore
|
||||
const payload = method(...args);
|
||||
|
||||
return acc;
|
||||
},
|
||||
{}
|
||||
),
|
||||
navigation.dispatch(
|
||||
typeof payload === 'function'
|
||||
? payload(navigation.dangerouslyGetState())
|
||||
: payload
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, {}),
|
||||
original: navigation,
|
||||
addListener(type: EventName, callback: () => void) {
|
||||
let unsubscribe: () => void;
|
||||
|
||||
@@ -29,10 +29,8 @@ export function navigate(
|
||||
return NavigationActions.navigate(options);
|
||||
}
|
||||
|
||||
export function goBack(fromKey?: null | never) {
|
||||
return NavigationActions.back(
|
||||
fromKey !== undefined ? { key: fromKey } : undefined
|
||||
);
|
||||
export function goBack(fromKey?: null | string) {
|
||||
return NavigationActions.back({ key: fromKey });
|
||||
}
|
||||
|
||||
export function setParams(params: object) {
|
||||
|
||||
Reference in New Issue
Block a user