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.
```/* eslint-disable */
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
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 (
export default DayPickerTest;
`
A work around people can use if they need to:
Pass a custom input component to DayPickerInput with the component property and have that input replace undefined with an empty string.
Works for me with 7.4.8 ... thanks for the fix
General react question: does it matter whether we use null or undefined to reset the component?
@himat I recommend to use null everywhere and ban undefined from your code. This approach comes from a talk by Douglas Crockford who pointed out that we don't need two types of "not set" and should stick to one of them.
@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.
Isn't it like this:
undefined - not set
null - set to nothing
I don't think you can really get rid of one or the other, they mean different things. If you get a model from API it's empty fields are set to null, for example.
Most helpful comment
@redaxmedia Interestingly Douglas Crockford himself stopped using
nulland only usesundefinedwith the rationale that undefined is already used a lot internally in javascript.