henninghall / react-native-date-picker

React Native Date Picker is datetime picker for Android and iOS. It includes date, time and datetime picker modes. The datepicker is customizable and is supporting different languages. It's written with native code to achieve the best possible look, feel and performance.
MIT License
2.14k stars 326 forks source link

state not update when I setState in onPress function #788

Closed xclidongbo closed 3 months ago

xclidongbo commented 3 months ago

Describe the bug state not update when I setState({date, minimumDate}) in onPress function.

Expected behavior state cat update when I setState({date, minimumDate}) in onPress function

To Reproduce

const index = (props) => {

  const date = new Date();
  const [currentDate, setCurrentDate] = useState(date);
  const [minimumDate, setMinimumDate] = useState(date);

  state = { date: new Date() }

  render = () =>
           <DatePicker
          visible={isVisible}
          date={currentDate}
          minimumDate={minimumDate}
          modalTitle={datePickerTitle}
          onTouchOutside={onTouchOutside}
          onCommit={(date) => {
            const tmpDate = moment(date).format('YYYY-MM-DD hh:mm:ss');
            console.log(tmpDate);
            if (datePickerTitle === startTimeDesc) {
              console.log('开始');
              setStartDesc(tmpDate);
            } else {
              console.log('结束');
              setEndDesc(tmpDate);
            }
            setIsVisible(false);
          }}
        />

  const onPressDatePicker = (title) => {
    const currentTime = new Date();
    const nearestTime = nearestMinuteTime(currentTime);
    const afterTime = afterHours(nearestTime);

    setCurrentDate(nearestTime);
    setMinimumDate(afterTime);
}

Smartphone (please complete the following information):

henninghall commented 3 months ago

hooks in a class component will cause issues

xclidongbo commented 3 months ago

hooks in a class component will cause issues

not class component, is function component. just a mistake.

xclidongbo commented 3 months ago

@henninghall
Can I update state with date & minimumDate on existing date picker ?

henninghall commented 3 months ago

@henninghall Can I update state with date & minimumDate on existing date picker ?

Yes, that is intended