Created
March 5, 2020 18:11
-
-
Save bogoslavskiy/a5c55e16de245312e37bbb3b35053226 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 from 'react'; | |
import { Keyboard } from 'react-native'; | |
export const useKeyboard = (): [boolean, () => void] => { | |
const [visible, setVisible] = React.useState(false); | |
const dismiss = () => { | |
Keyboard.dismiss(); | |
setVisible(false); | |
}; | |
React.useEffect(() => { | |
const onKeyboardDidShow = () => { | |
setVisible(true); | |
}; | |
const onKeyboardDidHide = () => { | |
setVisible(false); | |
}; | |
Keyboard.addListener('keyboardWillShow', onKeyboardDidShow); | |
Keyboard.addListener('keyboardWillHide', onKeyboardDidHide); | |
return () => { | |
Keyboard.removeListener('keyboardWillShow', onKeyboardDidShow); | |
Keyboard.removeListener('keyboardWillHide', onKeyboardDidHide); | |
}; | |
}, []); | |
return [visible, dismiss]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment