88 TouchableWithoutFeedback ,
99 ViewStyle ,
1010 View ,
11+ NativeEventSubscription ,
1112} from 'react-native' ;
1213import {
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 ( ) {
0 commit comments