[英]Should I wrap every prop with useCallback or useMemo, when to use this hooks?
With react hooks now available should I in case of functional components wrap every function passed with props with useCallback and every other props value with useMemo ? 随着反应钩现已我应该在功能部件的情况下,包裹用的道具通过各项功能useCallback并与其他所有的道具价值useMemo ?
Also having custom function inside my component dependent on any props value should I wrap it with useCallback ? 我的组件内部的自定义函数依赖于任何道具值,我应该用useCallback包装吗?
What are good practices to decide which props or const values from component wrap with this hooks ? 使用此挂钩决定组件包装中的哪些props或const值有什么好的做法?
If this improves performance why not to do it at all times ? 如果这样可以提高性能,为什么不在任何时候都这样做呢?
Lets consider custom button where we wrap click handler and add custom logic 让我们考虑自定义按钮,我们包装点击处理程序并添加自定义逻辑
function ExampleCustomButton({ onClick }) {
const handleClick = useCallback(
(event) => {
if (typeof onClick === 'function') {
onClick(event);
}
// do custom stuff
},
[onClick]
);
return <Button onClick={handleClick} />;
}
Lets consider custom button where we wrap click handler and add custom logic on condition 让我们考虑自定义按钮,我们包装点击处理程序并在条件下添加自定义逻辑
function ExampleCustomButton({ someBool }) {
const handleClick = useCallback(
(event) => {
if (someBool) {
// do custom stuff
}
},
[someBool]
);
return <Button onClick={handleClick} />;
}
Should i in this two cases wrap my handler with useCallback ? 我应该在这两种情况下使用useCallback包装我的处理程序吗?
Similar case with use memo. 使用备忘录的类似情况。
function ExampleCustomButton({ someBool }) {
const memoizedSomeBool = useMemo(() => someBool, [someBool])
const handleClick = useCallback(
(event) => {
if (memoizedSomeBool) {
// do custom stuff
}
},
[memoizedSomeBool]
);
return <Button onClick={handleClick} />;
}
In this example I even pass memoized value to useCallback . 在这个例子中,我甚至将memoized值传递给useCallback 。
Another case what if in the component tree many components memoize same value ? 另一种情况是,如果在组件树中许多组件记忆相同的值? How does this impact performance ?
这对性能有何影响?
Not worth it, for multiple reasons: 不值得,原因有多种:
To put all together - you'd waste more time typing all the hooks than a user would gain on having them in the application if you want to put them everywhere. 把所有的东西放在一起 - 如果你想把它们放在任何地方,你会浪费更多的时间来键入所有钩子,而不是用户在应用程序中获得它们。 The good old rule applies: Measure, then optimize .
适用的旧规则适用: 测量,然后优化 。
I agree with the principles proposed by @jalooc 我同意@jalooc提出的原则
To give some more insight about the exhibited use cases in the OP, here is my advice: 为了更好地了解OP中展示的用例,我的建议如下:
function Component() {
const callback = useCallback(() => { dostuff }, [deps])
return <Child prop={callback} />
}
The above makes sense if Child
is a very expensive component to render. 如果
Child
是一个非常昂贵的渲染组件,上述内容就有意义了。 As such, it is probably exported like so: 因此,它可能像这样导出:
function Child() {
...this takes significant CPU...
}
// Export as a pure component
export default React.memo(Child)
function Component({ foo }) {
// This very expensive computation will only run when it's input (foo)
// changes, allowing Component to re-render without performance issues
const bar = useMemo(() => {
... something very complicated with `foo` ...
}, [foo])
return <div>{bar}</div>
}
useCallback
) or move it outside the scope if you can. useCallback
它( useCallback
)或将其移出范围外(如果可以)。 React.memo
, with the help of #2
if necessary React.memo
使其纯净,必要时在#2
的帮助下 useMemo
) useMemo
它( useMemo
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.