在我尝试更好地理解 React Hooks 的过程中,我遇到了一些我没有预料到的行为。我试图创建一个引用数组,并通过 onRef 函数推送到所述数组,我将传递给我的 <div>'s
。每次组件重新渲染时,数组都会变得越来越大,大概只是因为它是一个简单的箭头函数并且没有被内存。
然后我添加了 useCallback
hook 以确保我不会多次获得相同的引用,但令我惊讶的是它仍然在每次重新渲染时调用该函数。添加空数组作为第二个参数后,refs 仅按预期每个组件触发一次。
下面的代码片段演示了此行为。
const Example = () => {
const _refs = React.useRef([]);
// Var to force a re-render.
const [ forceCount, forceUpdate ] = React.useState(0);
const onRef = (ref) => {
if (ref && ref !== null) {
console.log("Adding Ref -> Just an arrow function");
_refs.current.push(ref);
}
}
const onRefCallbackWithoutInputs = React.useCallback((ref) => {
if (ref && ref !== null) {
console.log("Adding Ref -> Callback without inputs.");
_refs.current.push(ref);
}
});
const onRefCallbackEmptyArray = React.useCallback((ref) => {
if (ref && ref !== null) {
console.log("Adding Ref -> Callback with empty array");
_refs.current.push(ref);
}
}, []);
React.useEffect(() => {
console.log("Refs size: ", _refs.current.length);
});
return (
<div>
<div ref={onRef}/>
<div ref={onRefCallbackWithoutInputs}/>
<div ref={onRefCallbackEmptyArray}/>
<div onClick={() => forceUpdate(forceCount + 1)}
style = {
{
width: '100px',
height: '100px',
marginTop: '12px',
backgroundColor: 'orange'
}
}>
{'Click me to update'}
</div>
</div>
);
};
ReactDOM.render(<Example/>, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script>
<div id='root' style='width: 100%; height: 100%'>
</div>
我假设useCallback
将有一个空数组作为第二个参数的默认值。那么不给出第二个参数到底有什么作用呢?为什么它的行为不同?
最佳答案
对于 useMemo
和 useCallback
(这本质上只是 useMemo
的特例),如果第二个参数是空数组,该值将被内存一次并始终返回。
如果省略第二个参数,则该值将永远不会被内存,并且 useCallback
和 useMemo
不会执行任何操作。
也许存在一些边缘情况,您可以有条件地内存:
useMemo(someValue, shouldMemoize ? [] : null)
但在绝大多数情况下,useMemo
和 useCallback
的第二个参数都应被视为强制性的。事实上,Typescript definitions treat them this way .
// Require a second argument, and it must be an array
function useCallback<T extends (...args: any[]) => any>(callback: T, deps: DependencyList): T;
// Second argument can be undefined, but must be explicitly passed as undefined, not omitted.
function useMemo<T>(factory: () => T, deps: DependencyList | undefined): T;
<小时/>
有一个 open pull request这增强了 exhaustive-deps
Hook eslint 规则,这样如果省略第二个参数,它将引发 lint 错误,所以很快这可能会是一个 linter 错误。
关于reactjs - 使用空数组作为输入的 `useCallback` 和没有第二个参数的 `useCallback` 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55026139/