Skip to content

Commit

Permalink
fix(useIntersection): return null if IntersectionObserver is not supp…
Browse files Browse the repository at this point in the history
…orted
  • Loading branch information
dizel3d committed Jan 27, 2020
1 parent 3f8687e commit 4f6d388
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/useIntersection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
};
Expand Down
8 changes: 8 additions & 0 deletions tests/useIntersection.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 4f6d388

Please sign in to comment.