react-datepicker icon indicating copy to clipboard operation
react-datepicker copied to clipboard

When using date range in one component instead of end date I get null

Open SadBamsi opened this issue 2 years ago • 7 comments

My component

    const { startDate, endDate } = useSelector(selectFormData);

    const [localDate, setLocalDate] = useState({
        startDate: startDate,
        endDate: endDate,
    });
    
    const onChange = (dates: [Date | null, Date | null], event: SyntheticEvent<any, Event> | undefined) => {
        // const [startDate, endDate] = dates;
        console.log(dates);
    };

<DatePicker
      id="single-datepicker"
      selected={localDate.startDate}
      onChange={onChange}
      startDate={localDate.startDate}
      endDate={localDate.endDate}
      selectsRange
      inline
      minDate={new Date()}
      monthsShown={2}
/>

but in dates i have array with date as startDate and null as endDate

image

SadBamsi avatar Feb 11 '23 12:02 SadBamsi

I am facing the same issue. Please fix the bug asap.

parixitsoni avatar Feb 11 '23 19:02 parixitsoni

Same here

Robertoleanonsystems avatar Feb 16 '23 16:02 Robertoleanonsystems

Maybe, it is not a bug:

When there is a start date and end date selected and the user just click in a new date on the calendar, the behavior right now is to only delete the end date and set the date selected by the user as a new start date, so it just restart the selection of the date range

Robertoleanonsystems avatar Mar 02 '23 10:03 Robertoleanonsystems

I am facing the same issue. Please fix the bug asap.

mushan520 avatar Aug 01 '23 03:08 mushan520

@Robertoleanonsystems is right. You can "fix" this like so:

onChange={ ([from, to]: [Date, Date]): void => {
	if (!to) {
		setFrom(from)
		setTo(undefined)
	}
	else {
		setFrom(from)
		setTo(to)
	}
} }

InsOpDe avatar Aug 14 '23 22:08 InsOpDe

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 10 days.

github-actions[bot] avatar Jun 04 '24 01:06 github-actions[bot]

If you only do console.log() in onChange, it doesn't work. You'll need to set the startDate into a React state, and reflect it into <DatePicker startDate={startDate} />. Then you'll be able to choose the endDate and get both values in return.

ozooxo avatar Feb 27 '25 01:02 ozooxo

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 10 days.

github-actions[bot] avatar Oct 07 '25 02:10 github-actions[bot]

This issue was closed because it has been stalled for 10 days with no activity.

github-actions[bot] avatar Nov 14 '25 02:11 github-actions[bot]