Skip to content

Commit f615859

Browse files
yedidyakethanshar
andauthored
Use subscription objects to remove subscriptions (#1527)
* Use subscription objects to remove subscriptions * Support RN64 as well as RN65 Appearance subscriptions Co-authored-by: Ethan Sharabi <[email protected]>
1 parent 7d1811c commit f615859

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/commons/asBaseComponent.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import {Appearance} from 'react-native';
2+
import {Appearance, EventSubscription} from 'react-native';
33
import hoistStatics from 'hoist-non-react-statics';
44
import * as Modifiers from './modifiers';
55
import forwardRef from './forwardRef';
@@ -27,12 +27,26 @@ function asBaseComponent<PROPS, STATICS = {}>(WrappedComponent: React.ComponentT
2727
error: false
2828
};
2929

30+
appearanceListenerSubscription?: EventSubscription = undefined;
31+
3032
componentDidMount() {
31-
Appearance.addChangeListener(this.appearanceListener);
33+
// < RN64 - void
34+
// >= RN65 - EventSubscription
35+
const subscription = Appearance.addChangeListener(this.appearanceListener);
36+
if (subscription !== undefined) {
37+
// @ts-ignore
38+
this.appearanceListenerSubscription = subscription;
39+
}
3240
}
33-
41+
3442
componentWillUnmount() {
35-
Appearance.removeChangeListener(this.appearanceListener);
43+
if (this.appearanceListenerSubscription) {
44+
// >=RN65
45+
this.appearanceListenerSubscription?.remove();
46+
} else {
47+
// <RN65
48+
Appearance.removeChangeListener(this.appearanceListener);
49+
}
3650
}
3751

3852
appearanceListener: Appearance.AppearanceListener = () => {

src/components/tagsInput/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import _ from 'lodash';
22
import PropTypes from 'prop-types';
33
import React from 'react';
4-
import ReactNative, {NativeModules, StyleSheet, ViewPropTypes, Image, DeviceEventEmitter} from 'react-native';
4+
import ReactNative, {NativeModules, StyleSheet, ViewPropTypes, Image, DeviceEventEmitter, EmitterSubscription} from 'react-native';
55
import {Constants} from '../../helpers';
66
import {Colors, BorderRadiuses, Typography} from '../../style';
77
import Assets from '../../assets';
@@ -88,6 +88,8 @@ export default class TagsInput extends BaseComponent {
8888
REMOVED: 'removed'
8989
};
9090

91+
backspacePressEventSubscription: EmitterSubscription = undefined;
92+
9193
constructor(props) {
9294
super(props);
9395

@@ -114,14 +116,14 @@ export default class TagsInput extends BaseComponent {
114116

115117
if (textInputHandle && NativeModules.TextInputDelKeyHandler) {
116118
NativeModules.TextInputDelKeyHandler.register(textInputHandle);
117-
DeviceEventEmitter.addListener('onBackspacePress', this.onKeyPress);
119+
this.backspacePressEventSubscription = DeviceEventEmitter.addListener('onBackspacePress', this.onKeyPress);
118120
}
119121
}
120122
}
121123

122124
componentWillUnmount() {
123125
if (Constants.isAndroid) {
124-
DeviceEventEmitter.removeListener('onBackspacePress', this.onKeyPress);
126+
this.backspacePressEventSubscription.remove();
125127
}
126128
}
127129

0 commit comments

Comments
 (0)