简体   繁体   English

@react-native-community/datetimepicker 显示弹出选择器 2 次

[英]@react-native-community/datetimepicker shows popup-picker 2 times

I'm using @react-native-community/datetimepicker in my app.我在我的应用程序中使用@react-native-community/datetimepicker。 This is supposed to collect a user-defined date from the datepicker and then send it to the database.这应该从日期选择器收集用户定义的日期,然后将其发送到数据库。 But, when I press a button and open the datepicker, the datepicker popup shows up two times.但是,当我按下按钮并打开日期选择器时,日期选择器弹出窗口会显示两次。 I pick a date, press ok and the window closes.我选择一个日期,按确定,window 关闭。 And then only some ms after, a new date picker opens up.然后仅仅几毫秒之后,一个新的日期选择器打开了。 Anyone know why this happens?有谁知道为什么会这样?

const onChange = (event, selectedDate) => {
        var inputDate = selectedDate.toISOString()
        var outputDate = inputDate.split("T")[0]
        setSelectedDate(outputDate)
        setDate(date)
        setShow(false);
        console.log(outputDate)
    }

const showDatepicker = () => {
        setShow(true)
    }

...

<View>
   <Button title="Choose date" onPress={showDatepicker} />
      </View>
      {show ? (
         <DateTimePicker
            value={date}
            onChange={onChange}
            mode={'date'}
            display="default"
         />
      ) : (<View />)}

Try below code that might help试试下面可能有帮助的代码

const onChange = (event, selectedDate) => {
    const inputDate = selectedDate.toISOString();
    const outputDate = inputDate.split('T')[0];
    setSelectedDate(outputDate);
    setDate(outputDate);
  };

  React.useEffect(() => {
    setShow(false);
  }, [date]);

The above answer did not work for me (Android version).上面的答案对我不起作用(Android版本)。 What worked for me was: Try setting the show to false on the onChange but make sure to call this BEFORE you set the values.对我有用的是:尝试在 onChange 上将 show 设置为 false,但请确保在设置值之前调用它。 I fixed this for Date mode by following this answer from: https://github.com/react-native-datetimepicker/datetimepicker/issues/54我按照以下答案为日期模式修复了此问题: https://github.com/react-native-datetimepicker/datetimepicker/issues/54

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 react-native-community 中的 datetimepicker - How to use datetimepicker from react-native-community 无法解决“@react-native-community/masked-view - Unable to resolve "@react-native-community/masked-view 使用 expo 和 @react-native-community/async-storage - using expo and @react-native-community/async-storage 如何通过@react-native-community/slider 在 android 中创建垂直滑块? - how to create vertical slider in android by @react-native-community/slider? React Native,更改 react-native-community/slider 和拇指图标的高度 - React Native, change the height of react-native-community/slider and thumb icon 使用 @react-native-community/blur 和 react-native 0.66.3 在 android 上崩溃 - using @react-native-community/blur with react-native 0.66.3 crash on android 错误:无法解析模块`@react-native-community/toolbar-android` - Error: Unable to resolve module `@react-native-community/toolbar-android` 我已经安装了 @react-native-community/async-storage 但导入它时出错 - I have installed @react-native-community/async-storage but there is an error importing it 尝试使用@react-native-community/slider 时,在 UI 管理器中找不到 RNCSlider - RNCSlider was not found in the UI Manager when trying to use @react-native-community/slider 如何设置 Atom 编辑器以仅使用 react-native-community eslint-config? - How to setup Atom editor to use only the react-native-community eslint-config?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM