@@ -42,19 +42,28 @@ export function useKeyboard() {
4242 }
4343
4444 useEffect ( ( ) => {
45- Keyboard . addListener ( 'keyboardWillShow' , handleKeyboardWillShow )
46- Keyboard . addListener ( 'keyboardDidShow' , handleKeyboardDidShow )
47- Keyboard . addListener ( 'keyboardWillHide' , handleKeyboardWillHide )
48- Keyboard . addListener ( 'keyboardDidHide' , handleKeyboardDidHide )
45+ const subscriptions = [
46+ Keyboard . addListener ( 'keyboardWillShow' , handleKeyboardWillShow ) ,
47+ Keyboard . addListener ( 'keyboardDidShow' , handleKeyboardDidShow ) ,
48+ Keyboard . addListener ( 'keyboardWillHide' , handleKeyboardWillHide ) ,
49+ Keyboard . addListener ( 'keyboardDidHide' , handleKeyboardDidHide ) ,
50+ ]
4951
5052 return ( ) => {
51- Keyboard . removeListener ( 'keyboardWillShow' , handleKeyboardWillShow )
52- Keyboard . removeListener ( 'keyboardDidShow' , handleKeyboardDidShow )
53- Keyboard . removeListener ( 'keyboardWillHide' , handleKeyboardWillHide )
54- Keyboard . removeListener ( 'keyboardDidHide' , handleKeyboardDidHide )
53+ if ( Keyboard . removeListener ) {
54+ // React Native < 0.65
55+ Keyboard . removeListener ( 'keyboardWillShow' , handleKeyboardWillShow )
56+ Keyboard . removeListener ( 'keyboardDidShow' , handleKeyboardDidShow )
57+ Keyboard . removeListener ( 'keyboardWillHide' , handleKeyboardWillHide )
58+ Keyboard . removeListener ( 'keyboardDidHide' , handleKeyboardDidHide )
59+ } else {
60+ // React Native >= 0.65
61+ for ( const subscription of subscriptions ) {
62+ subscription . remove ( )
63+ }
64+ }
5565 }
5666 } , [ ] )
57-
5867 return {
5968 keyboardShown : shown ,
6069 coordinates,
0 commit comments