UNPKG

react-native-rnevents

Version:

The package was created for React Native. It allows to open webpages, make a phone call, call using Viber, Whatsapp and Telegram, take a keyboard event for hooks and make hooks components to work.

46 lines (37 loc) 1.52 kB
/************************************************************************ * Copyright (c) Amur * https://github.com/AmurKhoyetsyan/react-native-rnevents.git * * React native Keyboard events for hooks * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. *************************************************************************/ import React, { useState, useEffect } from "react"; import { Keyboard } from "react-native"; export const KeyboardEvent = (config = {}) => { const { useWillShow = false, useWillHide = false } = config; const [visible, setVisible] = useState(false); const showEvent = useWillShow ? 'keyboardWillShow' : 'keyboardDidShow'; const hideEvent = useWillHide ? 'keyboardWillHide' : 'keyboardDidHide'; const dismiss = () => { Keyboard.dismiss(); setVisible(false); } useEffect(() => { const onKeyboardShow = () => { setVisible(true); } const onKeyboardHide = () => { setVisible(false); } Keyboard.addListener(showEvent, onKeyboardShow); Keyboard.addListener(hideEvent, onKeyboardHide); return () => { Keyboard.removeListener(showEvent, onKeyboardShow); Keyboard.removeListener(hideEvent, onKeyboardHide); }; }, [useWillShow, useWillHide]); return [visible, dismiss]; }; export default KeyboardEvent;