Skip to content

Instantly share code, notes, and snippets.

@marekpiechut
Last active July 3, 2020 11:58
Show Gist options
  • Select an option

  • Save marekpiechut/84da94467ee2cdc0970895ba4e78980c to your computer and use it in GitHub Desktop.

Select an option

Save marekpiechut/84da94467ee2cdc0970895ba4e78980c to your computer and use it in GitHub Desktop.
Get lazy loading cheap with intersectionobserver i ccc4a072ab24457ea3de05cd8ed8f5ed
const ExerciseItem = ({ exercise }: Props) => {
const [url, setUrl] = useState<?string>(null)
const thisRef = useRef<?HTMLDivElement>(null)
useEffect(() => {
let observer = new IntersectionObserver(
entries => entries.forEach(entry => {
if (entry.isIntersecting) {
thumbLoader(media).then(setUrl)
observer = observer.disconnect()
}
})
)
observer.observe(thisRef.current)
return () => (observer = observer && observer.disconnect())
}, [media, thumbLoader])
return (
<div
css={styles.thumb}
style={{ backgroundImage: `url(${url})` }}
ref={thisRef}
/>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment