[英]React.useCallback: do I need to pass functions in array of dependencies along with dependencies?
React docs example shows only [a, b]
as dependencies. React 文档示例仅显示
[a, b]
作为依赖项。 Function doSomething
is not passed.函数
doSomething
没有通过。
const memoizedCallback = useCallback(
() => {
doSomething(a, b);
},
[a, b],
);
But later docs reads:但后来的文档写道:
every value referenced inside the callback should also appear in the dependencies array.
回调中引用的每个值也应该出现在依赖项数组中。
My question: do I need to pass doSomething
as well?我的问题:我还需要通过
doSomething
吗?
const memoizedCallback = useCallback(
() => {
doSomething(a, b);
},
[a, b, doSomething],
);
Technically it's not required if it doesn't change, but it can bring unexpected behavior, so just for simplicity you need to pass all things you used inside if it's declared in component or comes from props.从技术上讲,如果它不更改,则不需要它,但它可能会带来意外行为,所以为了简单起见,如果它在组件中声明或来自 props,则需要传递你在内部使用的所有东西。 Also if you are using eslint, there is awesome plugin for hooks which is de-facto standard
此外,如果您使用的是 eslint,则有一个很棒的钩子插件,它是事实上的标准
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.