Skip to content

Instantly share code, notes, and snippets.

@gustavofabro
Last active October 26, 2021 12:15
Show Gist options
  • Save gustavofabro/2d3c711b5b526eca7a2d41162ee8f7bb to your computer and use it in GitHub Desktop.
Save gustavofabro/2d3c711b5b526eca7a2d41162ee8f7bb to your computer and use it in GitHub Desktop.
Uso da flag isLoading
function App() {
const [isLoading, setIsLoading] = useState(false);
// Restante do código
useEffect(() => {
const observer = new IntersectionObserver(
(entries: IntersectionObserverEntry[]) => {
if (!entries[0].isIntersecting || isLoading) {
return;
}
loadItems();
},
{
root: null,
rootMargin: '0px',
threshold: 0
});
if (loadingElementRef && loadingElementRef.current) {
observer.observe(loadingElementRef.current);
}
return () => observer.disconnect();
}, [loadingElementRef, isLoading, loadItems]);
return (
<main className="app">
<div className="list">
{data.map(item => (
<div key={item.id} className="item">{`Item ${item.id + 1}`}</div>
))}
{isLoading && <i className="loading">Carregando...</i>}
<div ref={loadingElementRef}></div>
</div>
</main>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment