Skip to content

Instantly share code, notes, and snippets.

@niraj-khatiwada
Created September 13, 2022 13:56
Show Gist options
  • Save niraj-khatiwada/c2c4371ec038a6efb3245e487e20da79 to your computer and use it in GitHub Desktop.
Save niraj-khatiwada/c2c4371ec038a6efb3245e487e20da79 to your computer and use it in GitHub Desktop.
import React from 'react';
import {Keyboard} from 'react-native';
function useKeyboardHeight() {
const [keyboardHeight, setKeyboardHeight] = React.useState(0);
function onKeyboardDidShow(e) {
setKeyboardHeight(e.endCoordinates.height);
}
function onKeyboardDidHide() {
setKeyboardHeight(0);
}
React.useEffect(() => {
Keyboard.addListener('keyboardDidShow', onKeyboardDidShow);
Keyboard.addListener('keyboardDidHide', onKeyboardDidHide);
return () => {
Keyboard.removeAllListeners('keyboardDidShow');
Keyboard.removeAllListeners('keyboardDidHide');
};
}, []);
return [keyboardHeight];
}
export default useKeyboardHeight;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment