Last active
June 25, 2020 12:38
-
-
Save velotiotech/3aaa197a406bec277bf273181abad8c0 to your computer and use it in GitHub Desktop.
Cleaner Efficient Code with Hooks and Functional Programming - Fixing Huge Components with hooks - Logic separation using a custom hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useState } from "react"; | |
export function useScroll() { | |
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 | |
}; | |
}, []); | |
return data; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment