useKeyboardState
useKeyboardState is a hook which gives an access to current keyboard state. This hook combines data from KeyboardController.state() and KeyboardController.isVisible() methods and makes it reactive (i. e. triggers a re-render when keyboard state/visibility has changed).
Donβt use state from useKeyboardState inside event handlers. It will cause unnecessary re-renders. See common pitfalls section for more details and alternatives.
Make sure that if you want to animate something based on keyboard presence then you've seen optimize animation section.
useKeyboardState allows you to pass a selector function to pick only the necessary part of the keyboard state data. This is a powerful technique to prevent unnecessary re-renders of your component when only a specific property of the keyboard state changes.
const appearance = useKeyboardState((state) => state.appearance);
In this example, your component will only re-render when the appearance property of the KeyboardState changes, rather than for any change in the entire state object.
Data structureβ
The KeyboardState is represented by following structure:
type KeyboardState = {
isVisible: boolean;
height: number;
duration: number; // duration of the animation
timestamp: number; // timestamp of the event from native thread
target: number; // tag of the focused `TextInput`
type: string; // `keyboardType` property from focused `TextInput`
appearance: string; // `keyboardAppearance` property from focused `TextInput`
};