-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use remove method on the event subscription (#2923)
- Loading branch information
1 parent
8c8e54d
commit 0c35337
Showing
2 changed files
with
56 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import * as React from 'react'; | ||
import { Keyboard, NativeEventSubscription, Platform } from 'react-native'; | ||
|
||
type Props = { | ||
onShow: () => void; | ||
onHide: () => void; | ||
}; | ||
export default function useIsKeyboardShown({ onShow, onHide }: Props) { | ||
React.useEffect(() => { | ||
let willShowSubscription: NativeEventSubscription | undefined; | ||
let willHideSubscription: NativeEventSubscription | undefined; | ||
let didShowSubscription: NativeEventSubscription | undefined; | ||
let didHideSubscription: NativeEventSubscription | undefined; | ||
|
||
if (Platform.OS === 'ios') { | ||
willShowSubscription = Keyboard.addListener('keyboardWillShow', onShow); | ||
willHideSubscription = Keyboard.addListener('keyboardWillHide', onHide); | ||
} else { | ||
didShowSubscription = Keyboard.addListener('keyboardDidShow', onShow); | ||
didHideSubscription = Keyboard.addListener('keyboardDidHide', onHide); | ||
} | ||
|
||
return () => { | ||
if (Platform.OS === 'ios') { | ||
if (willShowSubscription?.remove) { | ||
willShowSubscription.remove(); | ||
} else { | ||
Keyboard.removeListener('keyboardWillShow', onShow); | ||
} | ||
|
||
if (willHideSubscription?.remove) { | ||
willHideSubscription.remove(); | ||
} else { | ||
Keyboard.removeListener('keyboardWillHide', onHide); | ||
} | ||
} else { | ||
if (didShowSubscription?.remove) { | ||
didShowSubscription.remove(); | ||
} else { | ||
Keyboard.removeListener('keyboardDidShow', onShow); | ||
} | ||
|
||
if (didHideSubscription?.remove) { | ||
didHideSubscription.remove(); | ||
} else { | ||
Keyboard.removeListener('keyboardDidHide', onHide); | ||
} | ||
} | ||
}; | ||
}, [onHide, onShow]); | ||
} |