This is what I simply have in my file:
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]);
and now I would like to move it to another hook to utils
file like this:
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 };
};
and trying to use it like this:
const { refreshProgress } = useRefreshProgress(isInProgress);
refreshProgress(); // This expression is not callable. Type 'void' has no call signatures.
I just started learning react native and cannot solve it here;) Thank you for your help;)
2
Answers
Don’t need to call refreshProgress. It will be called automatically by useEffect hook when calling useRefreshProgress.
In addition to Jonas Beck’s answer, I would also refactor your hook further:
useEffect
bail out early before creating the interval ifisInProgress
isfalse
.time
state, because it might be usable.Code: