From c9c50355db4e0a4b1bf20ad2e1eae448a697c469 Mon Sep 17 00:00:00 2001 From: jkylehumphrey Date: Fri, 26 Jan 2018 12:56:26 -0500 Subject: [PATCH 1/2] implement prevRouteName --- index.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 754d4df..b845527 100644 --- a/index.js +++ b/index.js @@ -12,9 +12,17 @@ function getCurrentRouteKey(navigationState) { return route.key } -function updateFocus(currentState) { - const currentRouteKey = getCurrentRouteKey(currentState) - subscribedComponents.forEach((f) => f(currentRouteKey)) +function getRouteName(navigationState) { + if (!navigationState) return null + const route = navigationState.routes[navigationState.index] + if (route.routes) return getRouteName(route) + return route.routeName +} + +function updateFocus(currentState, prevState) { + const currentRouteKey = getCurrentRouteKey(currentState); + const prevRouteName = getRouteName(prevState); + subscribedComponents.forEach((f) => f(currentRouteKey, prevRouteName)) } @@ -50,7 +58,7 @@ function _bind(WrappedComponent, isInitialRoute) { } } - _handleNavigationChange = (routeKey) => { + _handleNavigationChange = (routeKey, prevRouteName) => { // update state only when isFocused changes const currentScreenKey = this.props.navigation.state.key; @@ -61,8 +69,9 @@ function _bind(WrappedComponent, isInitialRoute) { // immediately reflects any changes. if (this.isFocused !== (currentScreenKey === routeKey)) { this.setState({ - isFocused: !this.isFocused, - focusedRouteKey: routeKey, + isFocused: !this.isFocused, + focusedRouteKey: routeKey, + prevRouteName: prevRouteName }) this.isFocused = !this.isFocused } @@ -73,6 +82,7 @@ function _bind(WrappedComponent, isInitialRoute) { ) From 96a8ea55d17a8aa14cf6bf812f36f75a47ad6832 Mon Sep 17 00:00:00 2001 From: jkylehumphrey Date: Fri, 26 Jan 2018 13:27:09 -0500 Subject: [PATCH 2/2] create typings --- index.d.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..b7aa961 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,16 @@ +import * as ReactNavigation from 'react-navigation' +import * as React from 'react' + +declare module "react-navigation-is-focused-hoc" { + export function updateFocus(currentState: ReactNavigation.NavigationState, prevState: ReactNavigation.NavigationState); + export function getCurrentRouteKey(navigationState: ReactNavigation.NavigationState); + export function withNavigationFocus(WrappedComponent: string, isInitialRoute?: boolean); +} + +declare module 'react-navigation' { + export interface NavigationScreenConfigProps { + isFocused?: boolean; + focusedRouteKey?: string; + prevRouteName?: string; + } +}