简体   繁体   English

在 react-date-range 中识别选定的日期范围

[英]Identify selected date range in react-date-range

I am using react-date-range plugin to select a date range.我正在使用 react-date-range 插件来选择日期范围。

import { DateRangePicker} from 'react-date-range';
import 'react-date-range/dist/styles.css';
import 'react-date-range/dist/theme/default.css';

在此处输入图片说明

Following function is used to handle the date range when a range is selected以下函数用于在选择范围时处理日期范围

    function handleSelect() {
            
    }

I have added the DateRangePicker like this我已经像这样添加了 DateRangePicker

const selectionRange = {
    startDate: new Date(),
    endDate: new Date(),
    key: 'selection',
}

<div>
    <DateRangePicker
    ranges={[selectionRange]}
    onChange={handleSelect}/>
</div>

I want to make some operations on the selected date range in side the handleChange function.我想在 handleChange 函数旁边的选定日期范围内进行一些操作。 But I cannot find how to identify which date range is selected and the respective start and end dates.但我找不到如何确定选择了哪个日期范围以及各自的开始和结束日期。

In documentation onChange callback is described as在文档 onChange 回调被描述为在此处输入图片说明

But I cannot understand how to get these startDate and endDate parameters.但我无法理解如何获取这些 startDate 和 endDate 参数。 Can someone help me to get this values.有人能帮我得到这个值吗?

Documentation: https://openbase.io/js/react-date-range/documentation文档: https : //openbase.io/js/react-date-range/documentation

According to the documentation the onChange callback receives the ranges as an argument:根据文档onChange回调接收ranges作为参数:

import { DateRangePicker } from 'react-date-range';

class MyComponent extends Component {
  handleSelect(ranges){
    console.log(ranges);
    // {
    //   selection: {
    //     startDate: [native Date Object],
    //     endDate: [native Date Object],
    //   }
    // }
  }
  render(){
    const selectionRange = {
      startDate: new Date(),
      endDate: new Date(),
      key: 'selection',
    }
    return (
      <DateRangePicker
        ranges={[selectionRange]}
        onChange={this.handleSelect}
      />
    )
  }
}

It should be an object that has a key selection which is itself an object that contains the startDate and endDate .它应该是一个具有键selection的对象,该对象本身就是一个包含startDateendDate的对象。

My solution is:我的解决办法是:

const onChange = (item) => {
    if (item.selection.endDate !== item.selection.startDate) {
        console.log(item);
        setIsPopoverOpen(false);
    }
    setRange([item.selection]);
}

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

相关问题 如何将按钮添加到反应日期范围 - How to add a button to react-date-range 反应日期范围创建自定义输入范围 - react-date-range create custom inputRanges 使用 react-date-range 进行日期选择的月份跳跃 - Month jumping on date selection using react-date-range 如何更改 react-date-range 包中选择的输入颜色? - How I can change color of input selected in react-date-range package? 如何在 React 中使用 react-date-range 发布请求日期? - How to post request dates with react-date-range in React? 如何在 react-date-range 中的日期范围 select 之后自动关闭日期范围模式 - How to auto close date range modal after date range select in react-date-range 如何使用webpack模块捆绑器实现react-date-range picker? - How to implement react-date-range picker with webpack module bundler? 单击 react-date-range 定义的范围使用回调 - Using a callback on click of react-date-range defined ranges 加载页面时,如何在默认情况下在 react-date-range npm 中在静态范围内显示选定的过去 30 天的值 - How to show selected last 30 days value in static range by default in react-date-range npm when page is load 仅选择开始日期或结束日期时如何处理反应日期范围(日期范围)的 onChange 事件 - How to handel event onChange of react-date-range (Date range) when only select start date or end date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM