Skip to content

Commit

Permalink
Add an example which use removeEventListener (facebook#352)
Browse files Browse the repository at this point in the history
and replace androidBackHandler with backHandler.
  • Loading branch information
christoph-jerolimov authored and charpeni committed May 10, 2018
1 parent eebe7fb commit 37739db
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions docs/backhandler.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,35 @@ BackHandler.addEventListener('hardwareBackPress', function() {
});
```

Example 2:
Lifecycle example:

```javascript
componentDidMount() {
this.androidBackHandler = BackHandler.addEventListener('hardwareBackPress', () => {
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
}

componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress);
}

handleBackPress = () => {
this.goBack(); // works best when the goBack is async
return true;
}
```

Lifecycle alternative:

```javascript
componentDidMount() {
this.backHandler = BackHandler.addEventListener('hardwareBackPress', () => {
this.goBack(); // works best when the goBack is async
return true;
});
}

componentWillUnmount() {
this.androidBackHandler.remove();
this.backHandler.remove();
}
```

Expand Down

0 comments on commit 37739db

Please sign in to comment.