-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: avoid to observe external links (#1739)
* fix: avoid to observe external links * refactor: less is more * refactor: lazy health check
- Loading branch information
Showing
3 changed files
with
117 additions
and
81 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* global IntersectionObserver */ | ||
|
||
import React, { useState, useEffect, useRef } from 'react' | ||
|
||
const LazyRender = ({ onView, placeholder, options }) => { | ||
const [isVisible, setIsVisible] = useState(false) | ||
const ref = useRef(null) | ||
|
||
useEffect(() => { | ||
const { current } = ref | ||
|
||
const observer = new IntersectionObserver(([entry]) => { | ||
if (entry.isIntersecting) { | ||
setIsVisible(true) | ||
observer.disconnect() | ||
} | ||
}, options) | ||
|
||
if (current) observer.observe(current) | ||
return () => current && observer.unobserve(current) | ||
}, [options]) | ||
|
||
return <div ref={ref}>{isVisible ? onView() : placeholder()}</div> | ||
} | ||
|
||
export default LazyRender |
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
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