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

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

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