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

290
Views
React detect the change in iframe's src

I have an iframe element inside my component and I need to somehow detect when the URL of that iframe changes. What I have tried so far:

  1. Added onLoad callback, but this does not trigger every time I redirect in the iframe
  2. Used React.useCallback but also does not work as I wanted to
  3. Create timeout and get the URL from the iframe every x seconds

Minimalistic code example below

export const XComponent = (props: XComponentProps) => {
  
  const ref = React.useRef<any>();

  1.
  const onLoad = () => {
    const url = ref.current.contentWindow.location.href;
    // do stg with url
  }

  2.
  const getRef = React.useCallback((node: any) => {
    // store node into state, this was not triggered properly either
  }, []);

  3.
  React.useEffect(() => {
    const interval = setInterval(() => {
      const url = ref.current.contentWindow.location.href;
    }, 1000);
    return () => clearInterval(interval);
  }, []);

  return (
    <div className={ styles.albertStoremanTab }>
      <div className={ styles.container }>
        <iframe id={"iframe-my-id"} onLoad={onLoad}  src={props.defaultUrl} ref={ref}></iframe>
      </div>
    </div>
  );
};
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

That's simple:

useEffect(() => {
    // It runs only when defaultUrl changes
}, [props.defaultUrl])
about 4 years ago · Juan Pablo Isaza Report

0

Did you try using ref.current in the dependency array of the useEffect?

useEffect(() => { setURL(ref.current.contentWindow.location.href) }, [ref.current])

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!