question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot Clear Dates on DayPickerInput in latest version (7.4.0^)

See original GitHub issue

Trying to set dates to undefined to clear, causes the below error A component is changing a controlled input of type undefined to be uncontrolled. Input elements should not switch from controlled to uncontrolled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component.

import * as React from 'react';
import moment from 'moment';
import { formatDate, parseDate } from 'react-day-picker/moment';
import DayPickerInput from 'react-day-picker/DayPickerInput';
class DayPickerTest extends React.Component<Props> {

  constructor(props) {
    super(props);
    this.handleFromChange = this.handleFromChange.bind(this);
    this.handleToChange = this.handleToChange.bind(this);
    this.state = {
      from: undefined,
      to: undefined,
    };
  }

  showFromMonth() {
    const { from, to } = this.state;
    if (!from) {
      return;
    }
    if (moment(to).diff(moment(from), 'months') < 2) {
      this.to.getDayPicker().showMonth(from);
    }
  }

  handleFromChange(from) {
    // Change the from date and focus the "to" input field
    this.setState({ from });
  }

  handleToChange(to) {
    this.setState({ to }, this.showFromMonth);
  }

  clearDates = () => {
    this.setState({
      from: undefined,
      to: undefined,
    });
  }

  render() {
    const { from, to } = this.state;
    const modifiers = { start: from, end: to };
    return (
      <div className="InputFromTo">
        <div className="test"
          onClick={this.clearDates}
        />
        <DayPickerInput
          value={from}
          placeholder="From"
          format="LL"
          formatDate={formatDate}
          parseDate={parseDate}
          dayPickerProps={{
            selectedDays: [from, { from, to }],
            disabledDays: { after: to },
            toMonth: to,
            modifiers,
            numberOfMonths: 2,
            onDayClick: () => this.to.getInput().focus(),
          }}
          onDayChange={this.handleFromChange}
        />{' '}
        —{' '}
        <span className="InputFromTo-to">
          <DayPickerInput
            ref={el => (this.to = el)}
            value={to}
            placeholder="To"
            format="LL"
            formatDate={formatDate}
            parseDate={parseDate}
            dayPickerProps={{
              selectedDays: [from, { from, to }],
              disabledDays: { before: from },
              modifiers,
              month: from,
              fromMonth: from,
              numberOfMonths: 2,
            }}
            onDayChange={this.handleToChange}
          />
        </span>
      </div>
    );
  }
}

export default DayPickerTest;
`

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:10
  • Comments:6

github_iconTop GitHub Comments

2reactions
rstrandcommented, Jul 14, 2020

@redaxmedia Interestingly Douglas Crockford himself stopped using null and only uses undefined with the rationale that undefined is already used a lot internally in javascript.

2reactions
henryruhscommented, Apr 21, 2020

Works for me with 7.4.8 … thanks for the fix

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-day-picker/CHANGELOG.md - UNPKG
120, - (DayPickerInput) Fixed: overlay did not reset the displayed month when appearing again ... 147, > This should be the last minor...
Read more >
Unable to use DayPickerInput from React-Day-Picker with ...
My impression is that the TypeScript definitions are not up to date, and someone (you or me?) needs to update them? :) Share....
Read more >
Input Fields - React DayPicker
It is a common scenario to bind the date picker with an input field. ... const popper = usePopper(popperRef.current, popperElement, {
Read more >
react-day-picker - Bountysource
Customizable date picker and calendar component for React.js. Become a Bounty Hunter ... We can't release it if we don't have a good...
Read more >
react-day-picker - npm
Customizable Date Picker for React. Latest version: 8.4.1, last published: 9 days ago. Start using react-day-picker in your project by ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found