In React Native, user input is commonly handled using the TextInput component. To read and update values entered by the user, state management is used along with the onChangeText event.
React Native follows the concept of controlled components. The input value is stored in state, and the same state is passed back to the input. This ensures full control over user data.
// Import required hooks and components
import React, { useState } from "react";
import { View, Text, TextInput } from "react-native";
const App = () => {
const [name, setName] = useState("");
return (
<View>
<TextInput
placeholder="Enter your name"
value={name}
onChangeText={setName}
/>
<Text>Hello {name}</Text>
</View>
);
};
export default App;
Interact with the simulation below to understand how the Input field communicates with the State variable.
// Internal Memory (State)
const [name, setName] = useState("");
As the user types into the input field, the text is stored in state. The greeting text updates instantly, reflecting the entered value.
Think of TextInput as a pipe:
onChangeText captures the value