Created
April 6, 2021 17:27
-
-
Save sturmenta/3441f635fe8c6cb37d017a5df5679b1c to your computer and use it in GitHub Desktop.
react-native KeyboardListener
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
import React, {useEffect} from 'react'; | |
import {Keyboard} from 'react-native'; | |
const KeyboardListener: React.FC<{ | |
onDidShow: (e: any) => void; | |
onDidHide: (e: any) => void; | |
}> = ({onDidShow, onDidHide}) => { | |
useEffect(() => { | |
if (onDidShow) Keyboard.addListener('keyboardDidShow', onDidShow); | |
if (onDidHide) Keyboard.addListener('keyboardDidHide', onDidHide); | |
return () => { | |
Keyboard.removeListener('keyboardDidShow', onDidShow); | |
Keyboard.removeListener('keyboardDidHide', onDidHide); | |
}; | |
}, []); | |
return null; | |
}; | |
export default KeyboardListener; | |
// using like: <KeyboardListener onDidShow={() => setKeyboardOpen(true)} onDidHide={() => setKeyboardOpen(false)} /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment