UNPKG

react-native-keyboard-controller

Version:

Keyboard manager which works in identical way on both iOS and Android

27 lines (26 loc) 1.06 kB
import { useEffect, useState } from "react"; import { KeyboardEvents } from "../../bindings"; import { KeyboardController } from "../../module"; const EVENTS = ["keyboardDidShow", "keyboardDidHide"]; const getLatestState = () => ({ ...KeyboardController.state(), isVisible: KeyboardController.isVisible() }); export const useKeyboardState = () => { const [state, setState] = useState(getLatestState); useEffect(() => { const subscriptions = EVENTS.map(event => KeyboardEvents.addListener(event, () => // state will be updated by global listener first, // so we simply read it and don't derive data from the event setState(getLatestState))); // we might have missed an update between reading a value in render and // `addListener` in this handler, so we set it here. If there was // no change, React will filter out this update as a no-op. setState(getLatestState); return () => { subscriptions.forEach(subscription => subscription.remove()); }; }, []); return state; }; //# sourceMappingURL=index.js.map