Open In App

React Native Text Input Component

In this article, We are going to see how to create a TextInput in react-native. For this, we are going to use the TextInput component. It is a basic component that is used to collect data from users. 

For creating a TextInput in react native we have to import the TextInput component from React Native.



import { TextInput } from 'react-native'

Syntax:

<TextInput  
  // Define TextInput property
/>        

Props for TextInput Component:



Now let’s start with the implementation:

Project Structure:

Example: Now let’s implement the TextInput component. Inside that component whenever there is any text change it calls the function handleText that will set the name state to text. This text will be written in the Text component. 




import React, { Component } from 'react';
import {View,Text,TextInput } from 'react-native';
  
export default class App extends Component {
  state={
    name:"",
  }
  handleText=(text)=>{
    this.setState({name:text});
  }
  render() {
    return (
      <View>
      <TextInput
        style={{height: 50, borderColor: 'red'
                borderWidth: 3,margin:10,
        fontSize:35,}}
        onChangeText={(text) =>this.handleText(text)}
        placeholder="Enter Name"
        value={this.state.text}
      />
      <Text style={{fontSize:32,color:'green'}}>
        Your name is :{this.state.name}
      </Text>
      </View>
    );
  }
}

Start the server by using the following command.

npm run android

Output: If your emulator did not open automatically then you need to do it manually. First, go to your android studio and run the emulator. Now start the server again. 

Reference: https://reactnative.dev/docs/textinput


Article Tags :