这就是我的文件中的内容:
const isInProgress = progress?.status === 'IN_PROGRESS' ?? false;
useEffect(() => {
const interval = setInterval(() => {
if (isInProgress) {
return setTime(Date.now());
} else {
return null;
}
}, 1000);
return () => {
clearInterval(interval);
};
}, [isInProgress]);
现在我想将其移动到另一个钩子到
utils
文件,如下所示:
export const useRefreshProgress = (isInProgress: boolean) => {
const [_, setTime] = useState(Date.now());
const refreshProgress = useEffect(() => {
const interval = setInterval(() => {
if (isInProgress) {
return setTime(Date.now());
} else {
return null;
}
}, 1000);
return () => {
clearInterval(interval);
};
}, [isInProgress]);
return { refreshProgress };
};
并尝试像这样使用它:
const { refreshProgress } = useRefreshProgress(isInProgress);
refreshProgress(); // This expression is not callable. Type 'void' has no call signatures.
我刚开始学习React Native,这里无法解决;)谢谢你的帮助;)
不需要调用refreshProgress。当调用 useRefreshProgress 时,它会被 useEffect hook 自动调用。