Skip to content

Commit cdc2e41

Browse files
authored
fix: Remove BackHandler.removeEventListener method for RN 0.77+ (#4475)
1 parent 2637718 commit cdc2e41

File tree

2 files changed

+52
-62
lines changed

2 files changed

+52
-62
lines changed

docs/backhandler.md

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,34 @@ The event subscriptions are called in reverse order (i.e. the last registered su
1515
## Pattern
1616

1717
```tsx
18-
BackHandler.addEventListener('hardwareBackPress', function () {
19-
/**
20-
* this.onMainScreen and this.goBack are just examples,
21-
* you need to use your own implementation here.
22-
*
23-
* Typically you would use the navigator here to go to the last state.
24-
*/
25-
26-
if (!this.onMainScreen()) {
27-
this.goBack();
18+
const subscription = BackHandler.addEventListener(
19+
'hardwareBackPress',
20+
function () {
2821
/**
29-
* When true is returned the event will not be bubbled up
30-
* & no other back action will execute
22+
* this.onMainScreen and this.goBack are just examples,
23+
* you need to use your own implementation here.
24+
*
25+
* Typically you would use the navigator here to go to the last state.
3126
*/
32-
return true;
33-
}
34-
/**
35-
* Returning false will let the event to bubble up & let other event listeners
36-
* or the system's default back action to be executed.
37-
*/
38-
return false;
39-
});
27+
28+
if (!this.onMainScreen()) {
29+
this.goBack();
30+
/**
31+
* When true is returned the event will not be bubbled up
32+
* & no other back action will execute
33+
*/
34+
return true;
35+
}
36+
/**
37+
* Returning false will let the event to bubble up & let other event listeners
38+
* or the system's default back action to be executed.
39+
*/
40+
return false;
41+
},
42+
);
43+
44+
// Unsubscribe the listener on unmount
45+
subscription.remove();
4046
```
4147

4248
## Example
@@ -126,14 +132,3 @@ static addEventListener(
126132
```tsx
127133
static exitApp();
128134
```
129-
130-
---
131-
132-
### `removeEventListener()`
133-
134-
```tsx
135-
static removeEventListener(
136-
eventName: BackPressEventName,
137-
handler: () => boolean | null | undefined,
138-
);
139-
```

website/versioned_docs/version-0.77/backhandler.md

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,34 @@ The event subscriptions are called in reverse order (i.e. the last registered su
1515
## Pattern
1616

1717
```tsx
18-
BackHandler.addEventListener('hardwareBackPress', function () {
19-
/**
20-
* this.onMainScreen and this.goBack are just examples,
21-
* you need to use your own implementation here.
22-
*
23-
* Typically you would use the navigator here to go to the last state.
24-
*/
25-
26-
if (!this.onMainScreen()) {
27-
this.goBack();
18+
const subscription = BackHandler.addEventListener(
19+
'hardwareBackPress',
20+
function () {
2821
/**
29-
* When true is returned the event will not be bubbled up
30-
* & no other back action will execute
22+
* this.onMainScreen and this.goBack are just examples,
23+
* you need to use your own implementation here.
24+
*
25+
* Typically you would use the navigator here to go to the last state.
3126
*/
32-
return true;
33-
}
34-
/**
35-
* Returning false will let the event to bubble up & let other event listeners
36-
* or the system's default back action to be executed.
37-
*/
38-
return false;
39-
});
27+
28+
if (!this.onMainScreen()) {
29+
this.goBack();
30+
/**
31+
* When true is returned the event will not be bubbled up
32+
* & no other back action will execute
33+
*/
34+
return true;
35+
}
36+
/**
37+
* Returning false will let the event to bubble up & let other event listeners
38+
* or the system's default back action to be executed.
39+
*/
40+
return false;
41+
},
42+
);
43+
44+
// Unsubscribe the listener on unmount
45+
subscription.remove();
4046
```
4147

4248
## Example
@@ -126,14 +132,3 @@ static addEventListener(
126132
```tsx
127133
static exitApp();
128134
```
129-
130-
---
131-
132-
### `removeEventListener()`
133-
134-
```tsx
135-
static removeEventListener(
136-
eventName: BackPressEventName,
137-
handler: () => boolean | null | undefined,
138-
);
139-
```

0 commit comments

Comments
 (0)