Skip to content

icyJoseph/lazy-render-with-img

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lazy Render

Perhaps an anti pattern, but this project shows how to use loading=lazy on an img element to lazy render a component.

In this particular case, the lazy rendered component, triggers a call to react-toastify.

import { ToastContainer, toast } from "react-toastify"; import { LazyRender } from "native-lazy-render"; const HelloWorld = () => { useEffect(() => { toast("Hello World!", { toastId: "Hello" }); }, []); return null; }; export default function App() { const [show, setShow] = useState(true); useEffect(() => { if (!show) { const handler = () => setShow(true); window.addEventListener("scroll", handler); return () => window.removeEventListener("scroll", handler); } }, [show]); return ( <> <ToastContainer /> <header className="text"> <h1>Lazy Render</h1> <h2>Scroll down</h2> <button onClick={() => setShow(false)}>Reload</button> </header> <div className="long"></div> {show && ( <LazyRender> <HelloWorld /> </LazyRender> )} <footer className="text">End</footer> </> ); }

About

Lazy render using native image lazy loading

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published