Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Add hook-ey react-intersection-observer API#622

Merged
lemonmade merged 5 commits intomasterfrom
use-intersection-observer
Apr 9, 2019
Merged

Add hook-ey react-intersection-observer API#622
lemonmade merged 5 commits intomasterfrom
use-intersection-observer

Conversation

@lemonmade
Copy link
Member

This PR adds a new useIntersection hook to react-intersection-observer, and slightly reworks the API of the component version to feel more closely related. The README has all of the goods, but basically, the hook version just gives you the state + a ref to throw on a DOM node, so it's quite simple to use the hook version:

function MyComponent() {
  const [intersection, intersectionRef] = useIntersection();

  return (
    <div ref={intersectionRef}>Intersection: {intersection.isIntersecting}</div>
  );
}

return [intersectionEntry, node];
}

export function useValueTracking<T>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, agreed, we just don't have a place for them yet, and I didn't want to throw that in here too.

Copy link
Contributor

@marutypes marutypes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small (mostly documentation readability) comments, but this looks good overall to me.

const [intersectionEntry, setIntersectingEntry] = useState<
IntersectionObserverEntry
>(() => ({
boundingClientRect: emptyBoundingClientRect,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should each of these maybe be clones instead of a reference to the same object?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it matters, all the fields are meant to be readonly

@lemonmade lemonmade merged commit 961a53d into master Apr 9, 2019
@lemonmade lemonmade deleted the use-intersection-observer branch April 9, 2019 19:07
lemonmade added a commit that referenced this pull request Apr 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants