TextInput component is the fundamental way to get text input from users in React Native. This guide covers everything you need to know about handling text input.
Basic TextInput
The simplest form of text input:Always control your TextInput with
value and onChangeText. This makes it a “controlled component” that React can manage.Common TextInput Props
Keyboard Types
Different keyboards for different input types:- Email
- Phone
- Number
- URL
Secure Text Entry
For passwords and sensitive data:Multiline Text
For longer text input:Platform-Specific Behavior
TextInput has different behaviors on iOS and Android, as seen in the React Native source:- iOS
- Android
- Uses
UITextFieldfor single-line - Uses
UITextViewfor multiline returnKeyTypeaffects keyboard appearanceclearButtonModeshows clear button
Complete Login Form Example
Real-world form with validation:Advanced Features
Managing Focus
Auto-advancing Forms
Input Events
TextInput provides several event handlers:Keyboard Configuration
1
Set Return Key Type
2
Handle Submit
3
Enable/Disable Return Key
Common Patterns
Search Input
Best Practices
Always control your inputs
Always control your inputs
Use
value and onChangeText props to keep input state in sync with React state.Set appropriate keyboard types
Set appropriate keyboard types
Use
keyboardType to show the right keyboard for the input type (email, phone, number, etc.).Disable auto-correct for emails and usernames
Disable auto-correct for emails and usernames
Set
autoCorrect={false} and autoCapitalize="none" for email and username inputs.Provide clear visual feedback
Provide clear visual feedback
Style inputs differently for focus, error, and disabled states.
Use secureTextEntry for passwords
Use secureTextEntry for passwords
Never display passwords in plain text.
Next Steps
Using Lists
Learn to render search results and data lists
Platform-Specific Code
Handle iOS and Android differences