TextInput

It is a React Native component used to add user-defined text into an application. It is similar to <input /> of HTML.
Let's see an example.
Step 1
Change the App.js code and make it simple.
App.js code
  1. import React, { Component } from "react";
  2. import { View, Text } from "react-native";
  3. class App extends Component {
  4. render() {
  5. return (
  6. <View>
  7. <Text> Hi React Native</Text>
  8. </View>
  9. );
  10. }
  11. }
  12. export default App;
Output
Text Input In React Native
Step 2
Import TextInput from "react-native" and add TextInput component in the View component of React Native like below.
Text Input In React Native
Output
Text Input In React Native
Step 3
Import StyleSheet, View, Text from “react-native” for adding style in React Native component and displaying the text on the screen. The onChangeText function gets executed whenever the user types or enters text in the TextInput. So, I have added onChangeText property for setting the value of the state in the below example, and added Placeholder and PlaceHolderText color property.
Code - App.js
  1. import React, { Component } from "react";
  2. import { StyleSheet, View, TextInput, Text } from "react-native";
  3. class App extends Component {
  4. state = {
  5. myComment: ""
  6. }
  7. handleTextChange = (inputText) => {
  8. this.setState({ myComment: inputText })
  9. }
  10. render() {
  11. return (
  12. <View style={styles.container}>
  13. <TextInput
  14. style={styles.textInputStyle}
  15. onChangeText={this.handleTextChange}
  16. placeholder="Enter your comment"
  17. placeholderTextColor="red"
  18. />
  19. <Text>Ouptput of your TextInput :-</Text>
  20. <Text style={styles.textOutputStyle}>{this.state.myComment}</Text>
  21. </View>
  22. );
  23. }
  24. }
  25. const styles = StyleSheet.create({
  26. container: {
  27. flex: 1
  28. },
  29. textInputStyle: {
  30. borderColor: '#9a73ef',
  31. borderWidth: 1,
  32. height: 40,
  33. margin: 20,
  34. padding: 10,
  35. },
  36. textOutputStyle: {
  37. fontSize: 20
  38. }
  39. })
  40. export default App;
Output
Text Input In React Native

Multiline TextInput

It is similar to the <textarea /> of HTML. By default, TextInput is not multiline. To make theTextInput as multiline, just assign multiline = {true}. The numberOfLines = {mention number} property help to set the number of lines in multiline TextInput.
Code
  1. import React, { Component } from "react";
  2. import { StyleSheet, View, TextInput, Text } from "react-native";
  3. class App extends Component {
  4. state = {
  5. myComment: ""
  6. }
  7. handleTextChange = (inputText) => {
  8. this.setState({ myComment: inputText })
  9. }
  10. render() {
  11. return (
  12. <View style={styles.container}>
  13. <TextInput
  14. style={styles.textInputStyle}
  15. onChangeText={this.handleTextChange}
  16. placeholder="Enter your comment"
  17. placeholderTextColor="red"
  18. multiline={true}
  19. numberOfLines={5}
  20. />
  21. <Text>Ouptput of your TextInput :-</Text>
  22. <Text style={styles.textOutputStyle}>{this.state.myComment}</Text>
  23. </View>
  24. );
  25. }
  26. }
  27. const styles = StyleSheet.create({
  28. container: {
  29. flex: 1
  30. },
  31. textInputStyle: {
  32. borderColor: '#9a73ef',
  33. borderWidth: 1,
  34. height: 100,
  35. margin: 20,
  36. padding: 10,
  37. },
  38. textOutputStyle: {
  39. fontSize: 20,
  40. fontStyle: "italic",
  41. paddingTop: 10,
  42. color: "blue"
  43. }
  44. })
  45. export default App;
Output
Text Input In React Native
Example
User login functionality using TextInput and Button element,
In this example, we are going to create a small screen of login form like below.
Text Input In React Native
Steps for achieving the above functionality.
Code
  1. import React, { Component } from "react";
  2. import { StyleSheet, View, TextInput, Text, Button } from "react-native";
  3. class App extends Component {
  4. state = {
  5. userName: "",
  6. userPassword: ""
  7. }
  8. uaserNameTextChange = (inputText) => {
  9. this.setState({ userName: inputText })
  10. }
  11. uaserPasswordTextChange = (inputText) => {
  12. this.setState({ userPassword: inputText })
  13. }
  14. userLogin = () => {
  15. alert("User Name :-" + this.state.userName + "\n"
  16. + "Password :-" + this.state.userPassword + "\n")
  17. }
  18. render() {
  19. return (
  20. <View style={styles.container}>
  21. <Text style={styles.txtLogin}>User Login</Text>
  22. <TextInput
  23. style={styles.textInputStyle}
  24. onChangeText={this.uaserNameTextChange}
  25. placeholder="Enter username"
  26. placeholderTextColor="red"
  27. />
  28. <TextInput
  29. style={styles.textInputStyle}
  30. onChangeText={this.uaserPasswordTextChange}
  31. placeholder="Enter password"
  32. placeholderTextColor="red"
  33. secureTextEntry={true}
  34. />
  35. <View style={{ margin: 25 }}>
  36. <Button
  37. title="Login"
  38. color="green"
  39. onPress={this.userLogin}
  40. />
  41. </View>
  42. </View>
  43. );
  44. }
  45. }
  46. const styles = StyleSheet.create({
  47. container: {
  48. flex: 1,
  49. justifyContent: "flex-start",
  50. alignContent: "center",
  51. margin: 10
  52. },
  53. textInputStyle: {
  54. borderColor: '#9a73ef',
  55. borderWidth: 1,
  56. height: 40,
  57. marginLeft: 20,
  58. marginRight: 20,
  59. padding: 10,
  60. marginTop: 8
  61. },
  62. txtLogin: {
  63. padding: 20,
  64. fontWeight: "bold",
  65. fontSize: 20
  66. }
  67. })
  68. export default App;
Output
Once you add code as mentioned above, your output should be like below.
Text Input In React Native
Add username and password in the text field of the user login screen and check the output. Your output should be like the below screen.
Text Input In React Native

Summary

In this article, I have demonstrated the React Native TextInput component, uses, and some important properties. Hopefully, it will help.