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

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

0

That's simple:

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

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