Taming Forms in React TypeScript: A Deep Dive into Formik and Yup

Viraj Lakshitha Bandara - Jul 22 - - Dev Community

topic_content

Taming Forms in React TypeScript: A Deep Dive into Formik and Yup

Forms. The essential building blocks of web applications, yet often a source of headaches for developers. Juggling form state, validation, submission, and error handling can quickly turn into a tangled mess of spaghetti code. Thankfully, the React ecosystem offers powerful libraries like Formik and Yup to help us wrangle those forms into submission.

This blog post explores how to streamline form handling in your React TypeScript projects using these two libraries. We'll delve into their core functionalities, explore common use cases, and even peek into alternative solutions.

Introduction to Formik and Yup

Formik: The Form Wizard

Formik is a popular open-source library specifically designed to simplify form management in React. It eliminates the need for manual state management and event handling by providing a declarative way to define, validate, and submit forms.

Here are some of Formik's key features:

  • Centralized Form State: Say goodbye to scattered component state. Formik keeps all form data in a single, easily accessible object.
  • Simplified Input Handling: Formik automatically handles input changes, updating the form state behind the scenes.
  • Built-in Validation: Formik allows for easy integration with Yup, a powerful schema validation library, to ensure data integrity.
  • Submission Handling: Formik streamlines the process of submitting form data, whether to an API or any other desired endpoint.

Yup: Your Validation Guardian

Yup is a schema builder and object validation library that plays exceptionally well with Formik. It enables developers to define strict data schemas, ensuring that form submissions adhere to predefined rules and formats.

Key features of Yup include:

  • Schema-Based Validation: Define clear, reusable validation rules for different data types, such as strings, numbers, objects, and arrays.
  • Customizable Error Messages: Tailor error messages to provide specific feedback to users based on validation failures.
  • Asynchronous Validation: Perform validations that require external API calls or asynchronous operations.

Streamlining Your Forms: 5 Practical Use Cases

Let's dive into practical examples of how Formik and Yup can simplify common form handling scenarios:

1. Basic Login Form:

This classic example demonstrates Formik's core functionalities:

import React from 'react';
import { Formik, Form, Field, ErrorMessage } from 'formik';
import * as Yup from 'yup';

const LoginSchema = Yup.object().shape({
  email: Yup.string().email('Invalid email').required('Required'),
  password: Yup.string().required('Required'),
});

const LoginForm: React.FC = () => {
  const handleSubmit = (values: any) => {
    // Handle form submission (e.g., API call)
    console.log('Login Successful:', values);
  };

  return (
    <Formik
      initialValues={{ email: '', password: '' }}
      validationSchema={LoginSchema}
      onSubmit={handleSubmit}
    >
      <Form>
        <div>
          <label htmlFor="email">Email:</label>
          <Field type="email" name="email" id="email" />
          <ErrorMessage name="email" component="div" />
        </div>

        <div>
          <label htmlFor="password">Password:</label>
          <Field type="password" name="password" id="password" />
          <ErrorMessage name="password" component="div" />
        </div>

        <button type="submit">Login</button>
      </Form>
    </Formik>
  );
};

export default LoginForm;
Enter fullscreen mode Exit fullscreen mode

In this example, Formik manages the form state (initialValues), handles input changes (Field), and triggers validation and submission (onSubmit). Yup enforces data integrity with the defined LoginSchema.

2. Dynamic Form Fields:

Formik gracefully handles dynamic form structures, such as adding or removing input fields:

import React, { useState } from 'react';
import { Formik, Form, Field, FieldArray, ErrorMessage } from 'formik';
import * as Yup from 'yup';

const ContactInfoSchema = Yup.object().shape({
  phoneNumbers: Yup.array()
    .of(
      Yup.object().shape({
        type: Yup.string().required('Required'),
        number: Yup.string().required('Required'),
      }),
    )
    .min(1, 'Add at least one phone number'),
});

const ContactInfoForm: React.FC = () => {
  const [initialValues] = useState({
    phoneNumbers: [{ type: '', number: '' }],
  });

  const handleSubmit = (values: any) => {
    // Handle form submission
    console.log('Contact Info:', values);
  };

  return (
    <Formik
      initialValues={initialValues}
      validationSchema={ContactInfoSchema}
      onSubmit={handleSubmit}
    >
      {({ values, handleChange, handleBlur, handleSubmit, setFieldValue }) => (
        <Form onSubmit={handleSubmit}>
          <FieldArray name="phoneNumbers">
            {(arrayHelpers) => (
              <div>
                {values.phoneNumbers.map((phoneNumber, index) => (
                  <div key={index}>
                    <Field
                      type="text"
                      name={`phoneNumbers.${index}.type`}
                      placeholder="Type"
                      onChange={handleChange}
                      onBlur={handleBlur}
                    />
                    <ErrorMessage name={`phoneNumbers.${index}.type`} />

                    <Field
                      type="text"
                      name={`phoneNumbers.${index}.number`}
                      placeholder="Number"
                      onChange={handleChange}
                      onBlur={handleBlur}
                    />
                    <ErrorMessage name={`phoneNumbers.${index}.number`} />

                    <button
                      type="button"
                      onClick={() => arrayHelpers.remove(index)} // Remove a phone number
                    >
                      -
                    </button>
                  </div>
                ))}
                <button
                  type="button"
                  onClick={() => arrayHelpers.push({ type: '', number: '' })} // Add a phone number
                >
                  +
                </button>
              </div>
            )}
          </FieldArray>

          <button type="submit">Submit</button>
        </Form>
      )}
    </Formik>
  );
};

export default ContactInfoForm;
Enter fullscreen mode Exit fullscreen mode

Here, FieldArray allows us to manage an array of phone numbers, dynamically adding or removing them while Yup ensures each entry in the array adheres to the defined schema.

3. Asynchronous Validation (e.g., Username Availability):

Yup excels at incorporating asynchronous operations into the validation process:

import React from 'react';
import { Formik, Form, Field, ErrorMessage } from 'formik';
import * as Yup from 'yup';

const SignupSchema = Yup.object().shape({
  username: Yup.string()
    .required('Required')
    .test(
      'usernameAvailable',
      'Username already taken',
      async (value) => {
        const response = await fetch(`/api/users/availability?username=${value}`);
        const data = await response.json();
        return data.available;
      },
    ),
  password: Yup.string().required('Required'),
});

const SignupForm: React.FC = () => {
  const handleSubmit = (values: any) => {
    // Handle form submission
    console.log('Signup Successful:', values);
  };

  return (
    <Formik
      initialValues={{ username: '', password: '' }}
      validationSchema={SignupSchema}
      onSubmit={handleSubmit}
    >
      <Form>
        {/* ... other form fields ... */}

        <button type="submit">Signup</button>
      </Form>
    </Formik>
  );
};

export default SignupForm;
Enter fullscreen mode Exit fullscreen mode

We use Yup's test method to check username availability asynchronously. The validation function fetches data from an API endpoint and returns a boolean indicating availability.

4. Nested Objects and Arrays:

Both Formik and Yup effortlessly manage complex data structures within forms:

import React from 'react';
import { Formik, Form, Field, ErrorMessage } from 'formik';
import * as Yup from 'yup';

const ProfileSchema = Yup.object().shape({
  name: Yup.string().required('Required'),
  address: Yup.object().shape({
    street: Yup.string().required('Required'),
    city: Yup.string().required('Required'),
    // ... other address fields ...
  }),
  hobbies: Yup.array()
    .of(Yup.string())
    .min(1, 'Add at least one hobby'),
});

const ProfileForm: React.FC = () => {
  const handleSubmit = (values: any) => {
    // Handle form submission
    console.log('Profile Updated:', values);
  };

  return (
    <Formik
      initialValues={{
        name: '',
        address: { street: '', city: '' },
        hobbies: [''],
      }}
      validationSchema={ProfileSchema}
      onSubmit={handleSubmit}
    >
      <Form>
        {/* ... form fields for name, address, hobbies ... */}

        <button type="submit">Update Profile</button>
      </Form>
    </Formik>
  );
};

export default ProfileForm;
Enter fullscreen mode Exit fullscreen mode

Yup's schema definition seamlessly validates nested objects (address) and arrays (hobbies) within the form data.

5. Conditional Validation:

Implement dynamic validation rules based on form values:

import React from 'react';
import { Formik, Form, Field, ErrorMessage } from 'formik';
import * as Yup from 'yup';

const PaymentSchema = Yup.object().shape({
  paymentMethod: Yup.string().required('Required'),
  creditCardNumber: Yup.string()
    .when('paymentMethod', {
      is: 'creditCard',
      then: Yup.string()
        .required('Credit card number is required')
        .matches(/^\d{16}$/, 'Invalid credit card number'),
    })
    .nullable(),
  // ... other payment fields ...
});

const PaymentForm: React.FC = () => {
  const handleSubmit = (values: any) => {
    // Handle form submission
    console.log('Payment Successful:', values);
  };

  return (
    <Formik
      initialValues={{ paymentMethod: '', creditCardNumber: '' }}
      validationSchema={PaymentSchema}
      onSubmit={handleSubmit}
    >
      <Form>
        {/* ... other form fields ... */}

        <button type="submit">Pay Now</button>
      </Form>
    </Formik>
  );
};

export default PaymentForm;
Enter fullscreen mode Exit fullscreen mode

The validation for creditCardNumber becomes active only when the selected paymentMethod is "creditCard," showcasing Yup's flexibility.

Exploring Alternatives: A Glimpse Beyond

While Formik and Yup are formidable contenders in the form management arena, it's worth noting other popular options within the React ecosystem:

  • React Hook Form: A performance-focused library leveraging React Hooks for efficient form state management and validation. It often boasts better performance than Formik, especially in large-scale applications. https://react-hook-form.com/
  • Formik + Zod: Similar to Yup, Zod is a schema validation library gaining traction for its developer-friendly API and robust type inference capabilities. https://zod.dev/

Conclusion: Mastering Forms with Confidence

Formik and Yup, along with their counterparts, provide developers with the tools they need to build robust, user-friendly forms in React TypeScript applications. By abstracting away much of the complexity associated with form handling, these libraries empower us to focus on crafting seamless user experiences rather than wrestling with boilerplate code.

Whether you're building a simple contact form or a complex multi-step wizard, incorporating these tools into your workflow can significantly boost your productivity and ensure the creation of highly maintainable and scalable form-driven applications.

Architecting a Robust Form Workflow: An Advanced Use Case

Let's imagine you're building a sophisticated multi-step onboarding form for a financial platform. This form needs to:

  • Handle complex validation: Including conditional rules, cross-field dependencies, and asynchronous checks (e.g., verifying bank account details).
  • Manage a large data set: Potentially spanning multiple sections, with dynamic fields based on user choices.
  • Integrate with various services: This might involve storing data in a database (e.g., AWS DynamoDB), triggering email notifications (e.g., using AWS SES), and even performing KYC (Know Your Customer) checks using specialized APIs.

Solution:

We can leverage a powerful combination of AWS services and libraries to build a resilient, scalable, and secure solution:

Front-end (React TypeScript with Formik and Yup):

  1. Component Structure: Break down the form into manageable components, each responsible for a specific section (e.g., personal information, financial details, KYC).

  2. State Management: Utilize Formik to handle the overall form state, potentially employing useFormikContext to share context between components.

  3. Schema Validation: Define comprehensive Yup schemas for each section, incorporating:

    • Conditional Validation: Dynamically adjust validation rules based on user input.
    • Cross-Field Validation: Ensure consistency between related fields (e.g., password confirmation).
    • Asynchronous Validation: Validate data against external sources, like checking for duplicate emails or verifying addresses.
  4. Submission Handling:

    • Data Transformation: Transform form data into a suitable format for your backend API.
    • API Interaction: Use axios or fetch to securely communicate with your backend API.
    • Error Handling: Provide clear error messages to the user and implement retry mechanisms for network failures.

Backend (AWS Lambda & Serverless Framework):

  1. API Gateway: Define RESTful endpoints for form submission and data retrieval.

  2. Lambda Functions: Implement serverless functions to handle each form submission step:

    • Data Validation: Validate the received data against pre-defined schemas (potentially using a library like Joi or reusing Yup schemas on the backend).
    • Business Logic: Perform any necessary business logic, like calculating risk scores or generating reports.
    • Data Storage: Persist data securely in a database like DynamoDB.
    • Third-party Integrations: Communicate with external APIs for KYC checks, email notifications, or other integrations.

Additional AWS Services:

  • AWS Cognito: Securely manage user authentication and authorization.
  • AWS SES: Send email notifications upon successful form completion or to handle verification steps.
  • AWS Step Functions: Orchestrate complex workflows involving multiple Lambda functions, potentially used for asynchronous validation or multi-step processes.

Benefits:

  • Scalability and Reliability: AWS Lambda automatically scales to handle fluctuating traffic, ensuring high availability.
  • Cost-Effectiveness: Pay only for the compute time used, making it a cost-effective solution for handling form submissions.
  • Security: AWS provides robust security features to protect sensitive data, including data encryption at rest and in transit.

By combining the power of Formik, Yup, React TypeScript, and AWS services, you can architect a robust and scalable solution for even the most demanding form-driven workflows. This approach ensures a seamless user experience while maintaining data integrity and security.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player