Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

96
Views
How to reset input field in reactjs?

I'm Try to reset input field when user submit thire item. But I don't figure it out. Here is my code.

const onSubmit = (myData) => {
    const url = "https://automobilereact.herokuapp.com/product";
    const newData = { ...myData, sold: 0 };
    fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(newData),
    })
      .then((res) => res.json())
      .then((result) => console.log(result));

    const { data } = axios.post("https://automobilereact.herokuapp.com/add-item", {
      ...myData,
      email: user.email,
      sold: 0,
    });
  };

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

There's multiple solutions but this is the best way to do it

bind the input to a state:

const [inputContent, setInputContent] = useState("Default value")

code for input:

<input onChange={(e) => setInputContent(e.target.value)} value={inputContent} />

On submit remove the inputContent value

const onSubmit = (myData) => {
  setInputContent("")
  // Rest of code...
  };
about 4 years ago · Juan Pablo Isaza Report

0

const onSubmit = (myData) => {
    const url = "https://automobilereact.herokuapp.com/product";
    const newData = { ...myData, sold: 0 };
    fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(newData),
    })
      .then((res) => res.json())
      .then((result) => console.log(result));

    const { data } = axios.post("https://automobilereact.herokuapp.com/add-item", {
      ...myData,
      email: user.email,
      sold: 0,
    });

 // Try this out....
 let inputs = document.getElementById("id");
 inputs.value = "";
  };

Use this method for every input field. Just Set their value to empty. Like this...

let inputs = document.getElementById("id");
 inputs.value = "";
  };

Remember use all of them at the last of post method. If you use it before posting the value your data will be sent empty.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!