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

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

0

You can write the sending part inside a useeffect.

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

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 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