react 监听页面滚动到底部

717 阅读1分钟
import React, { useRef } from 'react'; 
function MyComponent() { 
    const myRef = useRef(null); 
    const handleScroll = () => {
        const { scrollHeight, clientHeight, scrollTop } = myRef.current;
        if (scrollHeight - clientHeight <= scrollTop + 1) { 
            console.log('Reached bottom'); 
        } 
    }; 
    return ( 
    <div ref={myRef} onScroll={handleScroll} style={{ height: '500px', overflow: 'auto' }}> 
    {/* Your content */} 
    </div> );
   }