Using React Js To Set Values In A Useform

In this article, we will learn how to use setvalues in useform using react js

using setvalue variable we have to add values of any of the filed in useform. you can see following code.

import React, { useEffect } from 'react'
import { useForm } from "react-hook-form";
export default function Setvaluecomponent() {
    const {
        register,
        handleSubmit,
        setValue,
        formState: { errors },
    } = useForm();
    const onSubmit = () => {
        console.log("submit")
    }
    useEffect(() => {
        setValue('name', "himani vgahsiya")
    }, [])
    
    return (
        <form onSubmit={handleSubmit(onSubmit)}>
            <h2>Edit value</h2>
            <input type="text" name="name" {...register("name")} />
            <button type='submit'>submit</button>
        </form>
    )
}

you can see below the output,

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories