Skip to content

Commit

Permalink
fix: linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Dec 2, 2023
1 parent 9ff59bb commit 3225e06
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
7 changes: 4 additions & 3 deletions packages/react/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ const Card = props => {
[lazy]
)
const lazyOptions = useMemo(() => (isObject(lazy) ? lazy : undefined), [lazy])
const [hasIntersected, cardRef] = useIntersectionObserver(
const cardRef = React.useRef(null)
const hasIntersected = useIntersectionObserver(
isLazyEnabled,
cardRef,
lazyOptions
)

Expand Down Expand Up @@ -156,7 +158,7 @@ microlink.io/${error.code.toLowerCase()}
}
}, [apiUrlProps, fetchData, apiUrl, mergeData, canFetchData])

useEffect(toFetchData, [url, setData, hasIntersected])
useEffect(toFetchData, [url, toFetchData, setData, hasIntersected])

const isLoading = isLoadingUndefined ? loadingState : loading

Expand Down Expand Up @@ -194,7 +196,6 @@ microlink.io/${error.code.toLowerCase()}
className={`${classNames.main} ${className}`.trim()}
href={url}
isLoading={isLoading}
ref={cardRef}
{...restProps}
>
{isLoading ? (
Expand Down
35 changes: 14 additions & 21 deletions packages/react/src/utils/hooks.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
/* global IntersectionObserver */

import { useCallback, useState } from 'react'
import { useEffect, useState } from 'react'

export const useIntersectionObserver = (enabled, options) => {
export const useIntersectionObserver = (enabled, ref, options) => {
const [hasIntersected, setHasIntersected] = useState(false)

const refCallback = useCallback(
node => {
if (enabled) {
const onIntersect = ([entry], self) => {
if (entry.isIntersecting) {
setHasIntersected(true)
self.unobserve(entry.target)
}
useEffect(() => {
if (enabled && ref.current) {
const onIntersect = ([entry], self) => {
if (entry.isIntersecting) {
setHasIntersected(true)
self.unobserve(entry.target)
}
const observer = new IntersectionObserver(onIntersect, options)

if (node !== null) {
observer.observe(node)
}
} else {
setHasIntersected(true)
}
},
[enabled, options]
)
const observer = new IntersectionObserver(onIntersect, options)
observer.observe(ref.current)
return () => observer.disconnect()
}
}, [ref, enabled, options])

return [hasIntersected, refCallback]
return hasIntersected
}

0 comments on commit 3225e06

Please sign in to comment.