简体   繁体   English

Airbnb / 反应日期 | 有没有办法无限滚动几个月而不是点击下个月?

[英]Airbnb / react-dates | Is there a way to infinity scroll months instead of clicking next month?

I have a react-dates component in a web React app for mobile browsers.我在用于移动浏览器的 web React 应用程序中有一个react-dates组件。 The component displays the month views vertically.该组件垂直显示月份视图。

 <DayPickerRangeControllerWrapper
   orientation="vertical"
   numberOfMonths={3}
   verticalHeight={800}
 />

The component initialized to display 3 months.组件初始化为显示 3 个月。 To move on to the next month, the user needs to click a button.要进入下个月,用户需要单击一个按钮。

The goal is to implement an infinity scroll - display the next month by scrolling instead of clicking.目标是实现无限滚动 - 通过滚动而不是单击来显示下个月。 For example, when the user scrolls down to the last displayed month, the next month (4th in this case) should render, and so on.例如,当用户向下滚动到最后显示的月份时,应该呈现下个月(在本例中为第 4 个月),依此类推。

I went over the Docs and found onNextMonthClick function, but couldn't find a way to invoke it by scrolling.我查看了文档并找到了onNextMonthClick function,但找不到通过滚动来调用它的方法。

I also tried to set the numberOfMonths to a large number (24) but it causes a delay in the component's render.我还尝试将numberOfMonths设置为一个很大的数字 (24),但这会导致组件的渲染延迟。

Is it possible to lazy load month by month by scrolling instead of clicking?是否可以通过滚动而不是单击来逐月延迟加载?

To render months by user scroll, you should add an event listener to the DOM, and update the number of months relative to scrolling.要通过用户滚动呈现月份,您应该向 DOM 添加一个事件侦听器,并更新与滚动相关的月份数。

For example:例如:

import React from "react";
import ReactDOM from "react-dom";
import { Grid, Row } from "react-flexbox-grid";
import { DayPickerRangeController } from "react-dates";
import "react-dates/initialize";

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { numberOfMonths: 0 };
  }

  componentDidMount() {
    window.addEventListener("scroll", this.handleScroll, { passive: true });
  }

  componentWillUnmount() {
    window.removeEventListener("scroll", this.handleScroll);
  }

  handleScroll = (event) => {
    this.setState((state, props) => ({
      numberOfMonths: state.numberOfMonths++
    }));
  };
  render = () => {
    return (
      <Grid>
        <Row>
          <DayPickerRangeController
            orientation="vertical"
            numberOfMonths={this.state.numberOfMonths}
            verticalHeight={800}
          />
        </Row>
      </Grid>
    );
  };
}


ReactDOM.render(<App />, document.getElementById("container"));

codesandbox demo 代码沙盒演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM