xgfe / react-native-datepicker

react native datePicker component for both Android and IOS, useing DatePikcerAndroid, TimePickerAndroid and DatePickerIOS
MIT License
2.12k stars 726 forks source link

onDateChange callback is made with old date value #335

Open jleon6626 opened 5 years ago

jleon6626 commented 5 years ago

Issue

onDateChange callback is made with old date value. In datepicker.js "onDatePicked", the state is set with setState, then the callback occurs immediately after, but the date value in setState isn't guaranteed to be set until next redraw so the current value of state.date is sent in the callback.

Expected Behavior

correct new date value passed into the onDateChange callback

Code


onDatePicked({action, year, month, day}) {
      this.setState({
        date: new Date(year, month, day)  // <= new state,date isn't guaranteed to be ready!!
      });
      this.datePicked();  // <= callback made here

// I kludge fix it by making my own datePicked function and passing in the new date to that
 onDatePicked({action, year, month, day}) {
    if (action !== DatePickerAndroid.dismissedAction) {
      this.setState({
        date: new Date(year, month, day)
      });
    //  this.datePicked();
      this.datePickedKludge( new Date(year, month, day) );
    } else {
      this.onPressCancel();
    }
  }

Environment

  1. react-native -v: 16.8.3
  2. node -v: v10.15.3
  3. npm -v:
  4. yarn --version:
  5. target platform: Android
  6. operating system: Win10
penghaogit commented 5 years ago

this.datePickedKludge ??What is it?

kelement commented 5 years ago

we will change this.datePicked to setState's callback, guaranteed state is new state