Skip to content

Commit 772a555

Browse files
authored
Deprecate removeListener APIs (#2578)
1 parent 52c8980 commit 772a555

File tree

6 files changed

+95
-121
lines changed

6 files changed

+95
-121
lines changed

docs/accessibilityinfo.md

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ const App = () => {
2121
const [screenReaderEnabled, setScreenReaderEnabled] = useState(false);
2222
2323
useEffect(() => {
24-
AccessibilityInfo.addEventListener(
24+
const reduceMotionChangedSubscription = AccessibilityInfo.addEventListener(
2525
"reduceMotionChanged",
26-
handleReduceMotionToggled
26+
reduceMotionEnabled => {
27+
setReduceMotionEnabled(reduceMotionEnabled);
28+
}
2729
);
28-
AccessibilityInfo.addEventListener(
30+
const screenReaderChangedSubscription = AccessibilityInfo.addEventListener(
2931
"screenReaderChanged",
30-
handleScreenReaderToggled
32+
screenReaderEnabled => {
33+
setScreenReaderEnabled(screenReaderEnabled);
34+
}
3135
);
3236
3337
AccessibilityInfo.isReduceMotionEnabled().then(
@@ -42,25 +46,11 @@ const App = () => {
4246
);
4347
4448
return () => {
45-
AccessibilityInfo.removeEventListener(
46-
"reduceMotionChanged",
47-
handleReduceMotionToggled
48-
);
49-
AccessibilityInfo.removeEventListener(
50-
"screenReaderChanged",
51-
handleScreenReaderToggled
52-
);
49+
reduceMotionChangedSubscription.remove();
50+
screenReaderChangedSubscription.remove();
5351
};
5452
}, []);
5553
56-
const handleReduceMotionToggled = reduceMotionEnabled => {
57-
setReduceMotionEnabled(reduceMotionEnabled);
58-
};
59-
60-
const handleScreenReaderToggled = screenReaderEnabled => {
61-
setScreenReaderEnabled(screenReaderEnabled);
62-
};
63-
6454
return (
6555
<View style={styles.container}>
6656
<Text style={styles.status}>
@@ -101,13 +91,17 @@ class AccessibilityStatusExample extends Component {
10191
};
10292
10393
componentDidMount() {
104-
AccessibilityInfo.addEventListener(
94+
this.reduceMotionChangedSubscription = AccessibilityInfo.addEventListener(
10595
'reduceMotionChanged',
106-
this._handleReduceMotionToggled
96+
reduceMotionEnabled => {
97+
this.setState({ reduceMotionEnabled });
98+
}
10799
);
108-
AccessibilityInfo.addEventListener(
100+
this.screenReaderChangedSubscription = AccessibilityInfo.addEventListener(
109101
'screenReaderChanged',
110-
this._handleScreenReaderToggled
102+
screenReaderEnabled => {
103+
this.setState({ screenReaderEnabled });
104+
}
111105
);
112106
113107
AccessibilityInfo.isReduceMotionEnabled().then(reduceMotionEnabled => {
@@ -119,52 +113,37 @@ class AccessibilityStatusExample extends Component {
119113
}
120114
121115
componentWillUnmount() {
122-
AccessibilityInfo.removeEventListener(
123-
'reduceMotionChanged',
124-
this._handleReduceMotionToggled
125-
);
126-
127-
AccessibilityInfo.removeEventListener(
128-
'screenReaderChanged',
129-
this._handleScreenReaderToggled
130-
);
116+
this.reduceMotionChangedSubscription.remove();
117+
this.screenReaderChangedSubscription.remove();
131118
}
132119
133-
_handleReduceMotionToggled = reduceMotionEnabled => {
134-
this.setState({ reduceMotionEnabled });
135-
};
136-
137-
_handleScreenReaderToggled = screenReaderEnabled => {
138-
this.setState({ screenReaderEnabled });
139-
};
140-
141120
render() {
142121
return (
143-
<View style={this.styles.container}>
144-
<Text style={this.styles.status}>
122+
<View style={styles.container}>
123+
<Text style={styles.status}>
145124
The reduce motion is{' '}
146125
{this.state.reduceMotionEnabled ? 'enabled' : 'disabled'}.
147126
</Text>
148-
<Text style={this.styles.status}>
127+
<Text style={styles.status}>
149128
The screen reader is{' '}
150129
{this.state.screenReaderEnabled ? 'enabled' : 'disabled'}.
151130
</Text>
152131
</View>
153132
);
154133
}
155-
156-
styles = StyleSheet.create({
157-
container: {
158-
flex: 1,
159-
alignItems: 'center',
160-
justifyContent: 'center',
161-
},
162-
status: {
163-
margin: 30,
164-
},
165-
});
166134
}
167135
136+
const styles = StyleSheet.create({
137+
container: {
138+
flex: 1,
139+
alignItems: 'center',
140+
justifyContent: 'center',
141+
},
142+
status: {
143+
margin: 30,
144+
},
145+
});
146+
168147
export default AccessibilityStatusExample;
169148
```
170149

@@ -273,7 +252,7 @@ Query whether a screen reader is currently enabled. Returns a promise which reso
273252
static removeEventListener(eventName, handler)
274253
```
275254

276-
Remove an event handler.
255+
> **Deprecated.** Use the `remove()` method on the event subscription returned by [`addEventListener()`](#addeventlistener).
277256
278257
---
279258

docs/appearance.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,4 @@ Add an event handler that is fired when appearance preferences change.
8787
static removeChangeListener(listener)
8888
```
8989

90-
Remove an event handler.
90+
> **Deprecated.** Use the `remove()` method on the event subscription returned by [`addChangeListener()`](#addchangelistener).

docs/appstate.md

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,24 @@ const AppStateExample = () => {
3636
const [appStateVisible, setAppStateVisible] = useState(appState.current);
3737
3838
useEffect(() => {
39-
AppState.addEventListener("change", _handleAppStateChange);
39+
const subscription = AppState.addEventListener("change", nextAppState => {
40+
if (
41+
appState.current.match(/inactive|background/) &&
42+
nextAppState === "active"
43+
) {
44+
console.log("App has come to the foreground!");
45+
}
46+
47+
appState.current = nextAppState;
48+
setAppStateVisible(appState.current);
49+
console.log("AppState", appState.current);
50+
});
4051
4152
return () => {
42-
AppState.removeEventListener("change", _handleAppStateChange);
53+
subscription.remove();
4354
};
4455
}, []);
4556
46-
const _handleAppStateChange = (nextAppState) => {
47-
if (
48-
appState.current.match(/inactive|background/) &&
49-
nextAppState === "active"
50-
) {
51-
console.log("App has come to the foreground!");
52-
}
53-
54-
appState.current = nextAppState;
55-
setAppStateVisible(appState.current);
56-
console.log("AppState", appState.current);
57-
};
58-
5957
return (
6058
<View style={styles.container}>
6159
<Text>Current state is: {appStateVisible}</Text>
@@ -89,23 +87,24 @@ class AppStateExample extends Component {
8987
};
9088
9189
componentDidMount() {
92-
AppState.addEventListener("change", this._handleAppStateChange);
90+
this.appStateSubscription = AppState.addEventListener(
91+
"change",
92+
nextAppState => {
93+
if (
94+
this.state.appState.match(/inactive|background/) &&
95+
nextAppState === "active"
96+
) {
97+
console.log("App has come to the foreground!");
98+
}
99+
this.setState({ appState: nextAppState });
100+
}
101+
);
93102
}
94103
95104
componentWillUnmount() {
96-
AppState.removeEventListener("change", this._handleAppStateChange);
105+
this.appStateSubscription.remove();
97106
}
98107
99-
_handleAppStateChange = nextAppState => {
100-
if (
101-
this.state.appState.match(/inactive|background/) &&
102-
nextAppState === "active"
103-
) {
104-
console.log("App has come to the foreground!");
105-
}
106-
this.setState({ appState: nextAppState });
107-
};
108-
109108
render() {
110109
return (
111110
<View style={styles.container}>
@@ -163,8 +162,6 @@ addEventListener(type, handler);
163162

164163
Add a handler to AppState changes by listening to the `change` event type and providing the handler
165164

166-
TODO: now that AppState is a subclass of NativeEventEmitter, we could deprecate `addEventListener` and `removeEventListener` and use `addListener` and `listener.remove()` directly. That will be a breaking change though, as both the method and event names are different (addListener events are currently required to be globally unique).
167-
168165
---
169166

170167
### `removeEventListener()`
@@ -173,7 +170,7 @@ TODO: now that AppState is a subclass of NativeEventEmitter, we could deprecate
173170
removeEventListener(type, handler);
174171
```
175172

176-
Remove a handler by passing the `change` event type and the handler
173+
> **Deprecated.** Use the `remove()` method on the event subscription returned by [`addEventListener()`](#addeventlistener).
177174
178175
## Properties
179176

docs/dimensions.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ const screen = Dimensions.get("screen");
3737
const App = () => {
3838
const [dimensions, setDimensions] = useState({ window, screen });
3939
40-
const onChange = ({ window, screen }) => {
41-
setDimensions({ window, screen });
42-
};
43-
4440
useEffect(() => {
45-
Dimensions.addEventListener("change", onChange);
41+
const subscription = Dimensions.addEventListener(
42+
"change",
43+
({ window, screen }) => {
44+
setDimensions({ window, screen });
45+
}
46+
);
4647
return () => {
47-
Dimensions.removeEventListener("change", onChange);
48+
subscription.remove();
4849
};
4950
});
5051
@@ -90,11 +91,11 @@ class App extends Component {
9091
};
9192
9293
componentDidMount() {
93-
Dimensions.addEventListener("change", this.onChange);
94+
this.dimensionsSubscription = Dimensions.addEventListener("change", this.onChange);
9495
}
9596
9697
componentWillUnmount() {
97-
Dimensions.removeEventListener("change", this.onChange);
98+
this.dimensionsSubscription.remove();
9899
}
99100
100101
render() {
@@ -167,7 +168,7 @@ Example: `const {height, width} = Dimensions.get('window');`
167168
static removeEventListener(type, handler)
168169
```
169170

170-
Remove an event handler.
171+
> **Deprecated.** Use the `remove()` method on the event subscription returned by [`addEventListener()`](#addeventlistener).
171172
172173
---
173174

docs/keyboard.md

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,22 @@ import React, { useState, useEffect } from "react";
1919
import { Keyboard, Text, TextInput, StyleSheet, View } from "react-native";
2020
2121
const Example = () => {
22+
const [keyboardStatus, setKeyboardStatus] = useState(undefined);
23+
2224
useEffect(() => {
23-
Keyboard.addListener("keyboardDidShow", _keyboardDidShow);
24-
Keyboard.addListener("keyboardDidHide", _keyboardDidHide);
25+
const showSubscription = Keyboard.addListener("keyboardDidShow", () => {
26+
setKeyboardStatus("Keyboard Shown");
27+
});
28+
const hideSubscription = Keyboard.addListener("keyboardDidHide", () => {
29+
setKeyboardStatus("Keyboard Hidden");
30+
});
2531
26-
// cleanup function
2732
return () => {
28-
Keyboard.removeListener("keyboardDidShow", _keyboardDidShow);
29-
Keyboard.removeListener("keyboardDidHide", _keyboardDidHide);
33+
showSubscription.remove();
34+
hideSubscription.remove();
3035
};
3136
}, []);
3237
33-
const [keyboardStatus, setKeyboardStatus] = useState(undefined);
34-
const _keyboardDidShow = () => setKeyboardStatus("Keyboard Shown");
35-
const _keyboardDidHide = () => setKeyboardStatus("Keyboard Hidden");
36-
3738
return (
3839
<View style={style.container}>
3940
<TextInput
@@ -78,27 +79,23 @@ class Example extends Component {
7879
};
7980
8081
componentDidMount() {
81-
this.keyboardDidShowListener = Keyboard.addListener(
82+
this.keyboardDidShowSubscription = Keyboard.addListener(
8283
'keyboardDidShow',
83-
this._keyboardDidShow,
84+
() => {
85+
this.setState({ keyboardStatus: 'Keyboard Shown' });
86+
},
8487
);
85-
this.keyboardDidHideListener = Keyboard.addListener(
88+
this.keyboardDidHideSubscription = Keyboard.addListener(
8689
'keyboardDidHide',
87-
this._keyboardDidHide,
90+
() => {
91+
this.setState({ keyboardStatus: 'Keyboard Hidden' });
92+
},
8893
);
8994
}
9095
9196
componentWillUnmount() {
92-
this.keyboardDidShowListener.remove();
93-
this.keyboardDidHideListener.remove();
94-
}
95-
96-
_keyboardDidShow = () => {
97-
this.setState({ keyboardStatus: 'Keyboard Shown' });
98-
}
99-
100-
_keyboardDidHide = () => {
101-
this.setState({ keyboardStatus: 'Keyboard Hidden' });
97+
this.keyboardDidShowSubscription.remove();
98+
this.keyboardDidHideSubscription.remove();
10299
}
103100
104101
render() {
@@ -183,7 +180,7 @@ This can be any of the following:
183180
static removeListener(eventName, callback)
184181
```
185182

186-
Removes a specific listener.
183+
> **Deprecated.** Use the `remove()` method on the event subscription returned by [`addListener()`](#addlistener).
187184
188185
**Parameters:**
189186

docs/linking.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ The method returns a `Promise` object. If the user confirms the open dialog or t
377377
removeEventListener(type, handler);
378378
```
379379

380-
Remove a handler by passing the `url` event type and the handler.
380+
> **Deprecated.** Use the `remove()` method on the event subscription returned by [`addEventListener()`](#addeventlistener).
381381
382382
---
383383

0 commit comments

Comments
 (0)