-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Add] feed scroll intersect observer 사용으로 변경 #32
why 비동기로 scrollEvent 를 감지하기 위해서 사용 how intersection observer를 hook으로 분리하여 사용
- Loading branch information
1 parent
d6b1a03
commit 9e5f57e
Showing
2 changed files
with
72 additions
and
13 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
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,37 @@ | ||
import { useState, useEffect, useCallback } from 'react'; | ||
|
||
const baseOption = { | ||
root: null, | ||
threshold: 0, | ||
rootMargin: '0px' | ||
}; | ||
|
||
const useIntersect = (interSectAction: any, option: any) => { | ||
const [ref, setRef] = useState(null); | ||
|
||
const checkIntersect = ( | ||
entry: IntersectionObserverEntry[], | ||
observer: IntersectionObserver | ||
) => { | ||
if (entry[0].isIntersecting) { | ||
// console.log('im in'); | ||
// observer.unobserve(entry[0].target); | ||
interSectAction(); | ||
// observer.observe(entry[0].target); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
// console.log('aaaaaaaaa', ref); | ||
let observer: any; | ||
if (ref) { | ||
observer = new IntersectionObserver(checkIntersect, baseOption); | ||
// console.log('----------------', ref); | ||
observer.observe(ref); | ||
} | ||
return () => observer && observer.disconnect(); | ||
}, [ref, option.root, option.threshold, option.rootMargin, checkIntersect]); | ||
return [ref, setRef]; | ||
}; | ||
|
||
export default useIntersect; |