reactjs - eslint-plugin-react-hooks 出现意外错误(react-hooks/exhaustive-deps)
问题描述
我有一个问题,我的 vscode eslint 给了我错误的警告。我在反应组件中有一个简单的函数,我正在尝试使用 useCallBack 钩子进行优化。
这是一个简单的函数,我得到两个道具,并且在我创建的 params 对象中使用,根据文档,我将这两个道具作为 useCallBack 的依赖项。
const Test = ({ ReportAddressNumber, ClientNumber }) => {
const [test, setTest] = useState({ count: 0 });
const fetchSampleIDs = useCallback(() => {
setTest(true);
const URL = `/Samples`;
const params = {
ReportAddressNumber,
ClientNumber,
};
fetch(URL, params)
}, [ClientNumber, ReportAddressNumber]);
useEffect(() => {
fetchSampleIDs();
}, [fetchSampleIDs]);
return <h1>{test.count}</h1>;
};
现在问题出在 vscode 中,它告诉我这两个道具作为 useCallback 中的依赖项是不必要的。
React Hook useCallback has unnecessary dependencies: 'ClientNumber' and 'ReportAddressNumber'. Either exclude them or remove the dependency array.eslint(react-hooks/exhaustive-deps)
我可以用我的.eslintrc
和package.json
文件重现这个。我创建了这个代码框来解决这个问题,为了查看实际问题,您需要将代码框导出为 zip 并运行npm install
并在 vscode 中查看它。
https://codesandbox.io/s/kind-mahavira-fvsub
我在这里感谢您的知识和贡献,谢谢。
解决方案
花了4个小时后,我终于得到了答案。
存在冲突的依赖关系。我使用eslint-config-airbnb: 16.1.0
的不包括任何 react-hooks linting 配置。但是,我自己安装了eslint-plugin-react-hooks
与eslint-config-aribnb
版本冲突的。
eslint-config-airbnb
按照说明升级解决了我的问题。如果有人有同样的问题。
推荐阅读
- python - 使用亚马逊 EC2 实例托管烧瓶 Web GUI
- python - 仅对某些方法应用 Flask MethodView 装饰器
- android - Android kotlin:可变实时数据更改不使用 xml 中的数据绑定反映/更新布局
- perl - 比较版本而不使用版本 CPAN 模块
- google-apps-script - 从android执行google appscript
- java - 如何使用 Java/Jackson 解析 YAML 文件并管理 $ref 参考值
- c# - 发布单元测试数据依赖的 REST api
- acumatica - 有没有办法在 Acumatica aspx 页面中指定图像的最大高度和宽度?
- php - 不推荐使用 copydb 后如何使用 PHP 复制 MongoDB 数据库
- quarkus - 如何解决 TransactionReaper 的问题?