简体   繁体   English

React Hook useEffect 缺少依赖项:'execute'

[英]React Hook useEffect has a missing dependency: 'execute'

I can't seem to find an answer to this despite trying different things.尽管尝试了不同的事情,但我似乎无法找到答案。 I keep getting the error:我不断收到错误:

 Line 18:5:  React Hook useEffect has a missing dependency: 'execute'. Either include it or remove the dependency array  react-hooks/exhaustive-deps
printWarnings

on this file:在这个文件上:

import {useEffect, useState} from 'react'

export const createEffect = (fs, dependency) => {
    const[data, setData] = useState({})

    const execute = () => (
        fs().then(res => {
            setData(res)
        }).catch(err => {
            // do some magic
        })
    )

    useEffect(() => {
        execute()
        const interval = setInterval(execute, 15000)
        return(() => clearInterval(interval))
    }, [dependency])

    return(data)
}

The TLDR is I need to make a fetch request to a specific API function defined by fs() either every 15 seconds or when some global state changes, defined by dependency . TLDR 是我需要每 15 秒或当某些全局 state 更改时,由fs()定义的特定 API function 发出一个提取请求,由dependency项定义。 I also want to capture the data and any errors hence why I'm wrapping the fs() around with a couple of then() and catch() blocks.我还想捕获数据和任何错误,因此为什么我用几个then()catch()块包裹fs()

The following code gives me the warning at the top when I execute it inside my functional component:当我在功能组件中执行以下代码时,它会在顶部给出警告:

import createEffect from './components/createEffect'
~~~
let api = new api('http://localhost:8080/v1');
const[status, updateStatus] = useState(true);
const summary = createEffect(api.getSummary, status)

Any idea how I can fix this?知道如何解决这个问题吗? I'm new to React useEffect and not 100% if this is the right way to go about doing something like this?如果这是 go 做这样的事情的正确方法,我是 React useEffect 的新手,而不是 100%? Specifying execute as a dependency inside my useEffect call causes the app to constantly re-render.在我的 useEffect 调用中将execute指定为依赖项会导致应用程序不断地重新渲染。 I'm running execute() before calling setInterval() because I want the query to run every 15 seconds AND when the component first renders.我在调用setInterval() execute() ,因为我希望查询每 15 秒运行一次,并且在组件首次呈现时运行。

For more context: I'm making a dashboard as a sample project and want to query multiple api endpoints every 15 seconds or immediately when some global state changes.有关更多上下文:我正在制作仪表板作为示例项目,并希望每 15 秒或在某些全局 state 更改时立即查询多个 api 端点。 If the global state does change, I want to reset my HTTP polling from whatever time that is left back to 15 seconds.如果全局 state 确实发生了变化,我想将我的 HTTP 轮询从剩下的任何时间重置为 15 秒。

The linting error is very straight just says exactly what you need to do, either leaving the dependency array of useEffect as empty, or add the execute function into it. linting 错误非常直接,只是准确地说明了您需要做什么,要么将useEffect的依赖数组保留为空,要么将execute function 添加到其中。

Basically, because you access the execute function inside the effect callback, and as it's created within the same scope as the effect, React requires it to be a dependency.基本上,因为您在效果回调中访问execute function,并且由于它是在与效果相同的 scope 中创建的,所以 React 要求它是一个依赖项。

To resolve the problem with continuous re-rerendering, you need to make your execute function a memoized function by using useCallback要解决连续重新渲染的问题,您需要使用 useCallback 使您的执行 function 成为记忆的useCallback

Solving it should be as simple as:解决它应该很简单:

const execute = useCallback(() => (
    fs().then(res => {
        setData(res)
    }).catch(err => {
        // do some magic
    })
), [setData]);

useEffect(() => {
        execute()
        const interval = setInterval(execute, 15000)
        return(() => clearInterval(interval))
    }, [execute])

I think you can add execute as a dependency, since the function definition itself isn't changing.我认为您可以将execute添加为依赖项,因为 function 定义本身并没有改变。 Just debug if the component is re-rendering after every call.如果组件在每次调用后重新渲染,只需调试即可。 I think they shouldn't.我认为他们不应该。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM