Navigate back to the homepage

React Native Textinput

Infinitbility
Textinput
January 28th, 2021 · 1 min read
React Native Textinput

This Article Part of React Native Tutorial Series want to start from scrach follow below link

https://infinitbility.com/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.

1import React, { Component } from 'react';
2import {
3 StyleSheet,
4 View,
5 Text,
6 SafeAreaView,
7 TextInput
8} from 'react-native';
9
10class App extends Component {
11
12 constructor(props) {
13 super(props);
14 this.state = {
15 name: ''
16 };
17 }
18
19 render() {
20 const { name } = this.state;
21 return (
22 <SafeAreaView style={{ flex: 1 }}>
23 <View style={styles.container}>
24 <Text>write your name</Text>
25 <TextInput
26 value={name}
27 onChangeText={(name) => this.setState({name: name})}
28 placeholder={'name'}
29 style={styles.input}
30 />
31 <Text style={{ color: 'blue' }}>{name}</Text>
32 </View>
33 </SafeAreaView>
34 );
35 }
36}
37
38const styles = StyleSheet.create({
39 container: {
40 flex: 1,
41 alignItems: 'center',
42 marginTop: 20,
43 backgroundColor: '#ffffff',
44 },
45 input: {
46 width: 250,
47 height: 44,
48 padding: 10,
49 marginTop: 20,
50 marginBottom: 10,
51 backgroundColor: '#e8e8e8'
52 },
53});
54
55export 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.

1<TextInput
2 ...
3 multiline
4 numberOfLines={4}
5 ...
6/>

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.

1constructor(props) {
2 super(props);
3 this.textinput = React.createRef(); // add line on constructor
4}

Step 2: define ref in textinput

1<TextInput
2 ...
3 ref={ref => (this.textinput = ref)} // define ref
4 ...
5/>

Step 3: Use ref for focus, blur, and clear data complete ref example;

1import React, { Component } from 'react';
2import {
3 StyleSheet,
4 View,
5 Text,
6 SafeAreaView,
7 TextInput,
8 TouchableOpacity
9} from 'react-native';
10
11class App extends Component {
12
13 constructor(props) {
14 super(props);
15 this.textinput = React.createRef();
16 this.state = {
17 name: ''
18 };
19 }
20
21 clickToFocus = () => {
22 this.textinput.focus();
23 };
24
25 clickToBlur = () => {
26 this.textinput.blur();
27 };
28
29 clickToClear = () => {
30 this.textinput.clear();
31 };
32
33 render() {
34 const { name } = this.state;
35 return (
36 <SafeAreaView style={{ flex: 1 }}>
37 <View style={styles.container}>
38 <Text>write your name</Text>
39 <TextInput
40 ref={ref => (this.textinput = ref)}
41 value={name}
42 onChangeText={(name) => this.setState({name: name})}
43 placeholder={'name'}
44 style={styles.input}
45 />
46 <TouchableOpacity onPress={this.clickToFocus}>{'Click To Focus'}</TouchableOpacity>
47 <TouchableOpacity onPress={this.clickToBlur}>{'Click To Blur'}</TouchableOpacity>
48 <TouchableOpacity onPress={this.clickToClear}>{'Click To Clear'}</TouchableOpacity>
49 </View>
50 </SafeAreaView>
51 );
52 }
53}
54
55const styles = StyleSheet.create({
56 container: {
57 flex: 1,
58 alignItems: 'center',
59 marginTop: 20,
60 backgroundColor: '#ffffff',
61 },
62 input: {
63 width: 250,
64 height: 44,
65 padding: 10,
66 marginTop: 20,
67 marginBottom: 10,
68 backgroundColor: '#e8e8e8'
69 },
70});
71
72export default App;

How do I add a border to Textinput in react native?

Add below StyleSheet code on your textinput style.

1borderColor: 'black',
2borderWidth:1,

How do I disable Textinput in react native?

add editable props in your textinput field.

1<TextInput
2 ...
3 editable={false}
4 ...
5/>

How do I add a password to Textinput in react native?

add secureTextEntry props in your textinput field. Does not work with multiline={true}.

1<TextInput
2 ...
3 secureTextEntry={true}
4 ...
5/>

How do I change text color in Textinput react native?

Add below StyleSheet code on your textinput style.

1color: '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
  7. React Native State
  8. React Native Props
  9. React Native Styling
  10. React Native Flexbox
  11. React Native Text
  12. React Native Textinput
  13. React Native Commands
  14. React Native ScrollView

Advances

  1. React Native Dark Mode
  2. React Native Fonts
  3. React Native SQLite
  4. React Native DatepickerAndroid
  5. React native ScrollView scroll to position
  6. How to align icon with text in react native
  7. React Native Image
  8. React Native Firebase Crashlytics
  9. React Native Async Storage
  10. React Native Share

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
  4. Unable to determine the current character, it is not a string, number, array, or object 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?
  9. how to disable drawer on the drawer navigation screen?
  10. Image not showing in ios 14 react native

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

More articles from Infinitbility

React Native Text

React Native Text

React Native Tutorial - React Native Text

January 27th, 2021 · 1 min read
React Native Flexbox

React Native Flexbox

React Native Tutorial - React Native Flexbox

January 24th, 2021 · 3 min read
© 2021 Infinitbility
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]