How to do uncontrolled input in react native #13152
Replies: 2 comments
-
|
Hi @mattiaferrari02, You’re not missing anything — this is expected behavior in React Native.
That’s why ✅ Correct way in React Native: use
|
Beta Was this translation helpful? Give feedback.
-
|
In React Native, the React Hook Form's standard The idiomatic way to bind uncontrolled inputs in React Native is by using the Here is the corrected code: import React from 'react';
import { View, TextInput, Button, Text, StyleSheet } from 'react-native';
import { useForm, Controller } from 'react-hook-form';
export default function LoginForm() {
const { control, handleSubmit, formState: { errors } } = useForm({
defaultValues: {
email: ''
}
});
const onSubmit = (data: any) => {
console.log("Submitted data:", data);
};
return (
<View style={styles.container}>
<Controller
control={control}
name="email"
rules={{ required: "Email is required" }}
render={({ field: { onChange, onBlur, value } }) => (
<TextInput
style={styles.input}
onBlur={onBlur}
onChangeText={onChange}
value={value}
placeholder="Email Address"
autoCapitalize="none"
keyboardType="email-address"
/>
)}
/>
{errors.email && (
<Text style={styles.errorText}>{errors.email.message}</Text>
)}
<Button title="Login" onPress={handleSubmit(onSubmit)} />
</View>
);
}
const styles = StyleSheet.create({
container: { padding: 16 },
input: { borderBottomWidth: 1, marginBottom: 8, padding: 8 },
errorText: { color: 'red', marginBottom: 8 }
}); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, i'm trying a simple thing
Why i can never arrive at onSubmit and i keep getting
I'm using
"react": "19.1.0",
"react-hook-form": "^7.66.0",
"react-native": "0.81.5",
Am I missing something?
Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions