简体   繁体   English

React Native 社区日期选择器:将选择日期设置为文本组件(React State Hooks)

[英]React Native Community Date Picker : Set Picked Date to Text Component (React State Hooks)

I have implemented to pick and view the date as following ,我已经实现了选择和查看日期如下,

const [pickDate, setPickDate] = useState(new Date());

const onChange = (event, selectedDate) => {
     const currentDate = selectedDate || date;
     setShow(Platform.OS === 'ios');
     setDate(currentDate);
     setPickDate(selectedDate);
   };

<View> 
    <View>
       <Button onPress={showDatepicker} title="Show date picker!" />
       </View>
       <View>
         <Button onPress={showTimepicker} title="Show time picker!" />
       </View>
       {show && (
         <DateTimePicker
           testID="dateTimePicker"
           value={date}
           mode={mode}
           is24Hour={true}
           display="default"
           onChange={onChange}
         />
       )}
       <Text>{pickDate}</Text>
    </View>
</View>

But I have got the following error when after the pick the date.但是在选择日期后出现以下错误。

在此处输入图片说明

Can anyone suggest the solution ?任何人都可以提出解决方案吗?

As per the official documentation, the date should be in the string format.根据官方文档,日期应该是字符串格式。

Like "2016-05-15" , what you seem to pass a Date object."2016-05-15" ,你似乎传递了一个 Date 对象。 const [pickDate, setPickDate] = useState(new Date());

Kindly replace date object with the string, that will surely work.请用字符串替换日期对象,这肯定会起作用。

Ref: https://github.com/xgfe/react-native-datepicker参考: https : //github.com/xgfe/react-native-datepicker

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM