javascript - React-Native Calendars:按下一天时将日期传递给自定义函数
问题描述
使用 React-Native Calendars 中的日历,我只想在按下一天时将日期(可能作为 dateString)传递给函数。
这是我的代码:
import React, {Component} from 'react';
import {View, Text, Button, ScrollView} from 'react-native';
import colors from '../config/colors';
import { TextInput } from '../components/TextInput';
import { Calendar } from 'react-native-calendars';
class SetReservation extends Component {
showDayTest(date) {
alert(date);
}
render() {
return (
<View>
<Calendar
onDayPress={(dateString) => this.showDayTest(dateString)}
/>
</View>
);
}
}
export default SetReservation;
现在,我只想让它在按下一天时在这个测试函数中提醒一个 dateString,所以我知道我可以用它来做我想做的事。这是正在发生的事情:
解决方案
onDayPress 函数在其回调中返回一个对象。这就是为什么当你提醒它时你会得到 [object Object]。它返回的对象应该看起来像
{
day: 1, // day of month (1-31)
month: 1, // month of year (1-12)
year: 2017, // year
timestamp, // UTC timestamp representing 00:00 AM of this date
dateString: '2016-05-13' // date formatted as 'YYYY-MM-DD' string
}
如果您将 dateString 包装在 JSON.stringify 中,那么您应该会在警报中看到完整的对象
onDayPress={(dateString) => this.showDayTest(JSON.stringify(dateString))}
但是,如果您解构返回的对象并执行此操作
onDayPress={({dateString}) => this.showDayTest(dateString)}
它应该给你你想要的。
推荐阅读
- c++ - 如何重载 [] 运算符以支持其他函数中的赋值和 const 参数?
- python - Tkinter 通过检查 Checkbuttons 运行多个功能
- node.js - REST API、删除请求、CORS 规范或查询错误?
- javascript - 如何使用 codeigniter 输入多行
- html - Change text of label on changing of select box option using jquery?
- python-3.x - 从字典中访问信息
- r - 如何根据其他行中的值填充 R 数据框中的一行中的值
- spring-cloud - Spring Cloud Contract:在 Contract DSL 中导入重复部分的首选方式
- c++ - 如何检查程序过去是否创建过文件
- javascript - Excel 加载项 | 使用功能区实现登录流程