How to create basic form with Formik in React Native

Key takeaways:

  • Forms are crucial for enabling user interactions such as account sign-ups, search queries, and feedback submissions. The design and functionality of these forms directly influence the user experience making them a fundamental component in React Native development.

  • As React Native applications scale, managing the state, validation, and user feedback of increasingly complex forms can become cumbersome. Formik simplifies the process of handling form state, input management, and validation, especially in complex forms.

  • By providing a structured approach to form handling, Formik reduces the complexity associated with managing multiple form fields and their interactions, making it easier to build and maintain robust forms in React Native.

  • Formik’s state management capabilities help avoid unnecessary re-renders, thereby optimizing the performance of React Native applications. By efficiently tracking input changes across multiple fields, Formik ensures that the application remains responsive and performs well, even as the complexity of forms increases.

In the world of React Native application development, building forms is essential for creating a smooth user experience. Forms in React Native serve as the primary means through which users interact with our application, providing a bridge for data input and user engagement. Whether it’s signing up for an account, submitting a search query, or providing feedback, forms are the conduits that facilitate these actions. The design and functionality of forms have a direct impact on the user experience and the overall success of our application.

Why use Formik for React Native forms?

React Native offers a variety of components and tools to simplify the form-building process. However, as our application grows and the complexity of our forms increases, managing state, validation, and user feedback can become challenging.

Creating forms with Formik in React Native makes managing state, input handling, and validation much easier, especially for complex forms. As mobile apps grow, maintaining form state and ensuring smooth user feedback can become challenging. This is where Formik simplifies form creation and state management.

Note: Want to build real world applications with React Native? Try this project out: Build an E-Commerce App with React Native, Redux, and Firebase.

Key features of Formik in React Native

Here are the basic components and utilities we’ll commonly use when working with Formik:

  • <Formik>: This is the top-level component that wraps the entire form. It handles form state, form submission, and various form-level actions. We pass the initial values of various form fields in initialValues and an onSubmit handler function, which utilizes these values once the form is submitted.

<Formik
initialValues={{ username: ''/* form field values */ }}
onSubmit={(values, { resetForm }) => {
// Handle form submission here
}}
>
{/* Your form components here */}
</Formik>
  • <Form>: This component is used to wrap the form fields. All form elements, like <Field> components, should be placed within this component. When submitting the form, it automatically triggers the onSubmit handler to collect the user input.

Note: Explore key concepts of React Native with the help of this project: Build a Music Player Application Using React Native.

  • <Field>: This component is used for rendering form fields. It takes care of field-level state management, validation, and user input handling. We can specify the name prop to link it to a specific field in the initialValues. This reduces boilerplate code when working with complex forms in React Native.

<Formik>
<Form>
<Field type="text" name="username"/>
</Form>
</Formik>
  • <ErrorMessage>: This component is used for displaying validation errors associated with a specific field. It shows an error message when a field’s validation fails.

<Formik>
<Form>
<Field type="text" name="username"/>
<ErrorMessage name="username" component="div" />
</Form>
</Formik>
  • onSubmit: This function is specified within the <Formik> component as a callback that gets triggered when the form is submitted. This function takes two parameters:

    • values: This parameter contains an object that holds the current values of all form fields. Each field value is associated with the name attribute defined in the <Field> component.

    • formBag: This parameter is an object that contains various helper methods and properties for managing the form. It includes functions like setFieldValue, setFieldTouched, and resetForm, as well as properties like dirty, isValid, and isSubmitting.

  • resetForm: This function allows us to clear the form after submission. This ensures a seamless user experience.

<Formik
initialValues={{ username: ''}}
onSubmit={(values, { resetForm }) => {
// Handle form submission here
console.log(values);
resetForm(); // Reset the form after submission
}}
>
</Formik>

Code example

Let’s look at the demo React Native application, which demonstrates how to use Formik to create a basic form.

Note: Click the “Run” button to execute the code. Once the server starts, you can click the URL next to “Your app can be found at:” to view the application in the new browser tab.

{
  "dependencies": {
    "@expo/webpack-config": "^18.0.1",
    "expo": "^48.0.7",
    "formik": "^2.4.5",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-native": "0.71.8",
    "react-native-web": "~0.18.7"
  },
  "devDependencies": {
    "@babel/core": "^7.19.3"
  },
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "nohup expo start --web &"
  },
  "version": "1.0.0",
  "private": true,
  "name": "myreactnative"
}
Creating a form using Formik in React Native

Code explanation

  • Line 11: First, we initialize a Formik form using the <Formik> component.

  • Line 12: We set the initialValues prop to define the initial values for the form fields: fullname, email, and password.

  • Line 13: The onSubmit function logs the values of the form fields to the console using console.log(values). Then, the resetForm() function is called to clear the form.

  • Lines 25–51:

    • The form fields are created using the <Field> component. For each field, attributes like type, name, placeholder, and inline styles are defined. The name attribute links the field to the corresponding key in the initialValues object, making it easier to manage form data.

    • To display validation errors for each field, the <ErrorMessage> component is utilized. The name prop of the <ErrorMessage> component corresponds to the name prop of the <Field>, associating it with a specific form field. If validation fails, the <ErrorMessage> component displays a relevant error message.

  • Line 46: A “submit” button is included within the form using a <button> element.

  • Line 50: A green-colored success message is displayed to inform the user that their form has been submitted successfully.

  • Lines 57–86: This code includes basic styling for the form and its elements. The customized styling is applied using the styles object created with StyleSheet.create().

Note: Want to build an AI powered game in React Native, try out this project: Create a Connect 4 Game Using React Native.

Benefits of Formik for mobile app development

  1. By using Formik’s form state management, developers can avoid unnecessary complexity when tracking input changes across multiple fields.

  2. Using Formik avoids frequent re-renders and minimizes performance issues in React Native applications compared to traditional state management methods.

  3. Developers can build multi-step forms with Formik or dynamically add and remove fields using FieldArray.

Challenges of using Formik in React Native

  1. New developers may find the initial setup and use of Formik’s APIs, such as <Field>, <Formik>, and <ErrorMessage>, a bit complex compared to traditional form handling.

  2. Although Formik optimizes re-renders, managing state for extremely large forms with a lot of fields can sometimes result in noticeable performance overhead compared to alternatives like React Hook Form.

Building reliable forms is a critical part of mobile app development. With Formik’s advanced form handling, managing state and handling form submissions becomes seamless. Using Formik for React Native will improve user experience, reduce bugs, and enhance the performance of your mobile app. Try it in your next project to see how dynamic forms in React Native can simplify your development process.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


Can I use Formik for dynamic forms in React Native?

Yes, Formik supports dynamic forms through FieldArray. This allows developers to add or remove form fields as needed, making it ideal for forms with dynamic field requirements.


Is Formik suitable for large forms with many fields?

Yes, Formik optimizes performance by limiting unnecessary re-renders. It’s a great choice for complex forms in mobile applications with many fields.


How does Formik handle validation errors?

Formik integrates with Yup to manage input validation and display errors through <ErrorMessage>. Developers can also implement custom validation logic if needed.


Can Formik be integrated with React Native UI libraries?

Formik works seamlessly with UI libraries like Material-UI, Ant Design, and others, enabling developers to create professional-looking forms.


How does Formik compare to React Hook Form?

Formik uses controlled components, making it easier to manage state and integrate with libraries like Yup for validation. React Hook Form, on the other hand, is more lightweight and optimized for performance with its use of uncontrolled components, making it ideal for large or complex forms. Choose Formik for simplicity and structure, and React Hook Form for better performance and flexibility.


Free Resources

HowDev By Educative. Copyright ©2025 Educative, Inc. All rights reserved