Skip to content

Instantly share code, notes, and snippets.

@thebuilder
Last active January 26, 2020 00:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thebuilder/5aa8a8d080228c0a33cecde43f14603b to your computer and use it in GitHub Desktop.
Save thebuilder/5aa8a8d080228c0a33cecde43f14603b to your computer and use it in GitHub Desktop.
useInView
/* eslint-disable react-hooks/exhaustive-deps */
import * as React from 'react'
import { observe, unobserve } from './intersection'
import { InViewHookResponse, IntersectionOptions } from './index'
type State = {
inView: boolean
entry?: IntersectionObserverEntry
}
export function useInView(
options: IntersectionOptions = {},
): InViewHookResponse {
const ref = React.useRef<Element>()
const [state, setState] = React.useState<State>({
inView: false,
entry: undefined,
})
const setRef = React.useCallback(
node => {
if (ref.current) {
unobserve(ref.current)
}
if (node) {
observe(
node,
(inView, intersection) => {
setState({ inView, entry: intersection })
if (inView && options.triggerOnce) {
// If it should only trigger once, unobserve the element after it's inView
unobserve(node)
}
},
options,
)
}
// Store a reference to the node
ref.current = node
},
[options.threshold, options.root, options.rootMargin, options.triggerOnce],
)
React.useDebugValue(state.inView)
return [setRef, state.inView, state.entry]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment