Day 13: Forms in React (Formik and Yup)

Day 13: Forms in React (Formik and Yup)

On the thirteenth day, I learned about forms in React, the Fragment tag, and react-icons in the react application.


React-icons

React Icons is a library that provides popular icon packs as React components. It allows developers to easily integrate icons into their React applications without the need for additional CSS or external libraries.

We can apply react icons in our project in following ways:

Installation: We install the React Icons library using npm.

npm install react-icons        

Importing Icons: After installation, we import individual icons or entire icon packs into React components.

import { FaEnvelope, FaLock, FaUserAstronaut } from "react-icons/fa6"        

Usage: Once imported, you can use the icon components just like any other React component by including them in your JSX code.?

<div className="formInput">
        <Field name="firstName" type="text" placeholder="Enter first name" className=  {`form-control ${errors.firstName && touched.firstName ? "is-invalid":""}`} id="firstName" />
                        <ErrorMessage name="firstName" component={"div"} className="invalid-feedback"/>
 <FaUserAstronaut className="icon"/>                    
</div>        

React Forms

In React, forms work similarly to HTML forms but with some additional features provided by React to manage form state and behavior more effectively.

Managing Forms using ‘Formik’

Formik is a popular form management library for React that simplifies the process of building and validating forms. It provides a set of utilities and components to handle form state, validation, and submission in a more organized and efficient manner.

Form Validation using Yup

Yup is a schema builder for value parsing and validation. It's often used in combination with form libraries like Formik in React applications to define validation rules for form inputs. Yup allows us to create schemas that define the shape and constraints of the data and then validate data against those schemas.

Using the tools and framework, we managed and validated a simple login form as follows.

React Form

Working with Formik and Yup

Managing forms with Formik and validating using Yup is a powerful combination for building robust and flexible forms in React. Formik handles form state management and submission, while Yup handles form validation.

Installation: First, we need to install Formik and Yup.

npm install formik yup        

Setting up the Formik Form: We use the Formik component to wrap our form and provide form management capabilities.

Wrapping the form with Formik

Validation with Yup: Yup is a schema validation library. We define validation rules using Yup schema and pass it to the validationSchema prop of the Formik component. Yup provides various validation methods like string(), number(), object(), etc. In the example above, we use string() for all the fields and specify validation rules such as email() for the email field and required() for both fields.

Validation using Yup

Handling Submission: We specify an onSubmit function in the Formik component, which will be called when the form is submitted. Inside the onSubmit function, you can perform actions like sending form data to a server or reset the application. In the example above, we simply reset the form values.

const onSubmit=(values, {resetForm})=>{
        console.log(values)
        resetForm()
    }        

This is all that we learnt till the thirteenth day of learning react.

Very grateful to our mentor Bikash Karki




要查看或添加评论,请登录

Aastha Sharma的更多文章

  • ExpressJS

    ExpressJS

    What is ExpressJS? Express.js is a Node.

  • Day 12: localStorage

    Day 12: localStorage

    It can be infuriating to close a webpage while filling out a form accidentally. We lose all the data already filled and…

  • Day 11: Layout, Offset, and the useParams Hook

    Day 11: Layout, Offset, and the useParams Hook

    Layout and Offset In React, "offset" and "limit" are commonly used when dealing with paginated data. These terms are…

  • Day 10: Using API data and Pagination in React

    Day 10: Using API data and Pagination in React

    APIs in React APIs (Application Programming Interfaces), in React, are used to fetch data from external sources, such…

  • Day 9: Environment Variables and Toastify in React

    Day 9: Environment Variables and Toastify in React

    What are Environment Variables? Environment variables in React serve as dynamic configurations for our application…

  • Day 8:React Hooks (Part 2)

    Day 8:React Hooks (Part 2)

    After a thorough introduction to React Hooks and the useState hook on day 7, we learned about the useEffect hook in…

  • Day 7: React Hooks

    Day 7: React Hooks

    On the seventh day of learning React, we learned about React hooks, their use, and their applications. React Components…

  • Day 6: Dynamic React Components using Props

    Day 6: Dynamic React Components using Props

    React utilizes a component-based architecture that enables developers to create reusable and modular UI elements. A…

  • Day 5: React Layouts, Outlets, and Links

    Day 5: React Layouts, Outlets, and Links

    On the fifth day of learning react, we grabbed on concepts like layouts, outlets, and links in React. They are…

  • Day 4: React-Router-DOM and BootStrap

    Day 4: React-Router-DOM and BootStrap

    On the fourth day of learning React, we learned about installing Bootstrap and React router DOM, all thanks to our…

社区洞察

其他会员也浏览了