简体   繁体   English

useMemo(()=>()=>{}, []) 和 useCallback(()=>{}, []) 一样吗?

[英]Is useMemo(()=>()=>{}, []) the same as useCallback(()=>{}, [])?

I'm pretty sure the answer is yes.我很确定答案是肯定的。 But for those who may have thought useCallback memoizes the result https://reactjs.org/docs/hooks-reference.html#usecallback states但是对于那些可能认为useCallback记住结果的人来说https://reactjs.org/docs/hooks-reference.html#usecallback states

useCallback will return a memoized version of the callback useCallback 将返回回调的记忆版本

not the result.不是结果。 nor build a callback that memoizes.也不会构建一个记忆的回调。

Anyway if useMemo(()=>()=>{...}, []) was mean to workaround a misunderstanding of the useCallback functionality would it be the same as useCallback(()=>{}, []) ?无论如何,如果useMemo(()=>()=>{...}, [])是为了解决对useCallback功能的误解,它是否与useCallback(()=>{}, [])相同?

The doc says they are the same:医生说他们是一样的:

useCallback(fn, deps) is equivalent to useMemo(() => fn, deps). useCallback(fn, deps) 等同于 useMemo(() => fn, deps)。

Another thing to keep in mind for both useMemo and useCallback is this : useMemouseCallback要记住的另一件事是:

You may rely on useMemo as a performance optimization, not as a semantic guarantee.您可能依赖 useMemo 作为性能优化,而不是语义保证。 In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, eg to free memory for offscreen components.在未来,React 可能会选择“忘记”一些以前记忆的值并在下一次渲染时重新计算它们,例如为屏幕外组件释放内存。 Write your code so that it still works without useMemo — and then add it to optimize performance.编写您的代码,使其在没有 useMemo 的情况下仍能正常工作——然后添加它以优化性能。

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

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