Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react hook form reset

// destructure reset from useForm as like below
const { register, handleSubmit, errors, reset } = useForm();
// then call the reset function when all of the data has been sent to the server
reset();

//example:
  const { register, handleSubmit, errors, reset } = useForm();
  const onSubmit = (data) => {
    const url = 'http://localhost:5000/item';
    axios
      .post(url, {
        ...data,
      })
      .then((res) => {
        if (res.data.acknowledged) {
          toast('Item Added Successfully...');
          reset(); // here I am reseting the form
        }
      });
  };
Comment

react hook form reset only one field

resetField('input-name'); // register input and resetField works
Comment

react hook form clear form

// ❌ avoid the following with deep nested default values
const defaultValues = { object: { deepNest: { file: new File() } } };
useForm({ defaultValues });
reset(defaultValues); // share the same reference

// ✅ it's safer with the following, as we only doing shallow clone with defaultValues
useForm({ deepNest: { file: new File() } });
reset({ deepNest: { file: new File() } });
Comment

PREVIOUS NEXT
Code Example
Javascript :: vuex state from another module 
Javascript :: Easy Way to Check if 2 Arrays are Equal in JavaScript 
Javascript :: Navigator.pushReplacementNamed parameters 
Javascript :: javascript getting input from console 
Javascript :: javascript bigint 
Javascript :: set cookie and get cookie in javascript 
Javascript :: how to implement redis pub sub model using nodejs 
Javascript :: add regexp to antd 
Javascript :: javascript storage get set item 
Javascript :: express node 
Javascript :: change port react app 
Javascript :: percentage formula in javascript 
Javascript :: Random Integer 1-10 
Javascript :: javascript fs read 
Javascript :: change datetime format in js 
Javascript :: object json parse nestjs 
Javascript :: how to get checked value of checkbox in jquery 
Javascript :: how to clear state in react hooks 
Javascript :: converting strings to numbers 
Javascript :: javascript randint 
Javascript :: hmac_sha256 node 
Javascript :: javascript get element using id and class name 
Javascript :: math .round 
Javascript :: jquery nice select 
Javascript :: nodejs dotenv path how to set 
Javascript :: print all days names of a month js javascript 
Javascript :: how to get folder names with fs 
Javascript :: nodejs bodyparser form data 
Javascript :: chartjs each dataset get colors 
Javascript :: node.js util module 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =