带有空数组作为输入的useCallback和没有第二个参数的useCallback有什么区别?

时间:2019-03-06 15:02:01

标签: reactjs react-hooks

在尝试更好地理解React Hooks的过程中,我遇到了一些意想不到的行为。我试图创建一个引用数组,并通过将传递给<div>'s的onRef函数推送到该数组。每次重新渲染组件时,数组都会不断变大,大概是因为它只是一个简单的箭头功能而没有被记忆。

因此,我添加了useCallback钩子,以确保不会多次获得相同的ref,但令我惊讶的是,每次重新渲染它仍调用该函数。将空数组添加为第二个参数后,引用仅按预期每个组件触发一次。

此行为在下面的代码段中得到演示。

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将有一个空数组作为第二个参数的默认值。那么,什么都不给出第二个参数呢?为什么行为不同?

2 个答案:

答案 0 :(得分:4)

对于useMemouseCallback(本质上只是useMemo的特例),如果第二个参数是一个空数组,则该值将被记忆一次并始终返回。

如果省略第二个参数,则该值将永远不会被记住,并且useCallbackuseMemo不会执行任何操作。

在某些情况下,您可能有条件地记住:

useMemo(someValue, shouldMemoize ? [] : null)

但是在大​​多数情况下,useMemouseCallback的第二个参数都应视为强制性的。实际上是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钩子eslint规则,因此,如果省略第二个参数,它将引发皮棉错误,因此很快这将是皮棉错误。

答案 1 :(得分:1)

我认为对于依赖项数组,所有挂钩,useEffect,useLayoutEffect,useCallback,useMemo都是相同的逻辑, 如果没有传递任何依赖项,则意味着我们为依赖项传递了null值,因此比较将始终为false,并且每次都会执行内联函数。

如果传递了Empty依赖项,则意味着没有东西可以进一步比较,因此内联函数只会执行一次。(就像我们指示React不再进行进一步的比较一样)

如果数组传递了某些变量,则它将根据变量的变化计算内联函数。

尽管将始终创建内联函数的实例。