Skip to content Skip to sidebar Skip to footer

React Hook Form: How To Reset The Form Content After Submission

I have a hidden field when you clicked the YES option, I want to reset the form to hide again the field after successful submission. Below is my code and here is the link to sandbo

Solution 1:

The 'showYes' state should be reset to false after submission.

I updated some codes. https://codesandbox.io/s/react-hook-form-using-emailjs-2-forked-fmido?file=/src/App.js

constresetForm = () => {
  reset();
  setShowYes(false);
};
constsendEmail = (formData) => {
  emailjs
    .send("YOUR_SERVICE_ID", "YOUR_TEMPLATE_ID", formData, "YOUR_USER_ID")
    .then((result) => {
      console.log(result.text);
      resetForm();
    },
    (error) => {
      console.log(error.text);
    }
  );
};

I'm not sure if I missed something on passing the data to the form. I also put defaultChecked attribute in option NO.

<input
  className="form-check-input"type="radio"
  value="true"id="yes"
  name="yes_no"
  {...register("yes_no", { required: true })}
  onClick={setShowYes}
/>
<label className="form-check-label mr-4" htmlFor="yes">
  Yes
</label>

Post a Comment for "React Hook Form: How To Reset The Form Content After Submission"