TextInput Basics and Keyboard Types
Render a TextInput, bind it to state with onChangeText, configure the keyboard type for email or numeric input, and handle the Return key event.
The TextInput Component
TextInput is the React Native component for capturing keyboard input from users. It renders as a native text field on both iOS and Android, giving users the platform's native typing experience including autocorrect, predictive text, and system keyboard shortcuts. Import it from react-native and use it for any input that requires a keyboard: search boxes, login forms, settings fields, and chat message composers. Without styling, TextInput has no visible border on iOS — always style it to make it visible to users.
import { TextInput, View, StyleSheet } from 'react-native';
import { useState } from 'react';
export default function BasicInput() {
const [text, setText] = useState('');
return (
<View style={{ padding: 16 }}>
<TextInput
value={text}
onChangeText={setText}
placeholder='Type something here...'
style={styles.input}
/>
</View>
);
}
const styles = StyleSheet.create({
input: {
borderWidth: 1,
borderColor: '#ccc',
borderRadius: 10,
paddingHorizontal: 14,
paddingVertical: 12,
fontSize: 16,
backgroundColor: '#fff',
},
});onChangeText vs onChange
The onChangeText callback is the most common way to react to text input. It receives the new text string directly as its argument, making it easy to pass to a useState setter: onChangeText={setText}. The lower-level onChange callback receives a full event object (event.nativeEvent.text) — useful when you also need other event metadata like cursor position. For most use cases, onChangeText is simpler and preferred. Use onChange only when you need the native event details.
import { TextInput } from 'react-native';
import { useState } from 'react';
function TextInputExample() {
const [text, setText] = useState('');
return (
<>
{/* Simple: receives the string directly */}
<TextInput
onChangeText={(newText) => setText(newText)}
// shorthand equivalent:
// onChangeText={setText}
/>
{/* Advanced: receives the full event object */}
<TextInput
onChange={(event) => {
console.log('text:', event.nativeEvent.text);
console.log('selection:', event.nativeEvent.selection);
}}
/>
</>
);
}All lessons in this course
- TextInput Basics and Keyboard Types
- Tap Handlers with TouchableOpacity and Pressable
- Toggles, Switches, and Checkboxes
- Building a Simple Login Form