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

Passing ref from parent to useTracker fails to detect inViewport #11

Open
ffdead opened this issue Jan 12, 2023 · 1 comment
Open

Passing ref from parent to useTracker fails to detect inViewport #11

ffdead opened this issue Jan 12, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@ffdead
Copy link
Member

ffdead commented Jan 12, 2023

Seems like there is a race condition with the IntersectionObserver when the ref is defined in the parent component.

For now, make sure you use the useTracker() hook in the same component that collects the ref to be safe.

@ffdead ffdead changed the title Passing ref from parent to useTracker fails Passing ref from parent to useTracker fail to detect inViewport Feb 9, 2023
@ffdead ffdead changed the title Passing ref from parent to useTracker fail to detect inViewport Passing ref from parent to useTracker fails to detect inViewport Feb 9, 2023
@ffdead
Copy link
Member Author

ffdead commented May 25, 2023

I think it would be more robust if we changed the interface for useTracker to be something like this:

const [setRef, tracker] = useTracker()

<div ref={setRef}>

I think it could be done in an almost backwards compatible way - we could return the array if the hook is called with no arguments or a config object as the only param. And keep returning the old return value if the first parameter is a DOMRef (the way most people use it today)

Something like this:

// new api
const [setRef, tracker] = useTracker()
const [setRef, tracker] = useTracker({ rootMargin: "100%" })

// old api - backwards compatible
const tracker = useTracker(ref)
const tracker = useTracker(ref, { rootMargin: "100%" })

@ffdead ffdead added the bug Something isn't working label Aug 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant