Skip to content

Instantly share code, notes, and snippets.

@bogoslavskiy
Created March 5, 2020 18:11
Show Gist options
  • Save bogoslavskiy/a5c55e16de245312e37bbb3b35053226 to your computer and use it in GitHub Desktop.
Save bogoslavskiy/a5c55e16de245312e37bbb3b35053226 to your computer and use it in GitHub Desktop.
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