Skip to content

Commit 8c8e54d

Browse files
authored
fix: use remove method on the event subscription (#2918)
1 parent a488d69 commit 8c8e54d

File tree

5 files changed

+66
-19
lines changed

5 files changed

+66
-19
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"@types/jest": "^24.0.13",
6161
"@types/node": "^13.1.0",
6262
"@types/react-dom": "^16.8.4",
63-
"@types/react-native": "^0.63.0",
63+
"@types/react-native": "^0.65.5",
6464
"@types/react-native-vector-icons": "^6.4.1",
6565
"@typescript-eslint/eslint-plugin": "^2.12.0",
6666
"@typescript-eslint/parser": "^2.12.0",

src/components/Menu/Menu.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
ViewStyle,
1515
ScrollView,
1616
findNodeHandle,
17+
NativeEventSubscription,
1718
} from 'react-native';
1819

1920
import { withTheme } from '../../core/theming';
@@ -166,6 +167,8 @@ class Menu extends React.Component<Props, State> {
166167

167168
private anchor?: View | null = null;
168169
private menu?: View | null = null;
170+
private backHandlerSubscription: NativeEventSubscription | undefined;
171+
private dimensionsSubscription: NativeEventSubscription | undefined;
169172

170173
private isCoordinate = (anchor: any): anchor is { x: number; y: number } =>
171174
!React.isValidElement(anchor) &&
@@ -239,15 +242,30 @@ class Menu extends React.Component<Props, State> {
239242
};
240243

241244
private attachListeners = () => {
242-
BackHandler.addEventListener('hardwareBackPress', this.handleDismiss);
243-
Dimensions.addEventListener('change', this.handleDismiss);
245+
this.backHandlerSubscription = BackHandler.addEventListener(
246+
'hardwareBackPress',
247+
this.handleDismiss
248+
);
249+
this.dimensionsSubscription = Dimensions.addEventListener(
250+
'change',
251+
this.handleDismiss
252+
);
244253

245254
this.isBrowser() && document.addEventListener('keyup', this.handleKeypress);
246255
};
247256

248257
private removeListeners = () => {
249-
BackHandler.removeEventListener('hardwareBackPress', this.handleDismiss);
250-
Dimensions.removeEventListener('change', this.handleDismiss);
258+
if (this.backHandlerSubscription?.remove) {
259+
this.backHandlerSubscription.remove();
260+
} else {
261+
BackHandler.removeEventListener('hardwareBackPress', this.handleDismiss);
262+
}
263+
264+
if (this.dimensionsSubscription?.remove) {
265+
this.dimensionsSubscription.remove();
266+
} else {
267+
Dimensions.removeEventListener('change', this.handleDismiss);
268+
}
251269

252270
this.isBrowser() &&
253271
document.removeEventListener('keyup', this.handleKeypress);

src/components/Modal.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
TouchableWithoutFeedback,
99
ViewStyle,
1010
View,
11+
NativeEventSubscription,
1112
} from 'react-native';
1213
import {
1314
getStatusBarHeight,
@@ -132,6 +133,8 @@ class Modal extends React.Component<Props, State> {
132133
}
133134
}
134135

136+
private subscription: NativeEventSubscription | undefined;
137+
135138
private handleBack = () => {
136139
if (this.props.dismissable) {
137140
this.hideModal();
@@ -140,8 +143,15 @@ class Modal extends React.Component<Props, State> {
140143
};
141144

142145
private showModal = () => {
143-
BackHandler.removeEventListener('hardwareBackPress', this.handleBack);
144-
BackHandler.addEventListener('hardwareBackPress', this.handleBack);
146+
if (this.subscription?.remove) {
147+
this.subscription.remove();
148+
} else {
149+
BackHandler.removeEventListener('hardwareBackPress', this.handleBack);
150+
}
151+
this.subscription = BackHandler.addEventListener(
152+
'hardwareBackPress',
153+
this.handleBack
154+
);
145155

146156
const { opacity } = this.state;
147157
const { scale } = this.props.theme.animation;
@@ -155,7 +165,11 @@ class Modal extends React.Component<Props, State> {
155165
};
156166

157167
private hideModal = () => {
158-
BackHandler.removeEventListener('hardwareBackPress', this.handleBack);
168+
if (this.subscription?.remove) {
169+
this.subscription?.remove();
170+
} else {
171+
BackHandler.removeEventListener('hardwareBackPress', this.handleBack);
172+
}
159173

160174
const { opacity } = this.state;
161175
const { scale } = this.props.theme.animation;
@@ -185,7 +199,11 @@ class Modal extends React.Component<Props, State> {
185199
};
186200

187201
componentWillUnmount() {
188-
BackHandler.removeEventListener('hardwareBackPress', this.handleBack);
202+
if (this.subscription?.remove) {
203+
this.subscription.remove();
204+
} else {
205+
BackHandler.removeEventListener('hardwareBackPress', this.handleBack);
206+
}
189207
}
190208

191209
render() {

src/core/Provider.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import * as React from 'react';
2-
import { AccessibilityInfo, Appearance, ColorSchemeName } from 'react-native';
2+
import {
3+
AccessibilityInfo,
4+
Appearance,
5+
ColorSchemeName,
6+
NativeEventSubscription,
7+
} from 'react-native';
38
import { ThemeProvider } from './theming';
49
import { Provider as SettingsProvider, Settings } from './settings';
510
import MaterialCommunityIcon from '../components/MaterialCommunityIcon';
@@ -32,18 +37,24 @@ const Provider = ({ ...props }: Props) => {
3237
};
3338

3439
React.useEffect(() => {
40+
let subscription: NativeEventSubscription | undefined;
41+
3542
if (!props.theme) {
36-
AccessibilityInfo.addEventListener(
43+
subscription = AccessibilityInfo.addEventListener(
3744
'reduceMotionChanged',
3845
setReduceMotionEnabled
3946
);
4047
}
4148
return () => {
4249
if (!props.theme) {
43-
AccessibilityInfo.removeEventListener(
44-
'reduceMotionChanged',
45-
setReduceMotionEnabled
46-
);
50+
if (subscription?.remove) {
51+
subscription.remove();
52+
} else {
53+
AccessibilityInfo.removeEventListener(
54+
'reduceMotionChanged',
55+
setReduceMotionEnabled
56+
);
57+
}
4758
}
4859
};
4960
}, [props.theme]);

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2749,10 +2749,10 @@
27492749
dependencies:
27502750
"@types/react" "*"
27512751

2752-
"@types/react-native@^0.63.0":
2753-
version "0.63.46"
2754-
resolved "https://registry.yarnpkg.com/@types/react-native/-/react-native-0.63.46.tgz#942df5af29046c6f22227495e00d5297cc1cea73"
2755-
integrity sha512-SnBnWRErpISIaWk4K8kAfIKqSPdZ8fdH6HIw7kVdz6jMl/5FAf6iXeIwRfVZg1bCMh+ymNPCpSENNNEVprxj/w==
2752+
"@types/react-native@^0.65.5":
2753+
version "0.65.5"
2754+
resolved "https://registry.yarnpkg.com/@types/react-native/-/react-native-0.65.5.tgz#e5e473be8c7ed784419554f25cc8850b9c3ce68d"
2755+
integrity sha512-lc2JVmqVIkFmEtUHX8jCkuepqRSzlhRPBIlVFVc0hgfoUxvntrORhmK7LCnAbHfmpUqVVGhMjax27CCTACQ2Kw==
27562756
dependencies:
27572757
"@types/react" "*"
27582758

0 commit comments

Comments
 (0)