Skip to content

Instantly share code, notes, and snippets.

@elvisgiv
Last active June 20, 2019 13:26
Show Gist options
  • Save elvisgiv/82fb848251333994a67ec262a86a6b06 to your computer and use it in GitHub Desktop.
Save elvisgiv/82fb848251333994a67ec262a86a6b06 to your computer and use it in GitHub Desktop.
    constructor(props) {
        super(props);
        this.state = {
            ...
            loadingMore: false,
        };
        ...
        this.onScroll = this.onScroll.bind(this);
    }
    
    componentDidMount() {
        window.addEventListener('scroll', this.onScroll, true);
    }

    componentWillUnmount() {
        window.removeEventListener('scroll', this.onScroll);
    }
    
    onScroll() {
        let scroll = $('#scroll-to');
        let hT = scroll.offset().top;
        let wH = $(window).height();
        //
        if (((hT - wH) <= 0) && !this.state.loadingMore) {
            this.setState({loadingMore: true});
            this.METHOD_WITH_YOUR_LOGIC();
        }
    };
    
    METHOD_WITH_YOUR_LOGIC() {
        // your logic here
        
        this.setState({loadingMore: false})
    }
    
    render() {
        return (
            <div>
                ...
                <div id='scroll-to'>
                </div>
            </div>
        )
    }    

!!! important !!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment