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

179
Visualizações
Ignore keys for null values when sending a post submit via a form

We currently have the following setup which works where we are sending a POST request via a form with a body and also redirects to a new endpoint.

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

I am trying to add more optional parameters to the post body. Since they are optional, only want to send if data exists.

But if I do the following, I still end up sending data with the newkey and empty value.

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>

Instead, I want to totally remove the key newKey from body since no value passed in.

Is there a way around this? Thanks.

about 4 years ago · Juan Pablo Isaza
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