简体   繁体   English

无法在回调内部调用React Hook“ useInterval”

[英]React Hook “useInterval” cannot be called inside a callback

I'm trying to setup a progress bar for a backend action that can take a while to complete. 我正在尝试为可能需要一段时间才能完成的后端操作设置进度条。

  1. User click button to export a file 用户单击按钮以导出文件
  2. API response has a token for the backend process API响应具有后端流程的令牌
  3. With useInterval, I check every half a second the progress % (with another endpoint, sending the token) to update my progress bar component 使用useInterval,我每隔半秒检查进度%(使用另一个端点,发送令牌)以更新进度条组件

I simplified the code in Codesandbox to reproduce the problem, in my Codesandbox, when clicking the button, I run useInterval to get a new random cat picture every 4 seconds and update my hook. 我简化了Codesandbox中的代码以重现此问题,在Codesandbox中,单击按钮时,我运行useInterval每隔4秒获取一张新的随机猫图片并更新我的钩子。

Codesandbox Codesandbox

As I understand, the problem is because I'm calling a Hook inside a custom Hook (useInterval), but I don't know another way to do this because native setInterval does not work with Hooks. 据我了解,问题是因为我在自定义的Hook(useInterval)中调用了Hook,但是我不知道另一种方法,因为本机setInterval无法与Hook一起使用。

I used the use-interval package 我使用了use-interval

You don't have to wrap useInterval with useEffect . 您不必包装useIntervaluseEffect Internally useInterval calls useEffect to do it work. 内部 useInterval调用useEffect可以正常工作。

And you can change values passed to useInterval in component and new values will be applied correctly. 您可以更改组件中传递给useInterval值,新值将正确应用。 You can change delay or callback arguments and useInterval will apply them. 您可以更改延迟或回调参数,并且useInterval将应用它们。

To showcase, I've added stop button, which stops retrieving cats. 为了展示,我添加了停止按钮,该按钮停止检索猫。

Here is working solution with cats :-) 这是猫的工作解决方案 :-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不能在回调中调用 React Hook “useLocation” - React Hook "useLocation" cannot be called inside a callback 无法在回调内调用 React Hook“useCustomFunction” - React Hook "useCustomFunction" cannot be called inside a callback 无法在回调中调用 React Hook "useCategory" - React Hook "useCategory" cannot be called inside a callback 不能在回调中调用 React Hook “useState” - React Hook “useState” cannot be called inside a callback 如何禁用 React Hook “useCallback”不能在回调警告中调用? - How to disable React Hook “useCallback” cannot be called inside a callback warning? 错误:无法在回调中调用 React Hook“useLogout”。 阿波罗 - Error: React Hook "useLogout" cannot be called inside a callback. Apollo 如何使用react修复错误“react use hook cannot be called inside a callback function”? - How to fix the error "react use hook cannot be called inside a callback function" using react? React Hook "useState" 不能在回调中调用。 使用 useMediaQuery 响应式 JS 媒体查询 - React Hook "useState" cannot be called inside a callback. Using useMediaQuery responsive JS media query 为什么在 React 中设置状态时会出现错误“无法在回调中调用钩子”? - Why am I getting error “hook cannot be called inside a callback” when setting state in React? 咖喱函数中的反应钩子(创建 HOC)从 linter 返回错误'React Hook“useContext”不能在回调中调用' - React hooks inside a curry function (creating a HOC) returning an error from linter 'React Hook "useContext" cannot be called inside a callback'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM