Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!