fix: support legacy goBack method

This commit is contained in:
satyajit.happy
2019-09-16 16:04:31 +02:00
parent 88a560aacb
commit d83d3de697
3 changed files with 28 additions and 22 deletions

View File

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

View File

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

View File

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