[英]ReactJS useState not updating momentJS prop
I am coding a CRM app and there's a asnyc function for gettingTrackers
and it working well.我正在编写一个 CRM 应用程序,并且有一个 asnyc function 用于获取
gettingTrackers
,并且运行良好。 There's another function called calculateStartTime
and this function suppose to calculate momentJS
variable and set it but this is not updating.还有另一个 function 称为
calculateStartTime
和这个 function 假设计算momentJS
变量并设置它,但这不是更新。
useEffect(() => {
async function gettingTrackers() {
await getTrackers(null, null);
}
gettingTrackers();
calculateStartTime();
}, []);
const [startTime, setStartTime] = useState(moment().format("YYYY-MM-DD"));
const [endTime, setEndTime] = useState(moment().format("YYYY-MM-DD"));
const calculateStartTime = () => {
const dateOfMay = moment("2020-05-01");
const now = moment();
let timeTheStart = null;
let timeTheEnd = null;
if (now.add(-32, "d").diff(dateOfMay) <= 0) {
timeTheStart = dateOfMay.format("YYYY-MM-DD")
timeTheEnd = moment().add(-2, "d").format("YYYY-MM-DD");
} else {
timeTheStart = moment().add(-32, "d").format("YYYY-MM-DD");
timeTheEnd = moment().add(-2, "d").format("YYYY-MM-DD");
}
console.log("calculating...")
console.log("start time > ", timeTheStart)
console.log("end time > ", timeTheEnd);
setStartTime(moment(timeTheStart).format("YYYY-MM-DD"))
setEndTime(moment(timeTheEnd).format("YYYY-MM-DD"))
// these 2 logs prints initial value, not updated value.
console.log(startTime);
console.log(endTime)
}
The problem is that I have to send startTime
and endTime
to another ReactJS component, and it sends first initial today value every time.问题是我必须将
startTime
和endTime
发送到另一个 ReactJS 组件,并且它每次都发送第一个初始今天值。 When I call calculateStartTime
it logs当我调用
calculateStartTime
它记录
calculating...
start time > 2020-06-07
end time > 2020-07-07
But when I click to button for another component, I print these variables and its output;但是当我单击另一个组件的按钮时,我会打印这些变量及其 output;
2020-07-09
2020-07-09
as initial values.作为初始值。 I log them with using
startTime
and endTime
as I described in useState
我使用
startTime
和endTime
记录它们,正如我在useState
中描述的那样
What I am missing on this problem?我在这个问题上缺少什么? Is there any memory-leak to not-working?
不工作是否有任何内存泄漏?
Edit:编辑:
const goToResultButton = (event, data) => {
event.preventDefault();
console.log("start time > ", startTime)
console.log("end time > ", endTime)
}
Thanks in advance!提前致谢!
With the below code block, you are setting the state and immediately you are trying to access the updated value, but state updates done in async fashion.使用下面的代码块,您正在设置 state 并立即尝试访问更新的值,但 state 更新以异步方式完成。 You will get the latest value in the next re-render.
您将在下一次重新渲染中获得最新值。
...
setStartTime(moment(timeTheStart).format("YYYY-MM-DD"))
setEndTime(moment(timeTheEnd).format("YYYY-MM-DD"))
// these 2 logs prints initial value, not updated value.
console.log(startTime);
console.log(endTime)
...
You can use a useEffect
to log or do something with latest values of startTime and endTime.您可以使用
useEffect
记录或使用 startTime 和 endTime 的最新值执行某些操作。
useEffect(() => {
console.log("startTime", startTime);
console.log("endTime", endTime);
}, [startTime, endTime]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.