用于将功能作为依赖性依赖性的用例中的用例

发布于 2025-01-20 05:18:34 字数 83 浏览 0 评论 0 原文

我开始学习做出反应,并遇到了代码片段,该代码段作为使用效率的依赖项数组传递。我想知道该功能通过作为依赖性传递的用例,为什么我们需要将功能作为依赖项传递?

I started learning to react and came across the code snippet where the function was passed as a dependency array in useEffect. I want to know the use case where such function is being passed as the dependency and why do we need to pass the function as a dependency?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北方的巷 2025-01-27 05:18:34

首先:仅当 useffect 回调中的代码使用该函数时,这才有意义。因此,让我们将其作为基准。 :-)

从根本上讲,您会这样做,因此 usefect 回调中的代码使用该函数的最新版本。

以下是一些重要的示例:

  • 该功能是道具。由于您的代码不知道为什么要获得该函数的新版本,因此重要的是,使用该功能的最新版本重新运行效果。
  • 该函数使用其关闭的状态信息(而不是使用状态设置器的回调形式)。如果您没有通过更新的功能重新运行效果,则该功能将使用过时的状态信息。 (但是我不会那样做。相反,我会使用状态设置器的回调形式。)

可能还有其他功能功能。

First: This only makes sense if the code in the useEffect callback uses the function. So let's take that as a baseline. :-)

Fundamentally, you'd do that so the code in the useEffect callback is using the most up-to-date version of the function.

Here are a couple of examples where that would be important:

  • The function is a prop. Since your code doesn't know why it got a new version of the function, it's important to re-run the effect with the up-to-date version of the function.
  • The function uses state information it closes over (rather than using the callback form of a state setter). If you didn't re-run the effect with the updated function, the function would use stale state information. (But I wouldn't do it that way. Instead, I'd have the function use the callback form of the state setter.)

There are likely others, but they all boil down to ensuring the effect uses the most recent version of the function.

北座城市 2025-01-27 05:18:34

我还在学习反应和这篇文章了解 useffect 的依赖关系数组中的功能。

中函数使用 < < /a>

函数是对象。它像对象一样具有自己的身份。每次都会重新呈现其状态的组件变化。当组件重新渲染时,组件内定义的函数将获得新的身份。

我做出了一些清晰的代码示例。文章中的示例尚不清楚我无法理解。使用“控制台”选项卡查看控制台日志。

I am also learning React and this article helped me understand functions in the dependency array of useEffect.

Functions in dependency array of useEffect

A function is an object. It has its own identity like an object. A component re-renders each time on of its state changes. When a component re-renders, the function which is defined inside the component gets a new identity.

I made a bit clear code example here. The example from the article was unclear for me to understand. Use the console tab to see the console logs.

君勿笑 2025-01-27 05:18:34

这取决于使用效果的使用和函数的定义。基本上,如果您在 useffect 数组中添加一个函数,则每次函数都会更改,或更准确,它的引用,您的效果将被新功能引用再次调用。

如果您想始终使用最新功能,这很好,但也可能很棘手。如果传递的函数是在组件内定义的,则意味着在每个组件上渲染该函数都将被重新定义,这意味着您的效果将对每个组件渲染进行调用。有时可能很重,取决于您的效果。

但是,如果定义函数的组件使用 usecallback ,则仍然可以避免它。这样,该函数将具有自己的依赖关系数组,并且只有在您决定需要时才重新定义(并更改参考)。

It depends on the use of the useEffect and the definition of the function. Basically, if you put a function inside a useEffect array, each time the function will change, or more accurately, it's reference, your effect will be called again, with the new function reference.

This is good in case you want to always use the latest function, but it can also be tricky. If the passed function is defined inside a component, it means that on every component render the function will be redefined, meaning your effect will be called on each component render. It can be heavy sometimes, depends on what your effect does.

It can still be avoided though, if the component in which the function is defined is using useCallback in order to memoize the function. This way, the function will have its own dependencies array, and will only be redefined (and change reference) when you decide it's needed.

萌化 2025-01-27 05:18:34

我有一个很好的用例,其中函数Current()返回当前语言代码,该语言代码是在另一个Utils模块中定义的函数,而不是内联函数定义了React组件本身

useEffect(() => {
      setTimeout(() => setButtonText('TXT_BUTTON_SEARCH_ANON'), 20000)
    }, [current()])

,我确实从Console中遇到了这样的错误:

警告:最终的参数传递给使用效应的大小在
渲染。该数组的顺序和大小必须保持恒定。

上一个:[no] incoming:[no,function(){
settimeout(function(){
返回setButTontext('txt_button_search_anon');
},20000); },0]
在内外

I have a good use case where the function current() returns the current language code, which is a function defined in another utils module, not a inline function defined the React component itself

useEffect(() => {
      setTimeout(() => setButtonText('TXT_BUTTON_SEARCH_ANON'), 20000)
    }, [current()])

I did get such a error from Console:

Warning: The final argument passed to useEffect changed size between
renders. The order and size of this array must remain constant.

Previous: [no] Incoming: [no, function () {
setTimeout(function () {
return setButtonText('TXT_BUTTON_SEARCH_ANON');
}, 20000); }, 0]
at IntroStep

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文