javascript - How to React js event on scroll load other components - Stack Overflow

I have more ponents in one page. All pages loaded at the same time.But i want after scroll event loadi

I have more ponents in one page. All pages loaded at the same time. But i want after scroll event loading next ponentas.

HomePage.js:

render() {
        return (
            <section className="section1">
                    <Component1 />
            </section>          <section className="section2">                
                    <Component2 />                
            </section>
             <section className="section3">
                <Component3 />
             </section>
             <section className="section4">
                <Component4 />
             </section>
             <section className="section5">
                <Component5 />
            </section>
            <footer>
                <Footer />
            </footer>       
         );
    }

Have any idea? It is possible in react.

Thank you!

I have more ponents in one page. All pages loaded at the same time. But i want after scroll event loading next ponentas.

HomePage.js:

render() {
        return (
            <section className="section1">
                    <Component1 />
            </section>          <section className="section2">                
                    <Component2 />                
            </section>
             <section className="section3">
                <Component3 />
             </section>
             <section className="section4">
                <Component4 />
             </section>
             <section className="section5">
                <Component5 />
            </section>
            <footer>
                <Footer />
            </footer>       
         );
    }

Have any idea? It is possible in react.

Thank you!

Share Improve this question asked Dec 19, 2017 at 16:22 Botir ZiyatovBotir Ziyatov 5181 gold badge6 silver badges20 bronze badges 1
  • I'm not too sure how to implement this, however I know there's an npm package that could acplish this: npmjs./package/react-infinite – MEnf Commented Dec 19, 2017 at 16:31
Add a ment  | 

1 Answer 1

Reset to default 4

You'll need to listen to a scroll event -- something like this:

ponentDidMount() {
    window.addEventListener('scroll', this.handleScroll)
}

Keep in mind that you may want to listen to a scroll event on a specific element, and not the entire window. This will depend on your requirements for how/when these other ponents should load.

In handling that event, keep track of your scroll distance somehow.

handleScroll = (event) => {
    this.setState({
        scrollY: window.scrollY
    });
}

Note: Updating your state on every scroll event is probably over-kill though. I think the best approach would be to detect when the user has scrolled to the bottom and then conditionally load/render new ponents.

Then, in your render function, conditionally render additional ponents based on the value of your scroll distance. Again, you will likely need to include more sophisticated logic here based on your specific needs, but here is a rudimentary example based on scroll distance:

render() {
    let additionalComponents = null;

    if (this.state.scrollY > 1000) { //arbitrary amount
        additionalComponents = (
            <Component6 />
        );
    }

    return (
        <section className="section1">
                <Component1 />
        </section>          <section className="section2">                
                <Component2 />                
        </section>
         <section className="section3">
            <Component3 />
         </section>
         <section className="section4">
            <Component4 />
         </section>
         <section className="section5">
            <Component5 />
        </section>

        { additionalComponents }

        <footer>
            <Footer />
        </footer>       
     );
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745221654a4617276.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信