ReactJS how to render a component only when scroll down and reach it on the page?

后端 未结 2 803
我寻月下人不归
我寻月下人不归 2020-12-19 05:03

I have a react component Data which includes several charts components; BarChart LineChart ...etc.

When Data comp

2条回答
  •  眼角桃花
    2020-12-19 05:42

    You have at least three options how to do that:

    1. Track if component is in viewport (visible to user). And then render it. You can use this HOC https://github.com/roderickhsiao/react-in-viewport

    2. Track ‘y’ scroll position explicitly with https://react-fns.netlify.com/docs/en/api.html#scroll

    3. Write your own HOC using Intersection Observer API https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API

    To render component you may need another HOC, which will return Chart component or ‘null’ based on props it receives.

提交回复
热议问题