Last active
October 26, 2021 12:15
-
-
Save gustavofabro/2d3c711b5b526eca7a2d41162ee8f7bb to your computer and use it in GitHub Desktop.
Uso da flag isLoading
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
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