This Article Part of React Native Tutorial Series want to start from scrach follow below link
https://infinitbility.github.io/react-native/table-of-contents
In This Article, we learn textinput and their props with examples.
Textinput or input
Textinput component uses to get data to users like their personal details, email, password, etc.
The below example explains to handle textinput in react native.
import React, { Component } from 'react';
import {
StyleSheet,
View,
Text,
SafeAreaView,
TextInput
} from 'react-native';
class App extends Component {
constructor(props) {
super(props);
this.state = {
name: ''
};
}
render() {
const { name } = this.state;
return (
<SafeAreaView style={{ flex: 1 }}>
<View style={styles.container}>
<Text>write your name</Text>
<TextInput
value={name}
onChangeText={(name) => this.setState({name: name})}
placeholder={'name'}
style={styles.input}
/>
<Text style={{ color: 'blue' }}>{name}</Text>
</View>
</SafeAreaView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
marginTop: 20,
backgroundColor: '#ffffff',
},
input: {
width: 250,
height: 44,
padding: 10,
marginTop: 20,
marginBottom: 10,
backgroundColor: '#e8e8e8'
},
});
export default App;
Textarea or multiline textinput
if you are web developer you familier with Textarea but in react you will mange textarea features also in textinput field.
Add below props in <TextInput>
component.
<TextInput
...
multiline
numberOfLines={4}
...
/>
for show bigger textinput increase height value.
Common Q & A
Common Q & A section explain commonly question ask on google.
How do you use REF in Textinput react native?
using textinput ref we can handle focus, blur, and clear data of textinput programmatically.
Learn with example.
Step 1: create textinput ref.
constructor(props) {
super(props);
this.textinput = React.createRef(); // add line on constructor
}
Step 2: define ref in textinput
<TextInput
...
ref={ref => (this.textinput = ref)} // define ref
...
/>
Step 3: Use ref for focus, blur, and clear data complete ref example;
import React, { Component } from 'react';
import {
StyleSheet,
View,
Text,
SafeAreaView,
TextInput,
TouchableOpacity
} from 'react-native';
class App extends Component {
constructor(props) {
super(props);
this.textinput = React.createRef();
this.state = {
name: ''
};
}
clickToFocus = () => {
this.textinput.focus();
};
clickToBlur = () => {
this.textinput.blur();
};
clickToClear = () => {
this.textinput.clear();
};
render() {
const { name } = this.state;
return (
<SafeAreaView style={{ flex: 1 }}>
<View style={styles.container}>
<Text>write your name</Text>
<TextInput
ref={ref => (this.textinput = ref)}
value={name}
onChangeText={(name) => this.setState({name: name})}
placeholder={'name'}
style={styles.input}
/>
<TouchableOpacity onPress={this.clickToFocus}>{'Click To Focus'}</TouchableOpacity>
<TouchableOpacity onPress={this.clickToBlur}>{'Click To Blur'}</TouchableOpacity>
<TouchableOpacity onPress={this.clickToClear}>{'Click To Clear'}</TouchableOpacity>
</View>
</SafeAreaView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
marginTop: 20,
backgroundColor: '#ffffff',
},
input: {
width: 250,
height: 44,
padding: 10,
marginTop: 20,
marginBottom: 10,
backgroundColor: '#e8e8e8'
},
});
export default App;
How do I add a border to Textinput in react native?
Add below StyleSheet code on your textinput style.
borderColor: 'black',
borderWidth:1,
How do I disable Textinput in react native?
add editable props in your textinput field.
<TextInput
...
editable={false}
...
/>
How do I add a password to Textinput in react native?
add secureTextEntry
props in your textinput field. Does not work with multiline={true}
.
<TextInput
...
secureTextEntry={true}
...
/>
How do I change text color in Textinput react native?
Add below StyleSheet code on your textinput style.
color: 'green'
More From React Native Tutorial
Basics
1. Introduction To React Native
2. React Native Environment Setup using expo
3. React Native Environment Setup for windows
4. React Native Environment setup on Mac OS
5. React Native Environment setup on linux
6. React Native Project Structure
Advances
4. React Native DatepickerAndroid
5. React native ScrollView scroll to position
6. How to align icon with text in react native
8. React Native Firebase Crashlytics
Error & Issue Solution
1. Task :app:transformDexArchiveWithDexMergerForDebug FAILED In React Native
2. Expiring Daemon because JVM heap space is exhausted In React Native
3. Task :app:transformNativeLibsWithMergeJniLibsForDebug FAILED In React Native
5. App crashed immediately after install react native video or track player
6. how to delete SQLite database in android react native
7. React native material dropdown twice click issue
8. How to get the current route in react-navigation?