0

Objective: To show a list of Time when the user selects Time from the DatePicker.

Problem: It only shows a single Time instead of list of times.

I can select time from the Datepicker. And when I clicked ok, the Selected time is shown on the flatlist. However upon selecting a new time, the old time gets replaced. This results in showing only 1 time on the list.

I have added screenshot of the app below.

Hence, any kind of help regarding solving this problem will be appreciated.

Code snippet below:

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

2
  • I think on click of AddAppointment you want to push the time slot to the array. So instead of doing timeSlots: [{ time: textAppointmentTime }] , you might want to do timeSlots: this.state.timeSlots.push({ time: textAppointmentTime }). Also I cannot see the code for Add Appointment button in the attached snippet. Commented Dec 19, 2019 at 10:43
  • I have the added the Add Appointment button. Initially it was there just for show and didn't contain any function. Regarding the line of code that you provided, I am getting this warning: Possible Unhandled Promise Rejection (id: 0): TypeError: _this.state.timeSlots.push is not a function Commented Dec 19, 2019 at 11:01

1 Answer 1

3

The only change you have to do is append item in array instead of update array object.

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

Mistake was that you were treating array as object.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. It solved my problem as the app lists all the selected time properly. I am a bit weak regarding objects and array and I am still learning at the moment. Once again, Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.