Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Ignorar claves para valores nulos al enviar una publicación a través de un formulario

Actualmente tenemos la siguiente configuración que funciona donde enviamos una solicitud POST a través de un formulario con un cuerpo y también redirige a un nuevo punto final.

 // this is from another file. Just adding it here to show that myRef is a useRef hook. const myRef = React.useRef(); export const postData = (myRef, token, link) => { myRef.current.children[0].value = token; myRef.current.action = link; myRef.current.submit(); }; const MyForm = ({ myRef, }) => ( <form id="MyForm" ref={myRef} method="POST"> <!-- custom_token ends up as the key for the token value being set above as the post body --> <input name="custom_token" type="hidden" /> </form> ); export default MyForm;

Estoy tratando de agregar más parámetros opcionales al cuerpo de la publicación. Dado que son opcionales, solo desea enviar si existen datos.

Pero si hago lo siguiente, todavía termino enviando datos con la newkey y el valor vacío.

 export const postData = (myRef, token, link, newValue) => { myRef.current.children[0].value = token; // NEW addition. Even though I am only setting value if it exists, // the post body still ends up with newKey and empty value. // I don't want to have the newKey and empty value at all since the value is not present. if (newValue) { myRef.current.children[1].value = newValue; } myRef.current.action = link; myRef.current.submit(); }; <form id="MyForm" ref={myRef} method="POST"> <input name="custom_token" type="hidden" /> <!-- NEW addition. --> <input name="newKey" type="hidden" /> </form>

En cambio, quiero eliminar por completo la clave newKey del cuerpo ya que no se pasó ningún valor.

¿Hay alguna forma de evitar esto? Gracias.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!