Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted almost 2 years ago

Multi-Step Form using React

react, webpack
Vaibhav Shete•150
@vaibhavbshete
A solution to the Multi-step form challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


I haven't tried to match the design pixel-by-pixel, but instead have tried to understand the look and feel and replicate it.

Pixel-perfect-wise it might be wrong, but what do you think about the feel?

Another question I have is about form validation. How to you go about it in React? Do you too keep all info in a state variable and check it? Is there some frequently used library that could make common validations easy?

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • Khaya•300
    @khaya05
    Posted almost 2 years ago

    Hey Vaibhav

    Great job on this project. One common way of validating forms in react is to use a library called "react-hook-form" and "yup" if you use javascript, or "ZOD" if you use typescript.

    Here is an example of using Yup and react-hook-form:

    Step 1: Install Yup and React Hook Form

    npm install yup react-hook-form
    

    Step 2: Import the required dependencies

    import React from 'react';
    import { useForm } from 'react-hook-form';
    import { yupResolver } from '@hookform/resolvers/yup';
    import * as yup from 'yup';
    

    Step 3: Define your form schema using Yup For example, if you have a form with a name field and an email field, you can define the schema like this:

    const schema = yup.object().shape({
    name: yup.string().required('Name is required').min(2, 'Name should be at least 2 characters'),
    email: yup.string().required('Email is required').email('Invalid email address'),
    });
    

    Step 4: Use React Hook Form with Yup resolver Inside your React component, initialize the React Hook Form and specify the Yup resolver:

    const MyForm = () => {
    const { register, handleSubmit, formState: { errors } } = useForm({
    resolver: yupResolver(schema),
    });
    
    const onSubmit = (data) => {
    // Handle form submission
    console.log(data);
    };
    
    return (
    <form onSubmit={handleSubmit(onSubmit)}>
    <div>
    <label>Name</label>
    <input type="text" {...register('name')} />
    {errors.name && <p>{errors.name.message}</p>}
    </div>
    <div>
    <label>Email</label>
    <input type="text" {...register('email')} />
    {errors.email && <p>{errors.email.message}</p>}
    </div>
    <button type="submit">Submit</button>
    </form>
    );
    };
    

    In the code above, the register function from React Hook Form is used to register the form fields. The handleSubmit function is used to handle form submission. The errors object contains the validation errors from Yup, and it's used to display error messages for the corresponding fields.

    Step 5: Render the form Finally, render the <MyForm /> component wherever you want the form to appear in your application.

    Also, check out the documentation to learn more:

    Yup:

    • Official Website
    • Documentation

    React Hook Form:

    • Official Website
    • Documentation
    • Validation with Yup

    Hope this helps!👍

Join our Discord community

Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!

Join our Discord
Frontend Mentor logo

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

How does the accessibility report work?

When a solution is submitted, we use axe-core to run an automated audit of your code.

This picks out common accessibility issues like not using semantic HTML and not having proper heading hierarchies, among others.

This automated audit is fairly surface level, so we encourage to you review the project and code in more detail with accessibility best practices in mind.

How does the CSS report work?

When a solution is submitted, we use stylelint to run an automated check on the CSS code.

We've added some of our own linting rules based on recommended best practices. These rules are prefixed with frontend-mentor/ which you'll see at the top of each issue in the report.

The report will audit all CSS, SCSS and Less files in your repository.

How does the HTML validation report work?

When a solution is submitted, we use html-validate to run an automated check on the HTML code.

The report picks out common HTML issues such as not using headings within section elements and incorrect nesting of elements, among others.

Note that the report can pick up “invalid” attributes, which some frameworks automatically add to the HTML. These attributes are crucial for how the frameworks function, although they’re technically not valid HTML. As such, some projects can show up with many HTML validation errors, which are benign and are a necessary part of the framework.

How does the JavaScript validation report work?

When a solution is submitted, we use eslint to run an automated check on the JavaScript code.

The report picks out common JavaScript issues such as not using semicolons and using var instead of let or const, among others.

The report will audit all JS and JSX files in your repository. We currently do not support Typescript or other frontend frameworks.

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub