我这里有一个useEffect代码:

useEffect(() => {
    if (status === "completed" && !error) 
      props.onAddedComment();
  }, [status,error,props.onAddedComment]);

But I get this warning in the terminal: React Hook useEffect has a missing dependency: 'props'. Either include it or remove the dependency array. However, 'props' will change when any prop changes, so the preferred fix is to destructure the 'props' object outside of the useEffect call and refer to those specific props inside useEffect

如果我通过props.onAddedComment而不是整个props 对象,为什么我需要使用解构?即使我加入.onAddedComment,它还会指整个props 吗?

在这段代码中,我对使用params有同样的问题:

useEffect(() => {
    sendRequest(params.quoteId);
  }, [params.quoteId, sendRequest]);

我没有收到这个警告,为什么?

简而言之,我的问题是,即使我在props 后添加了.something个,我是否应该始终使用destructuring,为什么不使用params来警告我.

谢谢

推荐答案

谢谢你找到这个.参见here.

当您将函数作为对象的一部分调用时,行为函数也可能取决于使用的对象,even if the function itself doesn't change.下面是一个最简单的例子

useEffect(() => {
  obj.fn();
}, [obj.fn]);

可能是个问题:

const Child = ({ obj }) => {
  React.useEffect(() => {
    obj.fn();
  }, [obj.fn]);
  return null;
};
const App = () => {
  console.log('App rendering');
  const [count, setCount] = React.useState(0);
  // This fn is stable across renders
  const [fn] = React.useState(() => function() { console.log('count is', this.count); });
  React.useEffect(() => {
    setTimeout(() => {
      console.log('Calling setCount; expecting obj.fn to be called again');
      setCount(count + 1);
    }, 1000);
  }, []);
  return <Child obj={{ count, fn }} />
};

ReactDOM.render(<App />, document.querySelector('.react'));
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div class='react'></div>

deps的要点是让您在回调中的任何内容发生更改时调用effect回调.因为从理论上讲,对对象的更改可能会对执行的逻辑if the object has a method产生更改,所以应该将对象本身添加到依赖关系数组中.

这不会产生错误:

useEffect(() => {
  sendRequest(params.quoteId);
}, [params.quoteId, sendRequest]);

因为quoteId不是您正在调用的函数;params中的this并不重要,与我上面的代码片段和您的原始代码相比,在这种情况下,props中的this理论上很重要.

如果你这样做了

useEffect(() => {
  sendRequest(params.getQuoteId());
}, [params.getQuoteId, sendRequest]);

这会产生警告,因为现在getQuoteId的调用取决于params是什么.

从对象中删除函数并将函数放入独立标识符中也会删除警告,因为将函数作为独立标识符而不是作为对象的一部分调用会删除函数对对象的可能依赖关系-函数中的this不再引用对象,而是undefined.

一种方法是,当您作为对象的一部分调用函数时,对象本身作为隐藏的附加参数传递给函数,作为函数中的this.

这是:

useEffect(() => {
  obj.fn();
}, [obj.fn]);

就像做

const { obj } = fn;
useEffect(() => {
  fn.call(obj)
}, [fn]);

这显然是缺少obj作为一个依赖项,即使fn的实现根本不考虑其this.

Javascript相关问答推荐

zoom svg以适应圆

如何粗体匹配的字母时输入搜索框使用javascript?

如何在 cypress 中使用静态嵌套循环

优化Google Sheet脚本以将下拉菜单和公式添加到多行

当输入字段无效时,我的应用程序不会返回错误

如何将未排序的元素追加到数组的末尾?

在FAQ Accodion应用程序中使用React useState时出现问题

Next.js中的服务器端组件列表筛选

输入的值的类型脚本array.排序()

Jexl to LowerCase()和Replace()

与在编剧中具有动态价值的定位器交互

用Reaction-RT-Chart创建实时条形图

如何正确地在ComponentWillUnmount中卸载状态以避免内存泄漏?

表单数据中未定义的数组键

如何将对象推送到firestore数组?

Firefox的绝对定位没有达到预期效果

如何在不将整个文件加载到内存的情况下,在Node.js中实现Unix粘贴命令?

:host::ng-Deep不将样式应用于material 复选框

如何在预览中显示html+css与数据URL?

使用Firebase实时数据库`update`在同一 node 上执行多个更新