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

156
Visualizações
Failing to attach event listener to window object in nextjs

Can someone please why can I not attach an event listener to window object in nextjs? When I try 'resize' event, everything works fine, but with 'load' event it seems 'load' event is not recognized'

import { useEffect } from "react";

export default function Home() {
  useEffect(() => {
    window.addEventListener("load", (e) => console.log(e));
  });

  return <div>Hello world</div>;
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem is that the page has already loaded when this code begins to run. I'm not sure what you're trying to achieve, but from what i see it can be done with just the useEffect hook.

You can verify what i just said by running this snippet :

const handler = (e) => console.log(e, "here");

  useEffect(() => {
    if (document.readyState === "complete") {
      handler();
    } else {
      window.addEventListener("load", handler);
      return () => document.removeEventListener("load", handler);
    }
  }, []);

Also I have to note that this is not nextJs specific, this is just react.

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