Skip to content

Instantly share code, notes, and snippets.

@velotiotech
Last active June 25, 2020 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save velotiotech/6ba646d4cf006c16497dfeb0104d9521 to your computer and use it in GitHub Desktop.
Save velotiotech/6ba646d4cf006c16497dfeb0104d9521 to your computer and use it in GitHub Desktop.
Cleaner Efficient Code with Hooks and Functional Programming - Fixing Huge Components with hooks
import React, { useEffect, useState } from "react";
export const LazyLoader = ({ isLoading, isCompleted }) => {
const [data, setData] = useState([]);
const loadMore = () => {
// Load and setData here
};
const handleScroll = () => {
if (!isLoading && isCompleted) {
loadMore();
}
};
// cDM and cWU
useEffect(() => {
document.addEventListener("scroll", handleScroll, false);
// more subscribers and event listeners
return () => {
document.removeEventListener("scroll", handleScroll, false);
// unsubscribe and remove listeners
};
}, []);
// cDU
useEffect(() => {
//
}, [/** dependencies */]);
return data && <div>{data}</div>;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment