mirror of
https://github.com/ragestudio/comty.git
synced 2025-07-08 16:54:15 +00:00
Refactor LoadMore to use ref instead of getElementById
This commit is contained in:
parent
a608f967c0
commit
d1b58d19fc
@ -3,7 +3,7 @@ import classnames from "classnames"
|
|||||||
|
|
||||||
import "./index.less"
|
import "./index.less"
|
||||||
|
|
||||||
export default React.forwardRef((props, ref) => {
|
const LoadMore = React.forwardRef((props, ref) => {
|
||||||
const {
|
const {
|
||||||
className,
|
className,
|
||||||
children,
|
children,
|
||||||
@ -12,10 +12,16 @@ export default React.forwardRef((props, ref) => {
|
|||||||
contentProps = {},
|
contentProps = {},
|
||||||
} = props
|
} = props
|
||||||
|
|
||||||
|
const nodeRef = React.useRef(null)
|
||||||
|
|
||||||
let observer = null
|
let observer = null
|
||||||
|
|
||||||
const insideViewportCb = (entries) => {
|
const insideViewportCb = (entries) => {
|
||||||
const { fetching, onBottom } = props
|
const { fetching, onBottom, hasMore } = props
|
||||||
|
|
||||||
|
if (!hasMore) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
entries.forEach((element) => {
|
entries.forEach((element) => {
|
||||||
if (element.intersectionRatio > 0 && !fetching) {
|
if (element.intersectionRatio > 0 && !fetching) {
|
||||||
@ -26,10 +32,8 @@ export default React.forwardRef((props, ref) => {
|
|||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
try {
|
try {
|
||||||
const node = document.getElementById("bottom")
|
|
||||||
|
|
||||||
observer = new IntersectionObserver(insideViewportCb)
|
observer = new IntersectionObserver(insideViewportCb)
|
||||||
observer.observe(node)
|
observer.observe(nodeRef.current)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log("err in finding node", err)
|
console.log("err in finding node", err)
|
||||||
}
|
}
|
||||||
@ -44,9 +48,10 @@ export default React.forwardRef((props, ref) => {
|
|||||||
<div ref={ref} className={classnames(className)} {...contentProps}>
|
<div ref={ref} className={classnames(className)} {...contentProps}>
|
||||||
{children}
|
{children}
|
||||||
|
|
||||||
<div style={{ clear: "both" }} />
|
{/* <div style={{ clear: "both" }} /> */}
|
||||||
|
|
||||||
<div
|
<div
|
||||||
|
ref={nodeRef}
|
||||||
id="bottom"
|
id="bottom"
|
||||||
className="bottom"
|
className="bottom"
|
||||||
style={{ display: hasMore ? "block" : "none" }}
|
style={{ display: hasMore ? "block" : "none" }}
|
||||||
@ -56,3 +61,7 @@ export default React.forwardRef((props, ref) => {
|
|||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
LoadMore.displayName = "LoadMore"
|
||||||
|
|
||||||
|
export default LoadMore
|
||||||
|
Loading…
x
Reference in New Issue
Block a user