Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

101
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda