gpt4 book ai didi

javascript - react native : Create Time List using FlatList and DatePicker

转载 作者:行者123 更新时间:2023-11-28 16:54:52 25 4
gpt4 key购买 nike

目标:当用户从日期选择器中选择时间时显示时间列表。

问题:它只显示单个时间而不是时间列表。

我可以从日期选择器中选择时间。当我单击确定时,所选时间将显示在平面列表上。但是,选择时间后,时间将被替换。这会导致列表中仅显示 1 次。

我在下面添加了应用程序的屏幕截图。

因此,任何有关解决此问题的帮助都将不胜感激。

代码片段如下:

import React, { Component } from 'react';
import { View, Text, StyleSheet, TouchableOpacity, ScrollView, FlatList } from 'react-native';
import DatePicker from '@react-native-community/datetimepicker';
import Feather from 'react-native-vector-icons/Feather';
import moment from 'moment';

export default class FrCreateScreen extends Component {

constructor(props) {
super(props);
this.state = {
//Time
appointmentTime: new Date(moment()),
modeAppointmentTime: 'time',
showAppointmentTime: false,
textAppointmentTime: moment().format('h:mm a').toString(),

//new timeSlots
timeSlots: [],
}
}

//[ timePicker start]
setAppointmentTime = (event, appointmentTime, textAppointmentTime, timeSlots) => {
appointmentTime = appointmentTime || this.state.appointmentTime;
textAppointmentTime = textAppointmentTime || moment(appointmentTime).format('h:mm a').toString();
this.setState({
showAppointmentTime: Platform.OS === 'ios' ? true : false,
appointmentTime,
textAppointmentTime,

//newly added
timeSlots: [{ time: textAppointmentTime }],
});
}

showTime = (modeAppointmentTime, textAppointmentTime, timeSlots) => {
textAppointmentTime = moment(this.state.appointmentTime).format('h:mm a').toString();
this.setState({
showAppointmentTime: true,
modeAppointmentTime,
textAppointmentTime,

//newly added
timeSlots: [{ time: textAppointmentTime }],
})
}

timePicker = () => {
this.showTime('time');
}
//[ timePicker end ]

getAppointmentTimePage() {
//const { timeSlots, selectedValue } = this.state;
const {
appointmentTime,
showAppointmentTime,
modeAppointmentTime,
textAppointmentTime
} = this.state;

return (
<View>
<Text style={[styles.title, { marginTop: 20 }]}>Select Appointment Time:</Text>
<View style={styles.container}>
{<TouchableOpacity onPress={this.timePicker}>
<Text style={styles.textDate}>
{textAppointmentTime}
</Text>
</TouchableOpacity>}
{showAppointmentTime && <DatePicker
value={appointmentTime}
mode={modeAppointmentTime}
onChange={this.setAppointmentTime}
minimumDate={new Date(moment())}
/>}
</View>
<TouchableOpacity style={styles.addContainer}>
<Text style={styles.addText}>Add Appointment</Text>
</TouchableOpacity>
</View>
);
}

render() {
return (
<ScrollView>
{this.getAppointmentTimePage()}
<TouchableOpacity>
<View style={styles.row}>
<FlatList
data={this.state.timeSlots}
keyExtractor={(times) => times.time}
renderItem={({ item }) => {
return (
<View>
<Text style={styles.textTime}>{item.time}</Text>
<TouchableOpacity>
<Feather name="trash" style={styles.icon} />
</TouchableOpacity>
</View>
);
}}
/>
</View>
</TouchableOpacity>
</ScrollView>
)
};
};

enter image description here

最佳答案

您需要做的唯一更改是在数组中追加项目,而不是更新数组对象。

timeSlots: [...this.state.timeSlots,{ time: textAppointmentTime }],

错误在于您将数组视为对象。

关于javascript - react native : Create Time List using FlatList and DatePicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59407616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com