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

113
Vistas
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 Respuestas
Responde la pregunta

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