Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Commit

Permalink
fix: drop isFirstRouteInParent method (#145)
Browse files Browse the repository at this point in the history
The `isFirstRouteInParent` method was added to determine whether the back button should be shown in the header for stack navigator or not.
This was mainly due to the API of the old version of stack whose public API of header didn't have all required info to determine whether it should be shown.
It was probably a mistake to add it, because this method doesn't look at history and so pretty much useless for other navigators which aren't stack.

In the new stack, the header receives a `previous` prop and the `headerLeft` option receives a `canGoBack` prop which can be used for this purpose.
It's also possible to check if a route is the first by doing `navigation.dangerouslyGetState().routes[0].key === route.key`.

So it's time to drop this method.
  • Loading branch information
satya164 authored and osdnk committed Oct 30, 2019
1 parent aa40130 commit 3a77107
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
6 changes: 6 additions & 0 deletions packages/compat/src/createCompatNavigationProp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ export default function createCompatNavigationProp<

return defaultValue;
},
isFirstRouteInParent(): boolean {
const { routes } = navigation.dangerouslyGetState();

// @ts-ignore
return routes[0].key === state.key;
},
dangerouslyGetParent() {
const parent = navigation.dangerouslyGetParent();

Expand Down
1 change: 1 addition & 0 deletions packages/compat/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type CompatNavigationProp<
paramName: T,
defaultValue?: ParamList[RouteName][T]
): ParamList[RouteName][T];
isFirstRouteInParent(): boolean;
dangerouslyGetParent<
T = NavigationProp<ParamListBase> | undefined
>(): T extends NavigationProp<ParamListBase>
Expand Down
7 changes: 0 additions & 7 deletions packages/core/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,6 @@ export type NavigationProp<
*/
setOptions(options: Partial<ScreenOptions>): void;

/**
* Check if the screen is the first route in the navigator.
* This method returns `true` if the index of the route is `0`, `false` otherwise.
* It can be useful to decide whether to display a back button in a stack.
*/
isFirstRouteInParent(): boolean;

/**
* Returns the parent navigator, if any. Reason why the function is called
* dangerouslyGetParent is to warn developers against overusing it to eg. get parent
Expand Down
7 changes: 2 additions & 5 deletions packages/core/src/useNavigationCache.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,9 @@ export default function useNavigationCache<
cache.current = state.routes.reduce<NavigationCache<State, ScreenOptions>>(
(acc, route, index) => {
const previous = cache.current[route.key];
const isFirst = route.key === state.routes[0].key;

if (previous && previous.isFirstRouteInParent() === isFirst) {
// If a cached navigation object already exists and has same `isFirstRouteInParent`, reuse it
// This method could return different result if the index of the route changes somehow
if (previous) {
// If a cached navigation object already exists, reuse it
acc[route.key] = previous;
} else {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -121,7 +119,6 @@ export default function useNavigationCache<
// This makes sure that we return the focus state in the whole tree, not just this navigator
return navigation ? navigation.isFocused() : true;
},
isFirstRouteInParent: () => isFirst,
};
}

Expand Down

0 comments on commit 3a77107

Please sign in to comment.