Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
1.4 kB · 41 lines
TSX
123456789101112131415161718192021222324252627282930313233343536373839404142import { useEffect, useState } from "react";import { Keyboard } from "react-native";
export function useKeyboard() { const [isKeyboardVisible, setIsKeyboardVisible] = useState(false); const [keyboardHeight, setKeyboardHeight] = useState(0); useEffect(() => { const willShowSubscription = Keyboard.addListener( "keyboardWillShow", (e) => { // setIsKeyboardVisible(true); setKeyboardHeight(e.endCoordinates.height); console.log("keyboardWillShow", e.endCoordinates.height); }, ); const willHideSubscription = Keyboard.addListener( "keyboardWillHide", (e) => { // setIsKeyboardVisible(false); setKeyboardHeight(0); console.log("keyboardWillHide", e.endCoordinates.height); }, ); const showSubscription = Keyboard.addListener("keyboardDidShow", (e) => { setIsKeyboardVisible(true); setKeyboardHeight(e.endCoordinates.height); console.log("keyboardDidShow", e.endCoordinates.height); }); const hideSubscription = Keyboard.addListener("keyboardDidHide", () => { setIsKeyboardVisible(false); setKeyboardHeight(0); }); return () => { showSubscription.remove(); hideSubscription.remove(); willShowSubscription.remove(); }; }, []);
return { isKeyboardVisible, keyboardHeight };}