Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useIntersect): call live callback #1084

Merged
merged 2 commits into from Oct 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/core/useIntersect.tsx
Expand Up @@ -5,6 +5,8 @@ export function useIntersect<T extends THREE.Object3D>(onChange: (visible: boole
const ref = React.useRef<T>(null!)
const check = React.useRef(false)
const temp = React.useRef(false)
const callback = React.useRef(onChange)
React.useLayoutEffect(() => void (callback.current = onChange), [onChange])
React.useEffect(() => {
const obj = ref.current
if (obj) {
Expand All @@ -18,7 +20,7 @@ export function useIntersect<T extends THREE.Object3D>(onChange: (visible: boole
obj.onBeforeRender = () => (check.current = true)
// Compare the check value against the temp value, if it differs set state
const unsub2 = addAfterEffect(() => {
if (check.current !== temp.current) onChange((temp.current = check.current))
if (check.current !== temp.current) callback.current?.((temp.current = check.current))
return true
})
return () => {
Expand Down