mirror of
https://github.com/streamich/react-use.git
synced 2026-01-25 14:17:16 +00:00
fix(useIntersection): return null if IntersectionObserver is not supported
This commit is contained in:
parent
3f8687e1f5
commit
4f6d3887be
@ -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]);
|
||||
};
|
||||
|
||||
@ -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');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user