React Hook Form helps you easily manage form data and validation in React Native apps without extra hassle.
React Hook Form integration in React Native
import { useForm, Controller } from 'react-hook-form'; const { control, handleSubmit, formState: { errors } } = useForm(); <Controller control={control} name="fieldName" rules={{ required: true }} render={({ field: { onChange, onBlur, value } }) => ( <TextInput onBlur={onBlur} onChangeText={onChange} value={value} /> )} /> const onSubmit = data => console.log(data);
useForm sets up form state and helpers.
Controller connects React Native inputs to React Hook Form.
const { control, handleSubmit } = useForm();
<Controller
control={control}
name="email"
rules={{ required: true }}
render={({ field }) => (
<TextInput {...field} placeholder="Email" />
)}
/><Controller
control={control}
name="password"
rules={{ required: true, minLength: 6 }}
render={({ field }) => (
<TextInput {...field} secureTextEntry placeholder="Password" />
)}
/>const onSubmit = data => alert(JSON.stringify(data));
<Button title="Submit" onPress={handleSubmit(onSubmit)} />This React Native app shows a simple form with name and email fields. It uses React Hook Form to manage input and validation. If fields are empty or email is invalid, error messages appear. On submit, it shows an alert with the entered data.
import React from 'react'; import { View, TextInput, Button, Text } from 'react-native'; import { useForm, Controller } from 'react-hook-form'; export default function App() { const { control, handleSubmit, formState: { errors } } = useForm(); const onSubmit = data => { alert(`Name: ${data.name}\nEmail: ${data.email}`); }; return ( <View style={{ padding: 20 }}> <Text>Name:</Text> <Controller control={control} name="name" rules={{ required: true }} render={({ field: { onChange, onBlur, value } }) => ( <TextInput style={{ borderWidth: 1, padding: 8, marginBottom: 5 }} onBlur={onBlur} onChangeText={onChange} value={value} placeholder="Enter your name" accessibilityLabel="Name input" /> )} /> {errors.name && <Text style={{ color: 'red' }}>Name is required.</Text>} <Text>Email:</Text> <Controller control={control} name="email" rules={{ required: true, pattern: /^[^@\s]+@[^@\s]+\.[^@\s]+$/ }} render={({ field: { onChange, onBlur, value } }) => ( <TextInput style={{ borderWidth: 1, padding: 8, marginBottom: 5 }} onBlur={onBlur} onChangeText={onChange} value={value} placeholder="Enter your email" keyboardType="email-address" accessibilityLabel="Email input" /> )} /> {errors.email && <Text style={{ color: 'red' }}>Valid email is required.</Text>} <Button title="Submit" onPress={handleSubmit(onSubmit)} accessibilityLabel="Submit form button" /> </View> ); }
Always add accessibilityLabel to inputs and buttons for screen readers.
Use handleSubmit to run validation before your submit logic.
React Hook Form reduces re-renders compared to controlled inputs alone, improving performance.
React Hook Form helps manage form state and validation easily in React Native.
Use Controller to connect native inputs to the form.
Validation rules show errors and prevent bad data submission.
Practice
React Hook Form in a React Native app?Solution
Step 1: Understand React Hook Form's role
React Hook Form is designed to simplify handling form data and validation in React Native apps.Step 2: Compare options with React Hook Form's purpose
Styling, data fetching, and navigation are handled by other libraries or React Native features, not React Hook Form.Final Answer:
To manage form state and validation easily -> Option DQuick Check:
To manage form state and validation easily = B [OK]
- Confusing React Hook Form with styling libraries
- Thinking it handles navigation or API calls
- Ignoring its validation features
useForm from React Hook Form in a React Native component?Solution
Step 1: Recall correct import syntax for named exports
React Hook Form exportsuseFormas a named export, so curly braces are needed.Step 2: Verify package name correctness
The official package is 'react-hook-form', not 'react-native-hook-form'.Final Answer:
import { useForm } from 'react-hook-form'; -> Option CQuick Check:
Named import from 'react-hook-form' = A [OK]
- Using default import instead of named import
- Wrong package name 'react-native-hook-form'
- Missing curly braces around useForm
const { control, handleSubmit } = useForm();
return (
<Controller
control={control}
name="email"
rules={{ required: true }}
render={({ field: { onChange, value } }) => (
<TextInput
value={value}
onChangeText={onChange}
/>
)}
/>
);What happens if the user submits the form without entering an email?
Solution
Step 1: Understand the validation rule
Therules={{ required: true }}means the email field must have a value.Step 2: Effect of missing required value on submission
If the email is empty, React Hook Form blocks submission and sets an error for that field.Final Answer:
The form submission is blocked and an error is shown -> Option AQuick Check:
Required rule blocks empty submission = C [OK]
- Assuming form submits with empty values
- Expecting automatic default values
- Thinking app crashes on validation error
const { control, handleSubmit } = useForm();
return (
<Controller
control={control}
name="password"
rules={{ minLength: 6 }}
render={({ onChange, value }) => (
<TextInput
value={value}
onChangeText={onChange}
/>
)}
/>
);Solution
Step 1: Check render prop destructuring
The render function receives an object with afieldproperty, which containsonChangeandvalue. Direct destructuring ofonChangeandvalueis incorrect.Step 2: Verify other props
TheminLengthrule accepts a number,controlis present, andnameis correctly placed.Final Answer:
The render prop destructuring is incorrect; should destructure field object -> Option BQuick Check:
Render prop needs field destructuring = D [OK]
- Destructuring onChange and value directly from render argument
- Using string instead of number for minLength
- Omitting control or name props
username and age. The age must be a number greater than 18. Using React Hook Form, which validation rule correctly enforces this for age?Solution
Step 1: Numeric validation from string input
TextInput provides strings;valueAsNumber: truein rules parses to number for validation.Step 2: Rule for >18
min: 19ensures parsed number >= 19 (>18).Step 3: Eliminate others
A'svalidateon string fails ("100" < "18" lexically). B for strings. D opposite.Final Answer:
rules={{ required: true, min: 19, valueAsNumber: true }} -> Option AQuick Check:
valueAsNumber: true + min: 19 = C [OK]
- validate without Number conversion (string "100" > 18 fails)
- minLength for numbers
- max instead of min
Ready to prove it?
Pass the test and this pattern moves unknown
