Explain it simply
Handling User Input, in plain language
TextInput is the component for entering text (similar to HTML's <input>) — pairing the value prop with the onChangeText callback via useState makes it a controlled input (state updates on every keystroke). Button is a basic built-in component, but its styling is limited — for custom designs, it's common to build with TouchableOpacity/Pressable combined with View+Text.
Problem → Why → Solution
The problem — why this concept matters
Assuming onChangeText is onChange (web-style) and using the wrong prop name — in React Native it must be onChangeText
A common mistake
Leaving TextInput uncontrolled without pairing value/onChangeText — it won't stay in sync with state
Core concept — understand how it works
For Handling User Input, separate ownership of navigation, form, server, and device state. Test rapid taps, back gestures, deep links, process restarts, keyboard use, and screen-reader flows.
- JavaScript state
- native component and layout
- Android or iOS UI
Explain the React and native-platform behavior of Handling User Input
Core syntax or pattern
import { useState } from 'react';
import { View, TextInput, TouchableOpacity, Text, StyleSheet } from 'react-native';
export default function LoginForm() {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
return (
<View style={styles.form}>
<TextInput
style={styles.input}
placeholder="Username"
value={username}
onChangeText={setUsername}
/>
<TextInput
style={styles.input}
placeholder="Password"
secureTextEntry
value={password}
onChangeText={setPassword}
/>
<TouchableOpacity style={styles.button} onPress={() => console.log(username)}>
<Text style={styles.buttonText}>Login</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
form: { padding: 16 },
input: { borderWidth: 1, borderColor: '#ccc', borderRadius: 8, padding: 12, marginBottom: 12 },
button: { backgroundColor: '#3b82f6', padding: 14, borderRadius: 8, alignItems: 'center' },
buttonText: { color: 'white', fontWeight: '600' },
});For Handling User Input, separate ownership of navigation, form, server, and device state. Test rapid taps, back gestures, deep links, process restarts, keyboard use, and screen-reader flows.
Read the code step by step
- JavaScript state
import { useState } from 'react';TextInput is the component for entering text (similar to HTML's <input>) — pairing the value prop with the onChangeText callback via useState makes it a controlled input (state updates on every keystroke). Button is a basic built-in component, but its styling is limited — for custom designs, it's common to build with TouchableOpacity/Pressable combined with View+Text.
- native component and layout
secureTextEntryFor Handling User Input, separate ownership of navigation, form, server, and device state. Test rapid taps, back gestures, deep links, process restarts, keyboard use, and screen-reader flows.
- Android or iOS UI
});Test an original Handling User Input example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
- JavaScript state
- native component and layout
- Android or iOS UI
Success
Explain the React and native-platform behavior of Handling User Input
Failure
Assuming onChangeText is onChange (web-style) and using the wrong prop name — in React Native it must be onChangeText
Let's think about this for a moment
TextInput is the component for entering text (similar to HTML's <input>) — pairing the value prop with the onChangeText callback via useState makes it a controlled input (state updates on every keystroke). Button is a basic built-in component, but its styling is limited — for custom designs, it's common to build with TouchableOpacity/Pressable combined with View+Text.
Let's connect this to a real scenario
To build a login form, you need two TextInputs for username/password and one submit button — adding secureTextEntry={true} to the password field masks it as dots (••••). Using TouchableOpacity instead of Button lets you set a custom color/border/padding — Button only uses the platform's default style, which makes it hard to customize.
Code Example
import { useState } from 'react';
import { View, TextInput, TouchableOpacity, Text, StyleSheet } from 'react-native';
export default function LoginForm() {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
return (
<View style={styles.form}>
<TextInput
style={styles.input}
placeholder="Username"
value={username}
onChangeText={setUsername}
/>
<TextInput
style={styles.input}
placeholder="Password"
secureTextEntry
value={password}
onChangeText={setPassword}
/>
<TouchableOpacity style={styles.button} onPress={() => console.log(username)}>
<Text style={styles.buttonText}>Login</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
form: { padding: 16 },
input: { borderWidth: 1, borderColor: '#ccc', borderRadius: 8, padding: 12, marginBottom: 12 },
button: { backgroundColor: '#3b82f6', padding: 14, borderRadius: 8, alignItems: 'center' },
buttonText: { color: 'white', fontWeight: '600' },
});On the login form, you can type a username/password, and pressing the Login button prints the username to the console.5-Minute Try-It
Build a login form yourself — include 2 TextInputs (username, password) and one TouchableOpacity button.
A Quick Heads-Up
Don't assume that adding secureTextEntry to a password TextInput makes the password 'secure' — it's only visual masking; actual transmission/storage security needs to be handled separately on the backend.
Real-world usage
How to apply it in a real project
Test an original Handling User Input example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
Mini challenge
Build it yourself
Test an original Handling User Input example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
import { useState } from 'react';
import { View, TextInput, TouchableOpacity, Text, StyleSheet } from 'react-native';
export default function LoginForm() {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
return (
<View style={styles.form}>
<TextInput
style={styles.input}
placeholder="Username"
value={username}
onChangeText={setUsername}
/>
<TextInput
style={styles.input}
placeholder="Password"
secureTextEntry
value={password}
onChangeText={setPassword}
/>
<TouchableOpacity style={styles.button} onPress={() => console.log(username)}>
<Text style={styles.buttonText}>Login</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
form: { padding: 16 },
input: { borderWidth: 1, borderColor: '#ccc', borderRadius: 8, padding: 12, marginBottom: 12 },
button: { backgroundColor: '#3b82f6', padding: 14, borderRadius: 8, alignItems: 'center' },
buttonText: { color: 'white', fontWeight: '600' },
});Expected: Explain the React and native-platform behavior of Handling User Input
Show hint
Test an original Handling User Input example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- JavaScript state → native component and layout → Android or iOS UI
- For Handling User Input, separate ownership of navigation, form, server, and device state. Test rapid taps, back gestures, deep links, process restarts, keyboard use, and screen-reader flows.
- Assuming onChangeText is onChange (web-style) and using the wrong prop name — in React Native it must be onChangeText
- Test an original Handling User Input example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
React Native Documentation — React Native