Skip to content

Commit

Permalink
fix: skip creating the observer if nothing changed
Browse files Browse the repository at this point in the history
  • Loading branch information
thebuilder committed Aug 10, 2020
1 parent e59f675 commit 9abc46a
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/useInView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,27 @@ export function useInView(
options: IntersectionOptions = {},
): InViewHookResponse {
const ref = React.useRef<Element>()
const previousOptions = React.useRef<IntersectionOptions>()
const [state, setState] = React.useState<State>(initialState)

const setRef = React.useCallback(
(node) => {
if (ref.current) {
const prevOptions = previousOptions.current
if (
ref.current === node &&
prevOptions &&
prevOptions.triggerOnce === options.triggerOnce &&
prevOptions.threshold === options.threshold &&
prevOptions.rootMargin === options.rootMargin &&
prevOptions.root === options.root
) {
// Nothing changed - Most likely the component just tried to reassign the ref.
// We can safely return early
return
}

// Make sure we unobserve the current node, so we can create a new instance
unobserve(ref.current)
}

Expand All @@ -43,6 +59,7 @@ export function useInView(

// Store a reference to the node, so we can unobserve it later
ref.current = node
previousOptions.current = options
},
[options.threshold, options.root, options.rootMargin, options.triggerOnce],
)
Expand Down

0 comments on commit 9abc46a

Please sign in to comment.