javascript - React-Native 日历 : Pass date into custom function when a day is pressed

标签 javascript react-native calendar 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;

现在,我只是希望它在按下一天时提醒此测试函数中的日期字符串,所以我知道我可以用它来做我想做的事情。这是正在发生的事情:

enter image description here

最佳答案

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)}

它应该给你你想要的。

关于javascript - React-Native 日历 : Pass date into custom function when a day is pressed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54170268/

相关文章:

javascript - 单击时,切换 Font Awesome 备忘单值

javascript - 扩展 Ecma Script api

ios - CVCalendar DayView.date 错误

java - 如何更改 java.util.Calendar/Date 的 TIMEZONE

swift3 - 每周触发通知 Swift 3

javascript - 如何在 JavaScript/node.js 中删除 HTTP 响应 header 中的无效字符?

javascript - 第 1 行出现 JSON 解析错误,期望 '{' 、 '['

javascript - 我如何在 AngularJS 中创建和聚焦新元素?

javascript - React-native IOS MapView 覆盖不显示

android - 在 React Navigation 中如何更新状态 goBack