reactjs – Set Array Value in setState in react js
I want to add data in set state (setArraytime) but array data value is not reflecting in setstate.
const [vehicle_type, setVehicleType] = useState("");
const [endTime, setEndtime] = useState("");
const [startTime, setStartTime] = useState("");
const [array_time, SetArrayTime] = useState([]);
let value = {
interval: vehicle_type,
startTime: startTime,
endTime: endTime,
};
const doArrayChunks = () => {
var inputDataFormat = "HH:mm a";
var outputFormat = "HH:mm a";
var tmp = moment(vehicle_type, inputDataFormat);
var dif = tmp - moment().startOf("day");
var startIntervalTime = moment(startTime, inputDataFormat).add(-dif, "ms");
var endIntervalTime = moment(startTime, inputDataFormat);
var finishTime = moment(endTime, inputDataFormat);
var intervals = [];
while (startIntervalTime < finishTime) {
var format =
startIntervalTime.format(outputFormat) +
" - " +
endIntervalTime.format(outputFormat);
intervals.push(format);
startIntervalTime.add(dif, "ms");
endIntervalTime.add(dif, "ms");
console.log("Intervals", intervals);
SetArrayTime(intervals);
console.log("array_time", array_time);
}
};
Read more here: Source link