Mastering Form Management in React Native with react-hook-form

Sugand singh
3 min readApr 28, 2024

--

Are you tired of dealing with complex form management in your React Native applications? Look no further! React Native, with its efficient and dynamic nature, provides the perfect platform for building powerful mobile applications. However, managing forms in React Native can often be challenging and time-consuming. Thankfully, with the advent of libraries like react-hook-form, form management has become a breeze.

In this article, we’ll dive into the world of react-hook-form and explore how it can streamline form development in React Native applications.

What is react-hook-form?

React-hook-form is a lightweight and performant form library for React and React Native. It offers a simple and intuitive API for managing form state, validation, and submission. Unlike other form libraries, react-hook-form utilizes React hooks to manage form state, resulting in cleaner and more concise code.

Getting Started

To get started with react-hook-form in your React Native project, you can install it via npm or yarn:

npm install react-hook-form

or

yarn add react-hook-form

Once installed, you can start using react-hook-form by importing the useForm hook:

import { useForm } from 'react-hook-form';

Basic Usage

Let’s create a simple login form using react-hook-form:

import React from 'react';
import { View, TextInput, Button } from 'react-native';
import { useForm, Controller } from 'react-hook-form';

const LoginForm = () => {
const { control, handleSubmit } = useForm();

const onSubmit = (data) => {
console.log(data);
};

return (
<View>
<Controller
control={control}
render={({ field: { onChange, onBlur, value } }) => (
<TextInput
onBlur={onBlur}
onChangeText={onChange}
value={value}
placeholder="Username"
/>
)}
name="username"
rules={{ required: true }}
defaultValue=""
/>
<Controller
control={control}
render={({ field: { onChange, onBlur, value } }) => (
<TextInput
onBlur={onBlur}
onChangeText={onChange}
value={value}
secureTextEntry
placeholder="Password"
/>
)}
name="password"
rules={{ required: true }}
defaultValue=""
/>
<Button title="Login" onPress={handleSubmit(onSubmit)} />
</View>
);
};

export default LoginForm;

In this example, we’re using the useForm hook to initialize our form. We then use theController component provided by react-hook-form to bind our form inputs to the form state. ThehandleSubmit function is used to trigger form submission, which invokes the onSubmit function defined earlier.

Form Validation

React-hook-form makes form validation a breeze with its built-in support for Yup, Joi, and other validation libraries. Let’s add validation to our login form:

import * as yup from 'yup';

const schema = yup.object().shape({
username: yup.string().required('Username is required'),
password: yup.string().required('Password is required'),
});

const LoginForm = () => {
const { control, handleSubmit, formState: { errors } } = useForm({
resolver: yupResolver(schema),
});

const onSubmit = (data) => {
console.log(data);
};

return (
<View>
<Controller
control={control}
render={({ field: { onChange, onBlur, value } }) => (
<TextInput
onBlur={onBlur}
onChangeText={onChange}
value={value}
placeholder="Username"
/>
)}
name="username"
defaultValue=""
/>
{errors.username && <Text>{errors.username.message}</Text>}
<Controller
control={control}
render={({ field: { onChange, onBlur, value } }) => (
<TextInput
onBlur={onBlur}
onChangeText={onChange}
value={value}
secureTextEntry
placeholder="Password"
/>
)}
name="password"
defaultValue=""
/>
{errors.password && <Text>{errors.password.message}</Text>}
<Button title="Login" onPress={handleSubmit(onSubmit)} />
</View>
);
};

In this example, we define a validation schema using Yup and pass it to the useForm hook using the resolver option. We then use theformState object provided by react-hook-form to access validation errors and display them next to the corresponding form fields.

Conclusion

React-hook-form is a powerful tool for managing forms in React Native applications. Its simplicity, performance, and built-in support for validation make it a go-to choice for developers looking to streamline their form development process. Whether you’re building a simple login form or a complex multi-step wizard, react-hook-form has got you covered. Give it a try in your next React Native project and experience the difference!

--

--

Sugand singh

Experienced React Native dev, UI/UX expert, performance optimizer. Keeping up with mobile trends. Let's build mobile magic!