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

177
Visualizações
React input states not updating after the form submission

When uploading the form with react the states aren't getting updated like they update for a second then go back to the inputted values I don't know why is this happening

Gif showing the code behaivour

These are the states

const [values, setValues] = useState({
    // Single File upload hooks
    singleFile: {},
    selectSingleFile: false,
    singleTitle: '',
    singleArtist: '',
    open: false,

    // Multiple File upload hooks
    multipleFiles: [],
    selectMultipleFiles: false,
    multipleTitle: '',
    multipleArtist: '',

    uploadStatus: Number,
  });

This is the handleSubmit code

const handleSubmit = (event) => {
    // converting into the form object
    const formData = new FormData();
    formData.append('title', values.singleTitle);
    formData.append('artist', values.singleArtist);
    formData.append('file', values.singleFile);

    // making request to the server
    let reqOptions = {
      url: '/music/singleFile',
      method: 'POST',
      data: formData,
    };
    axios.request(reqOptions).then(function (response) {
      console.log(response);
      setValues({ ...values, uploadStatus: response.status });
    });

    // Setting the hook back to default values
    setValues({
      ...values,
      singleFile: {},
      selectSingleFile: false,
      singleTitle: '',
      singleArtist: '',
      open: true,
    });
    event.preventDefault();
  };

Form Code

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

const handleSubmit = (event) => {
    // converting into the form object
    const formData = new FormData();
    formData.append('title', values.singleTitle);
    formData.append('artist', values.singleArtist);
    formData.append('file', values.singleFile);

    // making request to the server
    let reqOptions = {
      url: '/music/singleFile',
      method: 'POST',
      data: formData,
    };
    axios.request(reqOptions).then(function (response) {
      console.log(response);
      setValues({ ...values, uploadStatus: response.status });
    }).then(res =>{        
    // Setting the hook back to default values
    setValues({
      ...values,
      singleFile: {},
      selectSingleFile: false,
      singleTitle: '',
      singleArtist: '',
      open: true,
    });
     });

    event.preventDefault();
  };

Place your state clear in a .then so then you know for sure it will only clear after the api call was succesful.

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