简体   繁体   English

airbnb/react-dates DayPickerRangeController 在某些 function 调用上设置可见月份

[英]airbnb/react-dates DayPickerRangeController set visible month on some function call

Is there way to set DayPickerRangeController custom visible month after component rendered?有没有办法在组件渲染后设置 DayPickerRangeController 自定义可见月份? I have 'handleMonthChange' function which I want to change visible month when this function is called.我有 'handleMonthChange' function ,我想在调用此 function 时更改可见月份。 I try to set 'initialVisible' month but it is not working.我尝试设置“initialVisible”月份,但它不起作用。

import { useState } from "react";
import moment from "moment";
import { DayPickerRangeController } from "react-dates";
import "react-dates/initialize";
import "react-dates/lib/css/_datepicker.css";

const Date = (props) => {
  const [startDate, setStartDate] = useState(null);
  const [endDate, setEndDate] = useState(null);
  const [focusedInput, setFocusedInput] = useState("startDate");
  const [initialMonth, setInitialMonth] = useState(moment("01-01-2021"));

  const handleDatesChange = ({ startDate, endDate }) => {
    setStartDate(moment(startDate, "MMM DD, YYYY"));
    setEndDate(moment(endDate, "MMM DD, YYYY"));
  };

  const handleMonthChange = () => {
    console.log("month change");
    setInitialMonth(moment("01-06-2021"));
  };
  return (
    <div>
      <DayPickerRangeController
        onDatesChange={handleDatesChange}
        focusedInput={focusedInput}
        startDate={startDate}
        endDate={endDate}
        numberOfMonths={2}
        initialVisibleMonth={() => initialMonth}
      />
      <button onClick={handleMonthChange}>Change Visible Month</button>
    </div>
  );
};

export default Date;

I found workaround solution.我找到了解决方法。 Unmounting component and then render with new initialMonth卸载组件,然后使用新的 initialMonth 渲染

import { useState } from "react";
import moment from "moment";
import { DayPickerRangeController } from "react-dates";
import "react-dates/initialize";
import "react-dates/lib/css/_datepicker.css";

const Date = (props) => {
  const [startDate, setStartDate] = useState(null);
  const [endDate, setEndDate] = useState(null);
  const [focusedInput, setFocusedInput] = useState("startDate");
  const [initialMonth, setInitialMonth] = useState(moment("01-01-2021"));

  const handleDatesChange = ({ startDate, endDate }) => {
    setStartDate(moment(startDate, "MMM DD, YYYY"));
    setEndDate(moment(endDate, "MMM DD, YYYY"));
  };

  const handleMonthChange = () => {
    setInitialMonth(null)
    setTimeout(() => setInitialMonth(moment("01-06-2021")), 0);
  };

  if(!initialMonth) return <div>Loading...</div>

  return (
    <div>
      <DayPickerRangeController
        onDatesChange={handleDatesChange}
        focusedInput={focusedInput}
        startDate={startDate}
        endDate={endDate}
        numberOfMonths={2}
        initialVisibleMonth={() => initialMonth}
      />
      <button onClick={handleMonthChange}>Change Visible Month</button>
    </div>
  );
};

export default Date;

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

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