Categories
JavaScript Answers React Native Answers

How to get TextInput value with React Native?

Spread the love

Sometimes, we want to get TextInput value with React Native.

In this article, we’ll look at how to get TextInput value with React Native.

How to get TextInput value with React Native?

To get TextInput value with React Native, we can get it from the onChangeText callback.

For instance, we write:

import * as React from 'react';
import { ScrollView, View, TextInput, Text } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';
import { Dimensions } from 'react-native';

export default function App() {
  const [username, setUsername] = React.useState();

  return (
    <View>
      <TextInput
        onChangeText={(username) => setUsername(username)}
        value={username}
      />
      <Text>{username}</Text>
    </View>
  );
}

to set the onChangeText prop of the TextInput to a function that takes the input value as the argument.

In the function, we call setUsername to set the username value to the TextInput‘s value.

Therefore, when we type into the input, we see the same value displayed in the Text component.

Conclusion

To get TextInput value with React Native, we can get it from the onChangeText callback.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *