Skip to content

Instantly share code, notes, and snippets.

@cornedor
Last active February 19, 2016 10:24
Show Gist options
  • Save cornedor/e4d77e5742bdeedccb04 to your computer and use it in GitHub Desktop.
Save cornedor/e4d77e5742bdeedccb04 to your computer and use it in GitHub Desktop.
Wrapper around React Native's TextInput to make getting the text easier.
import React, {
Component,
PropTypes,
TextInput as NativeTextInput
} from 'react-native';
/**
* Wrapper for the native TextInput which adds getValue()
**/
class TextInput extends Component {
constructor(props) {
super(props);
this.state = {
text: '',
};
this.onChange = this._onChange.bind(this);
}
getValue() {
return this.state.text;
}
_onChange(text) {
this.setState({ text });
if (this.props.onChangeText !== undefined) {
this.props.onChangeText(text);
}
}
render() {
return (
<NativeTextInput {...this.props} onChangeText={this.onChange} />
);
}
}
TextInput.propTypes = {
onChangeText: PropTypes.func,
};
export default TextInput;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment