About Lesson
1. What is TextInput?
TextInput
is a basic component for creating input fields where users can type text.- It supports single-line and multi-line input, as well as advanced features like auto-correction, keyboard customization, and placeholder text.
2. Key Props of TextInput
value
: Holds the current value of the input field. Typically used in controlled components.placeholder
: Displays temporary text inside the input field until the user starts typing.onChangeText
: A callback function triggered whenever the text inside the input changes.keyboardType
: Specifies the type of keyboard displayed (e.g., numeric, email).secureTextEntry
: Hides input text for sensitive fields like passwords.multiline
: Allows the input field to accept multiple lines of text.
3. Event Handling
onFocus
andonBlur
: Triggered when the input field gains or loses focus.onSubmitEditing
: Fires when the user submits the input (e.g., by pressing the “Return” key).onEndEditing
: Executes when the user finishes editing the text field.
4. Styling TextInput
TextInput
can be styled like any other component using thestyle
prop. Common customizations include:- Border styles for creating visible input boxes.
- Padding and margins for spacing.
- Font styles to control text appearance.
5. Controlled vs. Uncontrolled Components
- Controlled: The
value
of the input is managed by the component’s state, allowing developers to have full control over the input behavior. - Uncontrolled: The component manages its own internal state without relying on external management.
6. Best Practices
- Always sanitize and validate user input, especially for forms.
- Use appropriate
keyboardType
values to improve user experience (e.g., numeric keypad for phone numbers). - Provide clear
placeholder
text to indicate the expected input format. - Use
maxLength
to limit the number of characters if needed.
7. Use Cases
- Forms for user registration, login, or feedback.
- Search bars for querying data.
- Custom input fields for settings or preferences.
The TextInput
component is an essential tool for building interactive and user-friendly forms in React Native applications.