Skip to content

Instantly share code, notes, and snippets.

@omariosouto
Created July 14, 2021 15:50
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save omariosouto/1520928479310d940d72f370bb32a877 to your computer and use it in GitHub Desktop.
Save omariosouto/1520928479310d940d72f370bb32a877 to your computer and use it in GitHub Desktop.
import { useEffect, useState } from 'react';
import { createGlobalStyle } from 'styled-components'
const GlobalStyle = createGlobalStyle`
body {
font-family: sans-serif;
}
main {
display: flex;
flex-direction: column;
align-items: center;
}
ul {
width: 300px;
height: 600px;
overflow-y: scroll;
padding: 0;
background-color: #ddd;
}
li {
height: 150px;
padding: 15px;
}
li img {
--size: 75px;
width: var(--size);
height: var(--size);
border-radius: 50%;
}
li div {
padding: 15px;
background-color: #fff;
height: calc(100% - 30px);
}
#sentinela {
width: 100%;
height: 5px;
background-color: red;
}
`;
export default function Home() {
const [followers, setFollowers] = useState([]);
const [currentPage, setCurrentPage] = useState(1);
useEffect(() => {
const perPage = 10;
const ENDPOINT = 'https://api.github.com/users/omariosouto/followers';
const URL = `${ENDPOINT}?per_page=${perPage}&page=${currentPage}&order=DESC`;
fetch(URL)
.then((response) => response.json())
.then((newFollowers) => setFollowers((prevFollowers) => [...prevFollowers, ...newFollowers]))
}, [currentPage]);
useEffect(() => {
const intersectionObserver = new IntersectionObserver(entries => {
if (entries.some(entry => entry.isIntersecting)) {
console.log('Sentinela appears!', currentPage + 1)
setCurrentPage((currentValue) => currentValue + 1);
}
})
intersectionObserver.observe(document.querySelector('#sentinela'));
return () => intersectionObserver.disconnect();
}, []);
return (
<main>
<GlobalStyle />
<h1>GitHub API: Infinite Scroller</h1>
<h2>Página atual: {currentPage}</h2>
<ul>
{followers.map(follower => (
<li key={follower.login}>
<div>
<img src={`https://github.com/${follower.login}.png`} />
<p>
github.com/<strong>{follower.login}</strong>
</p>
</div>
</li>
))}
<li id="sentinela"></li>
</ul>
</main>
)
}
@luizpjr
Copy link

luizpjr commented Sep 23, 2021

Fala Mario, tudo certo?
Cara estou iniciando nessa jornada de DEV, encontrei seu vídeo sobre o assunto e achei top, estou desenvolvendo este site ( https://bloghallyu.000webhostapp.com/ ) e gostaria de implementar esta aplicação nele, porem estou com um pouco de dificuldade, pode dar uma luz?
Vlw e parabéns pelos conteúdos.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment