Я пытаюсь вытолкнуть данные из поля TextInput и созданную временную метку в базу данных реального времени Firebase. Однако, когда приложение загружено, первый щелчок по кнопке «отправить» отправляет только textInput, а не «отметку времени»
Это простая версия файла,
import React, { Component } from 'react';
import { View, Text, TouchableHighlight, StyleSheet,
ImageBackground, TextInput, Alert, Picker, TouchableWithoutFeedback,
Keyboard } from 'react-native';
import React, { Component } from 'react';
import { View, Text, TouchableHighlight, StyleSheet,
ImageBackground, TextInput, Alert, Picker, TouchableWithoutFeedback,
Keyboard } from 'react-native';
import { db } from '../config';
let date = new Date().getDate(); //Current Date
let month = new Date().getMonth() + 1; //Current Month
let year = new Date().getFullYear(); //Current Year
let hours = new Date().getHours(); //Current Hours
let min = new Date().getMinutes(); //Current Minutes
let sec = new Date().getSeconds(); //Current Seconds
let currentTimeAndDate
const updateDatabase = item => {
db.ref('/Database/...').push({
Amount: item,
time: currentTimeAndDate
});
};
export default class AddItem extends Component {
constructor(props) {
super (props);
this.state = {
pickerSelection: "default value!",
date: " "
};
}
handleSubmit = () => {
this.setState({
//Setting the value of the date time
date:
date + '/' + month + '/' + year + ' ' + hours + ':' + min +
':' + sec,
});
currentTimeAndDate = this.state.date
if (this.state.pickerSelection === "WHATEVER") {
updateDatabase(this.state.textInputField);
Alert.alert("database updated!");
//This is a short version of the View:
render() {
return (
<View>
<TouchableHighlight
onPress={TextInput.CheckTextInputIsEmptyOrNot}
onPress={this.handleSubmit}
>
<Text style={styles.buttonText}>Update</Text>
</TouchableHighlight>
</View>
);
}
Я уже пытался отобразить currentTimeAndDate в Text после нажатия TouchableHighLight. Он не показывает первый щелчок мышью после загрузки приложения, но показывает второй раз.
в firebase данные вводятся так при первом нажатии:
EJDPvodisjpINJI_9
amount: "(input number fx. 10)"
time: " "
Второй раз нажал:
Efvpspodvpoep_10
amount: "(input number fx. "10")"
time: "(the local time of click fx. "8/5/2019 19:28:28")"
Что я могу сделать, чтобы он отправлял местное время в первый раз?