Skip to content

Commit

Permalink
fix: use remove method on the event subscription (#2923)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewalczak authored Oct 14, 2021
1 parent 8c8e54d commit 0c35337
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 20 deletions.
25 changes: 5 additions & 20 deletions src/components/BottomNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
StyleSheet,
StyleProp,
Platform,
Keyboard,
ViewStyle,
} from 'react-native';
import { getBottomSpace } from 'react-native-iphone-x-helper';
Expand All @@ -23,6 +22,7 @@ import { withTheme } from '../core/theming';
import useAnimatedValue from '../utils/useAnimatedValue';
import useAnimatedValueArray from '../utils/useAnimatedValueArray';
import useLayout from '../utils/useLayout';
import useIsKeyboardShown from '../utils/useIsKeyboardShown';

type Route = {
key: string;
Expand Down Expand Up @@ -482,25 +482,10 @@ const BottomNavigation = ({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

React.useEffect(() => {
if (Platform.OS === 'ios') {
Keyboard.addListener('keyboardWillShow', handleKeyboardShow);
Keyboard.addListener('keyboardWillHide', handleKeyboardHide);
} else {
Keyboard.addListener('keyboardDidShow', handleKeyboardShow);
Keyboard.addListener('keyboardDidHide', handleKeyboardHide);
}

return () => {
if (Platform.OS === 'ios') {
Keyboard.removeListener('keyboardWillShow', handleKeyboardShow);
Keyboard.removeListener('keyboardWillHide', handleKeyboardHide);
} else {
Keyboard.removeListener('keyboardDidShow', handleKeyboardShow);
Keyboard.removeListener('keyboardDidHide', handleKeyboardHide);
}
};
}, [handleKeyboardHide, handleKeyboardShow]);
useIsKeyboardShown({
onShow: handleKeyboardShow,
onHide: handleKeyboardHide,
});

const prevNavigationState = React.useRef<NavigationState>();

Expand Down
51 changes: 51 additions & 0 deletions src/utils/useIsKeyboardShown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as React from 'react';
import { Keyboard, NativeEventSubscription, Platform } from 'react-native';

type Props = {
onShow: () => void;
onHide: () => void;
};
export default function useIsKeyboardShown({ onShow, onHide }: Props) {
React.useEffect(() => {
let willShowSubscription: NativeEventSubscription | undefined;
let willHideSubscription: NativeEventSubscription | undefined;
let didShowSubscription: NativeEventSubscription | undefined;
let didHideSubscription: NativeEventSubscription | undefined;

if (Platform.OS === 'ios') {
willShowSubscription = Keyboard.addListener('keyboardWillShow', onShow);
willHideSubscription = Keyboard.addListener('keyboardWillHide', onHide);
} else {
didShowSubscription = Keyboard.addListener('keyboardDidShow', onShow);
didHideSubscription = Keyboard.addListener('keyboardDidHide', onHide);
}

return () => {
if (Platform.OS === 'ios') {
if (willShowSubscription?.remove) {
willShowSubscription.remove();
} else {
Keyboard.removeListener('keyboardWillShow', onShow);
}

if (willHideSubscription?.remove) {
willHideSubscription.remove();
} else {
Keyboard.removeListener('keyboardWillHide', onHide);
}
} else {
if (didShowSubscription?.remove) {
didShowSubscription.remove();
} else {
Keyboard.removeListener('keyboardDidShow', onShow);
}

if (didHideSubscription?.remove) {
didHideSubscription.remove();
} else {
Keyboard.removeListener('keyboardDidHide', onHide);
}
}
};
}, [onHide, onShow]);
}

0 comments on commit 0c35337

Please sign in to comment.