Skip to content

Instantly share code, notes, and snippets.

@gpDA
Created September 19, 2021 20:08
Show Gist options
  • Save gpDA/1e667f1b550ea44231f7ae9fd61b902d to your computer and use it in GitHub Desktop.
Save gpDA/1e667f1b550ea44231f7ae9fd61b902d to your computer and use it in GitHub Desktop.
// things to go over
// 1 - IntersectionObserver why it is being loaded (rendering 20 players at a time vs. lazy loading)
// 2 - lazy loading using `lazy` classname
async componentDidMount() {
this.handleScroll();
}
handleScroll = () => {
const lazys = document.querySelectorAll('.lazy');
const config = {
threshold: 0.5
};
let observer = new IntersectionObserver(function(entries) {
entries.forEach(entry => {
if(entry.isIntersecting) {
lazyLoading(entry);
}
})
}, config)
lazys.forEach(lazy => {
observer.observe(lazy);
})
function lazyLoading(entry) {
const { songs } = this.props;
const player = entry.target
const id = parseInt(entry.target.id);
const url = songs.tracks[id].uri;
if(player.classList[0] === 'lazy') {
player.src = `https://open.spotify.com/embed?uri=${url}`;
player.classList.remove('lazy');
observer.unobserve(player);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment