Keyboard Events for React Native
From time to time you might need to react to device's keyboard showing up or hiding.
This library from Johannes Lumpe will help you handle just that.
react-native-keyboardevents
Keyboard events for react-native
How to move a view when the keyboard shows up
var React = require("react-native");
var KeyboardEvents = require("react-native-keyboardevents");
var KeyboardEventEmitter = KeyboardEvents.Emitter;
class YourComponent extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
keyboardSpace: 0,
};
this.updateKeyboardSpace = this.updateKeyboardSpace.bind(this);
this.resetKeyboardSpace = this.resetKeyboardSpace.bind(this);
}
updateKeyboardSpace(frames) {
this.setState({ keyboardSpace: frames.end.height });
}
resetKeyboardSpace() {
this.setState({ keyboardSpace: 0 });
}
componentDidMount() {
KeyboardEventEmitter.on(
KeyboardEvents.KeyboardDidShowEvent,
this.updateKeyboardSpace
);
KeyboardEventEmitter.on(
KeyboardEvents.KeyboardWillHideEvent,
this.resetKeyboardSpace
);
}
componentWillUnmount() {
KeyboardEventEmitter.off(
KeyboardEvents.KeyboardDidShowEvent,
this.updateKeyboardSpace
);
KeyboardEventEmitter.off(
KeyboardEvents.KeyboardWillHideEvent,
this.resetKeyboardSpace
);
}
render() {
// create your content here
return (
<View>
// add your content here // when the keyboard is shown, this spacer will
push up all your other content
<View style={{ height: this.state.keyboardSpace }}></View>
</View>
);
}
}