0

I was creating a custom materiel UI component for DayPickerInput, but the problem is when onDayChange is fired,it gives an error.

handleToDateChange = (selectedDay, modifiers, dayPickerInput) => {
    const val = dayPickerInput.getInput().value;
}

Uncaught TypeError: Cannot read property 'value' of null

DayPickerInput Component

 <DayPickerInput
      component={props => <DatePickerCustomInput {...props} />}
      value={toDate}
      placeholder=""
      formatDate={this.setDatePickerformatDate}
      onDayChange={this.handleToDateChange}
  />

DayPickerInput Custom Component

class DatePickerCustomInput extends Component {
   render() {
       return (
           <TextField {...this.props}
               InputProps={{
                   startAdornment: (
                       <InputAdornment position="start">
                           <DateRange />
                       </InputAdornment>
                   ),
               }}

               value={this.props.value}
           />
       );
   }
}

to reproduce the issue : https://codesandbox.io/s/387r9plx65

wahdan
  • 1,208
  • 3
  • 16
  • 26

2 Answers2

0

Try do this!

handleToDateChange = (selectedDay, modifiers, dayPickerInput) => {
    const val = dayPickerInput.state.value;
}
seunggabi
  • 1,699
  • 12
  • 12
0

From the docs here. The onDayChange method has a signature of

onDayChange (day: date, modifiers: Object, e: SyntheticEvent) ⇒ void

day is a date object referring to the selected date in the picker. You don't need to pull the value out of the event to get the selected date.

kgstew
  • 472
  • 4
  • 12