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

217
Visualizações
Unable to remove child from useRef hook

I have the following useRef which contains 4 children.
I am trying to remove a child from here based on an if check.

But getting the error:

typeError: myRef.current.removeChild is not a function.

But looking at the answers here this seems to be correct. What am I doing wrong?

How to remove the child element of a div in react using useRef hook?

//This is coming from another file. Just for reference. 
const myRef = React.useRef();

export const postData = (
  myRef,
  token,
  id1,
  id2,
) => {
  myRef.current.children[0].value = token;
  myRef.current.children[1].value = id1;
  myRef.current.children[2].value = id2;

  [...myRef.current.children].forEach((child) => {
    if (!child.value) {

      // error on this line 
      myRef.current.removeChild(child);
      
    }
  });
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

you can use useEffect hook in here, maybe this will solve your issue.

...
const myRef = useRef(null)
useEffect(() => {
  myRef.current.children[0].value = token;
  myRef.current.children[1].value = id1;
  myRef.current.children[2].value = id2;

  [...myRef.current.children].forEach((child) => {
    if (!child.value) {
      myRef.current.removeChild(child);

    }
  });
}, [myRef.current, child.value])

return (
  <div ref={myRef}>
    ...
  </div>
)
...
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