Git Product home page Git Product logo

nextjs-react-hook-form-yup-05-2022's Introduction

✨ Setup React Hook Form Dan Yup Ke Projek React Kita ✨

Dokumentasi Plugin

plugin website
React Hook Form https://react-hook-form.com/
YUP https://github.com/jquense/yup

Instalasi

Instal React Hook Form ke dalam projek kita

npm install react-hook-form

Kemudian instal YUP

npm install @hookform/resolvers yup

Import Plugin Ke Projek Kita

Import plugin React Hook Form yang akan mengelola hook dari form input kita

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

Import plugin YUP yang akan mempermudak kita dalam membuat validasi yang lebih rapi dan tersetruktur

import { yupResolver } from '@hookform/resolvers/yup';
import * as yup from "yup";

Membuat Schema Dengan YUP

const schemaValidationWithYup = yup.object({
    fullName : yup.string("file wajib string").required("filed wajib di isi"),
    <!--letakkan semua validasi disini-->
})

Implementasi React Hook Form

menggabungkan antara React Hook Form dan YUP sebagai schema validatornya

  • register akan di gunakan untuk menghubungkan antara input form dengan validasi schema dari yup
  • handleSubmit adalah sebuah function yang akan di jalankan ketika kita menekan tombol submit
  • formState : { errors } sesuai dengan namanya kita akan gunakan untuk menampilkan error jika sebuah field input tidak di isi sesuai dengan validasi yang telah kita tentukan
  • useForm ini kita gunakan untuk memberi tahu kepada React Hook Form kalau kita untuk validasi menggunakan YUP, kenapa seperti itu ? sebenarnya React Hook Form sendiri juga sudah memiliki fitur untuk validasi namun menurut saya itu kurang rapih
const { register, handleSubmit, formState:{ errors } } = useForm({
    resolver: yupResolver(schemaValidationWithYup)
  });

Implementasi Ke Form Input Kita

const handleSubmitData = (data) => {
    <!--letakkan aksi selanjutnya disini-->
}

<form onSubmit={handleSubmit(handleSubmitData)}>
    <input type="text" {...register("fullName")}/>
    <p>{errors?.fullName?.message}</p>

    <button type="submit">Simpan</button>
</form>

nextjs-react-hook-form-yup-05-2022's People

Stargazers

Aris Munandar avatar

Watchers

Aris Munandar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.