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

234
Visualizações
How to send data only if a variable is true?

I'm saving data in this state but I would like to send "matches" to the server only if a variable is true, how can I do it?

Thanks in advance!

   const [formData, setFormData] = useState({
       user_id: cookies.UserId,
       first_name: "",
       dob_day: "",
       dob_month: "",
       dob_year: "",
       show_gender: false,
       gender_identity: "man",
       gender_interest: "woman",
       url: "",
       about: "",
       matches: [] // send only if variable is true
   })
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can write the sending part inside a useeffect.

useEffect(
        () => {
            yourVariable && sendingFucntion();
        }, 
        [yourVariable]
    )
about 4 years ago · Juan Pablo Isaza Relatório

0

Remove matches from your default state declaration, then, in your condition try something like this :

if(true /** your condition */) {
  setFormData({
   ...formData, /** current data*/
   matches:[] /** add your key matches with correct array value.*/
  })
}

I guess probably something like this ? :

if(matchsArray && Array.isArray(matchsArray) && matchsArray.length > 0) {
  setFormData({
   ...formData, /** current data*/
   matches:matchsArray /** add your key matches with correct array value.*/
  })
}

Don't forget to put your data state in a real FormData to build your POST request after. Like :


const finalFormData = new FormData()

finalFormData.append(
  'data', /** your key catch by your backend api*/ 
  formData /** the react state*/
)

Let me know if it's help you. :)

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