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

163
Views
Infinite load time on mobile device

I am trying to display a loader till the page finishes rendering and for that I used this code:

export default function App() {
  const [isLoading, setIsLoading] = React.useState(true);

  const handleLoading = () => {
    setIsLoading(false);
  };

  useEffect(() => {
    window.addEventListener('load', handleLoading);
    return () => window.removeEventListener('load', handleLoading);
  }, []);

  return (
    <div>
      {isLoading ? (
        <Load />
      ) : (
        <div className="app">
          <Topbar />
          <div className="parts">
            <Intro />
            <About />
            <Projects />
            <Contact />
          </div>
        </div>
      )}
    </div>
  );
}

This works fine on desktop devices except in incognito mode and in mobile devices. It shows the loader forever and when I press the refresh button twice only then will the page appear. I tested the site on gtmetrix and I got this error: Lighthouse Error: The page did not paint any content. How do I solve this?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

useEffect will get called when your state has changed, right?

then when do you think your useEffect will be called?

the component is created, the loading state is set to true, and everything is working good, why would your component calls your useEffect when the state is not even changing?

i have two suggestions:

  1. remove your 'load' event listener and add it to your main function code, this way the event listener will be initiated. then when the page loads, it will set your state into false, thus your useEffect will be called, and it removes the event listener.

  2. is to use class based components with lifecycle hooks that are designed for what you are trying to do.

about 4 years ago · Juan Pablo Isaza Report

0

I'm not sure that eventListener works fine in your case but if I were you I would simply setIsLoading(false) in useEffect and if you want it to stop before Component mounts you can do it in this way :

export default function App() {
const [isLoading, setIsLoading] = React.useState(true);
const [mounted,setMounted] = React.useState(false);

if(!mounted) setIsLoading(false)

useEffect(() => {
    setMounted(true)
}, []);

return (
    <div>
        {isLoading ? (
            <Load />
        ) : (
            <div className="app">
                <Topbar />
                <div className="parts">
                    <Intro />
                    <About />
                    <Projects />
                    <Contact />
                </div>
            </div>
        )}
    </div>
);

}

hope it was helpful ;)

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!