Skip to content

Instantly share code, notes, and snippets.

@PavanKu
Created May 19, 2019 18:32
Show Gist options
  • Save PavanKu/6d7d173c7bc094533740879f73b95879 to your computer and use it in GitHub Desktop.
Save PavanKu/6d7d173c7bc094533740879f73b95879 to your computer and use it in GitHub Desktop.
Simple Input Text in React Native
import React from 'react';
import { View, Text, TextInput, StyleSheet } from "react-native";
class SimpleTextInput extends React.Component {
constructor(props) {
super(props);
this.state = { text: '', isSubmitted: false };
}
handleChangeText = (text) => {
this.setState({ text, isSubmitted: false });
}
handleSubmitText = () => {
this.setState({ isSubmitted: true });
}
render() {
const { text, isSubmitted } = this.state;
return (
<View style={styles.container}>
<TextInput
style={styles.input}
placeholder={'Type here...'}
onChangeText={this.handleChangeText}
onSubmitEditing={this.handleSubmitText}/>
<Text style={styles.text}>{isSubmitted?`User has typed: ${text}`:'User is typing'}</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
alignSelf: 'stretch',
margin: 10,
padding: 10,
borderColor: '#ccc',
borderWidth: 1,
borderRadius: 5,
backgroundColor: '#eaf7fd'
},
text: {
fontSize: 14,
fontWeight: 'bold',
color: '#015169'
},
input: {
height: 40
}
});
export default SimpleTextInput;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment