Skip to content

Commit

Permalink
fix: use ref for back button listener (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
archansel authored Jul 25, 2020
1 parent b843882 commit 491dfe5
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
StyleSheet,
KeyboardAvoidingViewProps,
ViewStyle,
NativeEventSubscription,
} from 'react-native';
import {
PanGestureHandler,
Expand Down Expand Up @@ -172,6 +173,7 @@ const ModalizeBase = (
const nativeViewChildrenRef = React.useRef<NativeViewGestureHandler>(null);
const contentViewRef = React.useRef<ScrollView | FlatList<any> | SectionList<any>>(null);
const tapGestureOverlayRef = React.useRef<TapGestureHandler>(null);
const backButtonListenerRef = React.useRef<NativeEventSubscription>(null);

// We diff and get the negative value only. It sometimes go above 0
// (e.g. 1.5) and creates the flickering on Modalize for a ms
Expand Down Expand Up @@ -224,7 +226,7 @@ const ModalizeBase = (
): void => {
const { timing, spring } = openAnimationConfig;

BackHandler.addEventListener('hardwareBackPress', handleBackPress);
backButtonListenerRef.current = BackHandler.addEventListener('hardwareBackPress', handleBackPress);

let toValue = 0;
let toPanValue = 0;
Expand Down Expand Up @@ -306,7 +308,7 @@ const ModalizeBase = (
const toValue =
toInitialAlwaysOpen && alwaysOpen ? (modalHeightValue || 0) - alwaysOpen : screenHeight;

BackHandler.removeEventListener('hardwareBackPress', handleBackPress);
backButtonListenerRef.current.remove();

cancelTranslateY.setValue(1);
setBeginScrollYValue(0);
Expand Down Expand Up @@ -875,7 +877,7 @@ const ModalizeBase = (
Keyboard.addListener('keyboardDidHide', handleKeyboardHide);

return (): void => {
BackHandler.removeEventListener('hardwareBackPress', handleBackPress);
backButtonListenerRef.current.remove();
Keyboard.removeListener('keyboardDidShow', handleKeyboardShow);
Keyboard.removeListener('keyboardDidHide', handleKeyboardHide);
};
Expand Down

0 comments on commit 491dfe5

Please sign in to comment.