项目作者: icyJoseph

项目描述 :
Lazy render using native image lazy loading
高级语言: TypeScript
项目地址: git://github.com/icyJoseph/lazy-render-with-img.git
创建时间: 2021-04-23T14:19:05Z
项目社区:https://github.com/icyJoseph/lazy-render-with-img

开源协议:Mozilla Public License 2.0

下载


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.

  1. import { ToastContainer, toast } from "react-toastify";
  2. import { LazyRender } from "native-lazy-render";
  3. const HelloWorld = () => {
  4. useEffect(() => {
  5. toast("Hello World!", { toastId: "Hello" });
  6. }, []);
  7. return null;
  8. };
  9. export default function App() {
  10. const [show, setShow] = useState(true);
  11. useEffect(() => {
  12. if (!show) {
  13. const handler = () => setShow(true);
  14. window.addEventListener("scroll", handler);
  15. return () => window.removeEventListener("scroll", handler);
  16. }
  17. }, [show]);
  18. return (
  19. <>
  20. <ToastContainer ></ToastContainer>
  21. <header className="text">
  22. <h1>Lazy Render</h1>
  23. <h2>Scroll down</h2>
  24. <button onClick={() => setShow(false)}>Reload</button>
  25. </header>
  26. <div className="long"></div>
  27. {show && (
  28. <LazyRender>
  29. <HelloWorld ></HelloWorld>
  30. </LazyRender>
  31. )}
  32. <footer className="text">End</footer>
  33. </>
  34. );
  35. }