Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

I have the code const [startTime, setStartTime] = useState(""); const [endTime,…

  1. User

    I have the code const [startTime, setStartTime] = useState(""); const [endTime, setEndTime] = useState(""); const onChangeTime = (date, dateString) => { console.log(date, dateString); setStartTime(dateString[0]); setEndTime(dateString[1]); };

    return ( <Row className="section-modal" style={{ marginTop: "1rem" }}> <Col span={24} style={{ width: "90%", paddingLeft: "2rem", paddingRight: "2rem", }} > HORARIOS DE USO:

    									<div className="time_days">
    										<p>Lunes:</p>
    										<TimePicker.RangePicker
    											format="HH:mm"
    											onChange={onChangeTime}
    										/>
    									</div>
    									<div className="time_days">
    										<p>Martes:</p>
    										<TimePicker.RangePicker
    											format="HH:mm"
    											onChange={onChangeTime}
    										/>
    									</div>
    									<div className="time_days">
    										<p>Miércoles:</p>
    										<TimePicker.RangePicker
    											format="HH:mm"
    											onChange={onChangeTime}
    										/>
    									</div>
    									<div className="time_days">
    										<p>Jueves:</p>
    										<TimePicker.RangePicker
    											format="HH:mm"
    											onChange={onChangeTime}
    										/>
    									</div>
    									<div className="time_days">
    										<p>Viernes:</p>
    										<TimePicker.RangePicker
    											format="HH:mm"
    											onChange={onChangeTime}
    										/>
    									</div>
    									<div className="time_days">
    										<p>Sábado:</p>
    										<TimePicker.RangePicker
    											format="HH:mm"
    											onChange={onChangeTime}
    										/>
    									</div>
    									<div className="time_days">
    										<p>Domingo:</p>
    										<TimePicker.RangePicker
    											format="HH:mm"
    											onChange={onChangeTime}
    										/>
    									</div>
    								</Col>
    							</Row>
    

    ) modify it so I have a function that handles all of the days start and end times without having to write a function for each day in react, components are from antd

  2. Assistant

    You can store each day's start and end times in a single state object and use a loop to generate the TimePicker components for each day. Here's a modified example:

    import React, { useState } from "react";
    import { Row, Col, TimePicker } from "antd";
    
    const TimePickerExample = () => {
      const [time, setTime] = useState({
        Monday: { startTime: "", endTime: "" },
        Tuesday: { startTime: "", endTime: "" },
        Wednesday: { startTime: "", endTime: "" },
        Thursday: { startTime: "", endTime: "" },
        Friday: { startTime: "", endTime: "" },
        Saturday: { startTime: "", endTime: "" },
        Sunday: { startTime: "", endTime: "" },
      });
    
      const onChangeTime = (day) => (date, dateString) => {
        setTime((prevTime) => ({
          ...prevTime,
          [day]: { startTime: dateString[0], endTime: dateString[1] },
        }));
      };
    
      const days = [
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday",
        "Sunday",
      ];
    
      return (
        <Row className="section-modal" style={{ marginTop: "1rem" }}>
          <Col
            span={24}
            style={{
              width: "90%",
              paddingLeft: "2rem",
              paddingRight: "2rem",
            }}
          >
            <p>HORARIOS DE USO:</p>
    
            {days.map((day) => (
              <div key={day} className="time_days">
                <p>{day}:</p>
                <TimePicker.RangePicker
                  format="HH:mm"
                  onChange={onChangeTime(day)}
                />
              </div>
            ))}
          </Col>
        </Row>
      );
    };
    
    export default TimePickerExample;
    

    In this example, I created an array days containing the names of each day. Then, I mapped over the days array and generated a TimePicker.RangePicker component for each day. The onChangeTime function now takes the day as an argument, and returns another function that will properly update the state with the start and end times for that day.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.