Skip to content

Instantly share code, notes, and snippets.

@tolumide-ng
Last active February 1, 2021 11:21
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 tolumide-ng/78453b76c7a82512eb2e950875636a8e to your computer and use it in GitHub Desktop.
Save tolumide-ng/78453b76c7a82512eb2e950875636a8e to your computer and use it in GitHub Desktop.
import * as React from "react";
export const DisplayProductsComponent = () =>< {
const {createScrollObserver, converge, setConverge} = useIntersectionObserver();
const [product, setProduct] = React.useState<ProductDef>(undefined);
const loadMoreRef = React.useRef<HTMLDivElement>(null);
let scrollObserver = createScrollObserver();
React.useEffect(() => {
// other conditions/cases
if(loadMoreRef?.current) {
scrollObserver.observe(loadMoreRef.current);
}
}, [product])
React.useEffect(() => {
if(converge && whateverOtherCondition){
loadMoreFunc();
// Return converge back to it's original state, so that we can be notified when there is another convergence
// as the user scrolls after more content has been loaded
setConverge(false);
}
}, [converge]);
function loadMore(){
fetch(...);
}
return (
<>
...
<div ref={loadMoreRef}></div>
</>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment