Reputation: 1
import {Keyboard} from 'react-native';
Keyboard.addListener(
'keyboardWillShow',
keyboardWillShow
);
am trying to listen to the keyboard events willShow and willHide and then do something about the events
Upvotes: 0
Views: 147
Reputation: 458
Try the following function, it returns a boolean based on if the keyboard is open or not, and you can add actions to it
import {useEffect, useState} from 'react';
import {Keyboard} from 'react-native';
export function useKeyboard() {
const [isKeyboardVisible, setKeyboardVisible] = useState(false);
useEffect(() => {
const keyboardWillShowListener = Keyboard.addListener(
'keyboardWillShow',
() => {
setKeyboardVisible(true); // or some other action
},
);
const keyboardWillHideListener = Keyboard.addListener(
'keyboardWillHide',
() => {
setKeyboardVisible(false); // or some other action
},
);
return () => {
keyboardWillHideListener.remove();
keyboardWillShowListener.remove();
};
}, []);
return isKeyboardVisible;
}
Upvotes: 1