gpt4 book ai didi

reactjs - 使用 useMemo 或 useCallback VS useRef 的空依赖关系

转载 作者:行者123 更新时间:2023-12-03 13:22:14 25 4
gpt4 key购买 nike

在此GitHub issue我基本上建议改变:

x = useCallback( ... , []);

致:

x = useRef( ... ).current;

两者是相同的,但使用 useRef React 不会比较依赖关系。

对此的答复附带了一个问题:

Is there ever a situation where a dependency-less useMemo or useCallback would be a better choice than useRef?

我想不出一个,但我可能忽略了一些用例。

那么有人能想到这样的情况吗?

最佳答案

根据 React Hooks API 文档:

Keep in mind that useRef doesn’t notify you when its content changes. Mutating the .current property doesn’t cause a re-render ... Using a callback ref ensures that even if a child component displays the measured node later (e.g. in response to a click), we still get notified about it in the parent component and can update the measurements.

您可以阅读更多相关信息herehere .

关于reactjs - 使用 useMemo 或 useCallback VS useRef 的空依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58810036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com