Created
October 11, 2019 11:27
-
-
Save lukebrandonfarrell/8238a83eb9490b65a4f4ed411b4846d3 to your computer and use it in GitHub Desktop.
Hook to return if the react-native keyboard is open / closed
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, { useState, useRef } from "react"; | |
import { Keyboard } from "react-native"; | |
/** | |
* Returns if the keyboard is open / closed | |
* | |
* @return {bool} isOpen | |
*/ | |
export function useKeyboardStatus(){ | |
const [isOpen, setIsOpen] = useState(false); | |
const keyboardShowListener = useRef(null); | |
const keyboardHideListener = useRef(null); | |
useEffect(() => { | |
keyboardShowListener.current = Keyboard.addListener('keyboardDidShow', () => setIsOpen(true)); | |
keyboardHideListener.current = Keyboard.addListener('keyboardDidHide', () => setIsOpen(false)); | |
return () => { | |
keyboardShowListener.current.remove(); | |
keyboardHideListener.current.remove(); | |
} | |
}) | |
return isOpen; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment