Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to reset the form in React after submitting?

Need a way to reset my form after i hit submit..everything works well except for the form dosent reset after the submit .pls check out my code thanks.I checked out for something called reset() online but the answers are way complicated is there any simple method

import { useEffect, useState } from "react";
import { useParams } from "react-router-dom";



function AddReviewForm({user,handleAddReviews}){
    const params = useParams();
   const[img,setImg]=useState("");
   const[r,setR]=useState("")
      const newReview = {
        img,
        r,
        restaurant_id: params.id,
        user_id: user.id,
      };
      const configObj = {
        method: "POST",
        headers: {
          Accept: "application/json",
          "Content-Type": "application/json",
        },
        body: JSON.stringify(newReview),
      };


      // function handleReviewChange(event) {
      //   setreviewData({
      //     ...reviewData,
      //     [event.target.name]: event.target.value,
      //   });
      // }
      function handleReviewSubmit(event) {
        event.preventDefault();
        fetch(`/reviews`, configObj)
          .then((r) => r.json())
          .then((review)=>{
            handleAddReviews(review);
            
          }
          );
      }
    return  (
        <>
        <h1>Add review form</h1>
        <form onSubmit={handleReviewSubmit}>
       
        <label htmlFor="img"  >Image</label>
         <input type="text" name="img"   value={img}  onChange={(e) => setImg(e.target.value)} placeholder="name" />
       
        <label htmlFor="r"  >Review</label>
         <input type="text" name="r"   value={r}  onChange={(e) => setR(e.target.value)} placeholder="review" />
     
       <input type="submit" />

        </form>
        </>
    )
}
export default AddReviewForm;

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You have to set each variable to an empty string:

setR('')
setImg('')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading