reactjs - 使用空数组作为输入的 `useCallback` 和没有第二个参数的 `useCallback` 有什么区别?

标签 reactjs react-hooks

在我尝试更好地理解 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将有一个空数组作为第二个参数的默认值。那么不给出第二个参数到底有什么作用呢?为什么它的行为不同?

最佳答案

对于 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 Hook eslint 规则,这样如果省略第二个参数,它将引发 lint 错误,所以很快这可能会是一个 linter 错误。

关于reactjs - 使用空数组作为输入的 `useCallback` 和没有第二个参数的 `useCallback` 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55026139/

相关文章:

javascript - 通过 state.value ReactJS 将 textarea 值复制到新组件

javascript 我想做矩减法或差异

reactjs - react Hook : activable states patterns

javascript - 当第一次在 useEffect 上获取数据时,为什么 React 的 useReducer 返回空状态?

javascript - React Hooks useEffect 不会使用正确的状态

reactjs - 在ComponentDidUpdate中显示错误 react native 和Redux

reactjs - 将 Pros 传递给 React-Router 4.0.0 子路由

ruby-on-rails - 基本的 React 元素在带有 webpack 的 Rails 5.1 中不显示

javascript - 使用 React useState 钩子(Hook)向循环内的对象添加键值对

reactjs - 如何在 React 中处理父级的 mouseMove 事件?