Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

97
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda