如何将 React Native 钩子调用移动到钩子?

问题描述 投票:0回答:1

这就是我的文件中的内容:

  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,这里无法解决;)谢谢你的帮助;)

javascript react-native react-hooks
1个回答
0
投票

不需要调用refreshProgress。当调用 useRefreshProgress 时,它会被 useEffect hook 自动调用。

© www.soinside.com 2019 - 2024. All rights reserved.