You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Performance optimizations are not free. They ALWAYS come with a cost but do NOT always come with a benefit to offset that cost.
Therefore, optimize responsibly.
优化并不是免费的。(在优化上)你往往失去的比得到的更多。所以我们需要负责任地优化。
So when should I useMemo and useCallback?
There are specific reasons both of these hooks are built-into React:
React guarantees the dispatch function to be constant throughout the component lifetime. So the example above doesn’t ever need to resubscribe the interval.
(You may omit dispatch, setState, and useRef container values from the deps because React guarantees them to be static. But it also doesn’t hurt to specify them.)
You may be wondering: how can this possibly work? How can the reducer “know” props when called from inside an effect that belongs to another render? The answer is that when you dispatch, React just remembers the action — but it will call your reducer during the next render. At that point the fresh props will be in scope, and you won’t be inside an effect.
This is why I like to think of useReducer as the “cheat mode” of Hooks. It lets me decouple the update logic from describing what happened. This, in turn, helps me remove unnecessary dependencies from my effects and avoid re-running them more often than necessary.
1. 《When to useMemo and useCallback》
优化并不是免费的。(在优化上)你往往失去的比得到的更多。所以我们需要负责任地优化。
所以我应该什么时候使用 useMemo 和 useCallback?
这两个 hooks 内置于 React 都有特别的原因:
2. 《A Complete Guide to useEffect》
E.g.
To
Moving Functions Inside Effects 把函数放到 useEffect 里,可以避免由于函数增长导致的依赖遗漏问题
如果有多个 useEffect 时,函数不用每个都放进去,可以用 useCallback 包裹起来,当函数参数变化时,自动生成新的函数,然后也会触发依赖此函数的 useEffect 执行。这样可以触发数据流的自动变化。
The text was updated successfully, but these errors were encountered: