I am trying to use function components in which eventoffset value is initially set to -1. when user clicks "ok" to save the values in backend func saveEvent is called and in that calOffsetUTC is calculating the new offset and utc values which set the state values accordingly. I am trying to call addTimeline() with new offset and utc values but it seems state values are not updated.
where am i going wrong.
Any help appreciated.
const Dialog = (props: Props) => {
let currentdate = new Date(Date.now());
const [eventDate, seteventDate] = React.useState(currentdate);
const [eventOffset, seteventOffset] = React.useState(-1);
const [eventUTC, seteventUTC] = React.useState();
const calcOffsetUTC = (date) => {
var selectedDate = date;
if (eventOffset == -1) {
var offset = selectedDate.getTimezoneOffset() * 60;
var utc = selectedDate.valueOf();
seteventOffset(offset); //trying to set state of "eventoffset"
seteventTime(utc);
}
}
const saveEvent = () => {
calcOffsetUTC(eventDate);
props.addTimeline(eventUTC, eventOffset); //latest eventoffset value is not found here.
}
const buttons = [];
buttons.push({ text: translate("ok"), icon: "ok", callback: () => saveEvent() });
return <Dialog buttons={buttons} ></Dialog>;
}