fix(useIntersection): return null if IntersectionObserver is not supported

This commit is contained in:
dizel3d 2020-01-28 00:25:11 +03:00
parent 3f8687e1f5
commit 4f6d3887be
2 changed files with 9 additions and 1 deletions

View File

@ -7,7 +7,7 @@ const useIntersection = (
const [intersectionObserverEntry, setIntersectionObserverEntry] = useState<IntersectionObserverEntry | null>(null);
useEffect(() => {
if (ref.current) {
if (ref.current && typeof IntersectionObserver === 'function') {
const handler = (entries: IntersectionObserverEntry[]) => {
setIntersectionObserverEntry(entries[0]);
};

View File

@ -78,6 +78,14 @@ describe('useIntersection', () => {
expect(result.current).toEqual(null);
});
it('should return null if IntersectionObserver is not supported', () => {
targetRef = createRef();
targetRef.current = document.createElement('div');
delete window.IntersectionObserver;
expect(() => renderHook(() => useIntersection(targetRef, {}))).not.toThrow();
});
it('should disconnect an old IntersectionObserver instance when the ref changes', () => {
targetRef = createRef();
targetRef.current = document.createElement('div');