Lesson 9 of 9
Practice & Quiz - React Native Forms
Test your mastery of controlled TextInput, keyboard avoidance, react-hook-form with Zod, and accessible form patterns with a practical login form exercise and 5 quiz questions.
Practical Exercise - Login Form with Keyboard Avoidance
import React, { useRef } from 'react';
import {
View, Text, TextInput, Pressable, StyleSheet,
KeyboardAvoidingView, ScrollView, Platform,
} from 'react-native';
import { useForm, Controller } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { z } from 'zod';
const LoginSchema = z.object({
email: z.string().email('Invalid email'),
password: z.string().min(8, 'Min 8 characters'),
});
type LoginForm = z.infer<typeof LoginSchema>;
export function LoginScreen({ onLogin }: { onLogin: (data: LoginForm) => Promise<void> }) {
const passwordRef = useRef<TextInput>(null);
const {
control,
handleSubmit,
formState: { errors, isSubmitting },
} = useForm<LoginForm>({ resolver: zodResolver(LoginSchema) });
return (
// KeyboardAvoidingView: prevents keyboard from covering inputs
<KeyboardAvoidingView
style={styles.flex}
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
>
<ScrollView
contentContainerStyle={styles.container}
keyboardShouldPersistTaps="handled" // taps dismiss keyboard & activate buttons
>
<Text style={styles.heading}>Sign In</Text>
{/* Controller bridges react-hook-form with uncontrolled native inputs */}
<Controller
control={control}
name="email"
render={({ field: { onChange, onBlur, value } }) => (
<View style={styles.field}>
<Text style={styles.label}>Email</Text>
<TextInput
style={[styles.input, errors.email && styles.inputError]}
value={value}
onChangeText={onChange}
onBlur={onBlur}
keyboardType="email-address"
autoCapitalize="none"
autoCorrect={false}
returnKeyType="next"
onSubmitEditing={() => passwordRef.current?.focus()}
accessibilityLabel="Email address"
accessibilityHint="Enter your email to sign in"
/>
{errors.email && <Text style={styles.error}>{errors.email.message}</Text>}
</View>
)}
/>
<Controller
control={control}
name="password"
render={({ field: { onChange, onBlur, value } }) => (
<View style={styles.field}>
<Text style={styles.label}>Password</Text>
<TextInput
ref={passwordRef}
style={[styles.input, errors.password && styles.inputError]}
value={value}
onChangeText={onChange}
onBlur={onBlur}
secureTextEntry
returnKeyType="done"
onSubmitEditing={handleSubmit(onLogin)}
accessibilityLabel="Password"
/>
{errors.password && <Text style={styles.error}>{errors.password.message}</Text>}
</View>
)}
/>
<Pressable
style={({ pressed }) => [styles.btn, pressed && styles.btnPressed, isSubmitting && styles.btnDisabled]}
onPress={handleSubmit(onLogin)}
disabled={isSubmitting}
accessibilityRole="button"
accessibilityState={{ busy: isSubmitting }}
>
<Text style={styles.btnText}>{isSubmitting ? 'Signing in...' : 'Sign In'}</Text>
</Pressable>
</ScrollView>
</KeyboardAvoidingView>
);
}
const styles = StyleSheet.create({
flex: { flex: 1 },
container: { flexGrow: 1, justifyContent: 'center', padding: 24 },
heading: { fontSize: 28, fontWeight: '700', marginBottom: 32, color: '#1a1a1a' },
field: { marginBottom: 16 },
label: { fontSize: 14, fontWeight: '500', color: '#374151', marginBottom: 6 },
input: { borderWidth: 1, borderColor: '#d1d5db', borderRadius: 8, padding: 12, fontSize: 16 },
inputError: { borderColor: '#ef4444' },
error: { fontSize: 12, color: '#ef4444', marginTop: 4 },
btn: { backgroundColor: '#229c6b', padding: 16, borderRadius: 12, alignItems: 'center', marginTop: 8 },
btnPressed: { opacity: 0.85 },
btnDisabled: { opacity: 0.5 },
btnText: { color: '#fff', fontWeight: '700', fontSize: 16 },
});Module Quiz
What does `behavior='padding'` vs `behavior='height'` do on KeyboardAvoidingView?
Tip
Tip
Practice Practice Quiz React Native Forms in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Module 3 Knowledge Check: React Hook Form, Zod, and keyboard avoidance
Practice Task
Note
Practice Task - (1) Write a working example of Practice Quiz React Native Forms from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Practice Quiz React Native Forms is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.
Finished this lesson?