我试图节省调用函数start的时间,但是在调用OneSignal.initWithLaunchOptions(launchOptions, appId: "YOUR ONE SIGNAL APP ID HERE", handleNotificationAction: { (result) in
let payload = result?.notification.payload
if let additionalData = payload?.additionalData {
let destination = additionalData["destination"] as? String ?? ""
print("the destination is: \(destination)")
}
},settings: onesignalInitSettings)
之后,setStartTS()
仍然为空。我不确定requestAnimationFrame是否在此处引入任何类型的错误,以及有关问题可能在何处的任何想法
startTS
答案 0 :(得分:2)
React的钩子提供了一个名为useEffect的api。安装组件时,可能需要此函数来调用start()
函数。
const Chrono = () => {
const [startTS, setStartTS] = useState(null);
const [diff, setDiff] = useState(null);
const [intervalId, setIntervalId] = useState(null);
// --> add effect
useEffect(() => {
start();
});
const start = () => {
if (startTS) {
return;
}
const tic = +new Date();
setStartTS(tic);
setIntervalId(requestAnimationFrame(tick));
};
const tick = () => {
const tock = +new Date();
const timePassed = tock - startTS;
setDiff(timePassed);
setIntervalId(requestAnimationFrame(tick));
};
return (...)
}
有关useEffect api的更多详细信息,请查看here