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

391
Visualizações
In React, is it okay to use getElementById, querySelector, etc versus useRef if you don't perform any DOM manipulation?

Specifically used just to view the heights/widths (getBoundingClientRect) of elements. I recently encountered a need to find all the separate heights of a dynamic amount of child elements to perform a calculation. I added a ref within the child component, and passed a function down from the parent in an attempt to update the parent's list of child dimensions (which was in state). I found this to be overtly complex and confusing and unreliable. So, in the parent, I just did a simple for loop with getElementById after giving each child an id of child-${index}.

I know you are NOT supposed to do any direct DOM manipulation in React; however, if your goal is read some data only, then is it an issue or bad practice?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It might not be a problem right now, but I would consider using getElementById instead of a ref bad practice in general (or at least call it "a workaround").

1

getElementById works "outside" of React, so you are not using React here.

That might work for now, but also might interfere with what might do React at some time. E.g. you might access or hold a reference to a DOM node, and React might decide to remove that node while you were reading it. I don't see why this might happen in your example, but when using two separate systems it is hard to keep track of the possible consequences.

2

With the id child-${index} you have introduced a logical dependency (coupling) between the parent and the child. The id child-${index} acts as a reference here, and has to be kept in sync manually.

This might be easier in a short term, but is actually more complex as a general approach (e.g. less maintainable, reusable, ...). You could say, Reacts whole purpose is to avoid such complexities.

Your components should be as independent of each other as possible, and should only communicate through the props.

suggestion

I suggest to avoid both getElementById and passing a ref, and have the children know their size (e.g. using a custom hook), and pass only the sizes up to the parent (not the ref).

If that is not possible, I would prefer to use refs.

Also note that "confusion" is not the same as "complexity": Confusion can be decreased by acquiring more information, but complexity is an inherent property of a system.

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