简体   繁体   中英

Does useEffect block rendering the page?

I would like to do a little load time every time the user arrives or reloads the page. I use for that a useEffect () which has a setTimeout () and which simply sets the variable setLoading to false after 1 second. But my question is does this setTimeout block the return of the function? Otherwise it is useless since I would like this useEffect to allow the page to load the html and render it almost instantly.

 const [isLoading, setLoading] = useState(true); useEffect(() => { const timer = setTimeout(() => { setLoading(false); }, 1000); return () => clearTimeout(timer); });

 {isLoading? <Loading />: ( <Router> <Switch> <Route exact path="/"> <Header showModal={showModal} /> <div className="top"> <div className="leftPart"> <Presentation /> <Skills /> </div> <Timeline /> </div> <Projects /> <Footer /> <ConnexionModal showModal={openModal} hideModal={hideModal} /> </Route> <Route exact path="/admin"> <Admin /> </Route> <Route path="*"> <Erreur404 /> </Route> </Switch> </Router> )}

No, it's non-blocking by definition.

But, if you want to implement a loading logic, useEffect is definitely the right tool.

You just need to pass a second argument to it.

  const [isLoading, setLoading] = useState(true);

  useEffect(() => {
    const timer = setTimeout(() => {
      setLoading(false);
    }, 1000);
  }, []);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM