Conteúdo do Curso
Foundations of React Native
Foundations of React Native
2. Core Concepts of React Native
Setting Up the React Native ProjectUnderstanding React Native ComponentsWorking with View and Text ComponentsUsing the Image ComponentStyling in React NativeChallenge: Apply Styles to ComponentsWorking with TouchableOpacity for User InteractionImplementing ScrollView for Scrolling ContentRendering Lists in React NativeChallenge: Build an Interactive Resort ListQuiz: React Native Basics
3. Essential React Native Principles
Handling User Input with TextInput
Theory
The TextInput
component is used to allow users to input text. It provides a text input field where users can type, paste, or edit text, making it a fundamental component for collecting user input in forms, search bars, chat applications, and more.
Why Do We Need It?
- User Input Handling:
- Allows users to input text data into your application;
- Essential for gathering information from users, such as usernames, passwords, messages, search queries, and more.
- Versatility:
- Can be customized and configured to meet various input requirements, including keyboard type, placeholder text, secure text entry, and input validation.
How to Work with TextInput
- The
TextInput
can be controlled through its props and state; - We can listen for user input events using callback functions provided by the component, such as
onChangeText
.
Example
Explanation
- We use the
useState
hook to manage two state variables:name
andwelcomeMessage
.name
stores the user's input text, whilewelcomeMessage
stores the message displayed at the top of the screen; - The
handleNameChange
function is called whenever the user types or edits their name in theTextInput
component. It updates thename
state with the new input value; - The
handleNameSubmission
function is called when the user submits their name.- It first checks if the input name is not empty (using
trim()
to remove leading and trailing whitespace); - If the input name is not empty, it updates the
welcomeMessage
state to display a personalized welcome message ("Welcome, [name]!"
); - It then clears the
name
state by setting it to an empty string, effectively resetting theTextInput
component to an empty state for the next input; - If the input name is empty, it updates the
welcomeMessage
state to prompt the user to enter their name ("Please enter your name."
).
- It first checks if the input name is not empty (using
Result
In Practice
Tudo estava claro?
Obrigado pelo seu feedback!
Seção 4. Capítulo 4