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

114
Visualizações
Accessing DOM node using useState hook in react
const [contentRef, setContentRef] = useState<any>(null);

const doc = contentRef?.contentWindow?.document;
const mountNode = doc?.body;
const insertionTarget = doc?.head;

  return (
    <Box
      as="iframe"
      ref={setContentRef}
      {...props}
    >
      {* some code here *}
    </Box>

Recently while working with iframes and @emotion/react i came across this code but I am not sure about using useState for accessing DOM node. when I console.log(contentRef) it returns iframe DOM node.

Can someone please explain how is this working and how it is different from using useRef().

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

0

What you see there is a "callback ref".

Instead of passing a ref attribute created by createRef(), you pass a function. The function receives the React component instance or HTML DOM element as its argument, which can be stored and accessed elsewhere.

setContentRef is a function that accepts the new state value. So the DOM node is passed to the setState call after its rendered. You will be able to access the DOM node in contentRef state.

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