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

258
Vistas
How to change the json request format when input is captured via a form

We have the following form.

const MyForm = ({
  myFormRef,
}) => (
  <form id="myForm" ref={myFormRef} method="POST">
    <input name="request_token" type="hidden" />
    <input name="id" type="hidden" />
    <input name="versionId" type="hidden" />
    <input name="additionalId" type="hidden" />
  </form>
);

export default MyForm;

We are using useRef hook to reference this form and calling it within another Component as follows.

const myFormRef = useRef();

<MyForm
    myFormRef={myFormRef}
/>

The postData function at the end is in a js file.
Question is on this postData function.
This is triggered when a form is submitted via a button click.

It is all working fine where I am capturing correct details and able to submit the form.

The issue:

It is the format of how I am sending the data over which is the issue.

I am sending over details in following format in this form.

{
    token,
    id,
    versionId,
    additionalId
}

Instead I want to send it as:

{
    token,
    overallData: {
        id,
        versionId,
        additionalId
    }
}

How can I modify the request format to wrap inside overallData for the 3 fields (token is still outside) before submitting?

export const postData = (
  myFormRef,
  token,
  myLink,
  id,
  versionId,
  additionalId
) => {
  const fields = {
    request_token: token,
    id,
    versionId,
    additionalId,
  };

  // checking if data exists for a key (example id or token) and remove if no value against the key. 
  [...myFormRef.current.children].forEach((child) => {
    child.value = fields[child.name];
    if (!child.value) {
      myFormRef.current.removeChild(child);
    }
  });

  // then submit
  myFormRef.current.action = myLink;
  myFormRef.current.submit();
};

Note: I have to stick with this style to use the form and useRef() due to other dependencies.

Thus looking for a way to possibly modify postData. Else advise other options. Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try this:

<form id="myForm" ref={myFormRef} method="POST">
    <input name="request_token" type="hidden" />
    <input name="overallData.id" type="hidden" />
    <input name="overallData.versionId" type="hidden" />
    <input name="overallData.additionalId" type="hidden" />
  </form>

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