Navigate back to the homepage

React Native Textinput

Infinitbility
Textinput
January 28th, 2021 · 1 min read

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

Advances

Error & Issue Solution

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.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

More articles from Infinitbility

React Native Text

React Native Tutorial - React Native Text

January 27th, 2021 · 1 min read

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]