简体   繁体   English

React useEffect Hook 中 Object 和数组依赖项的 ESLint 规则

[英]ESLint rule for Object & array dependencies in the React useEffect Hook

The app that I work on is having an issue with using non-memoized versions of reference types such as arrays, objects, and functions (see Object & array dependencies in the React useEffect Hook ).我正在处理的应用程序在使用参考类型的非记忆版本时遇到问题,例如 arrays、对象和函数(请参阅React useEffect Hook 中的 Object 和数组依赖项)。 At the moment I am going through all of our code and manually fixing the issues, however this is not an ideal long-term solution for preventing developers from making the same mistake in the future.目前我正在检查我们所有的代码并手动修复问题,但这并不是防止开发人员在未来犯同样错误的理想长期解决方案。

I am investigating ways to prevent this from happening in the future to optimize performance and would like to find a lint rule to enforce that this isn't done by anyone in the future.我正在研究防止这种情况在未来发生以优化性能的方法,并希望找到一个 lint 规则来强制未来任何人都不会这样做。 However, I'm not seeing one.但是,我没有看到一个。

Does anyone have any suggestions on good ways to enforce that this error isn't made other than just communicating it effectively with the entire development team and helping to make sure that everyone is aware to watch for this during code reviews?除了与整个开发团队进行有效沟通并帮助确保每个人都知道在代码审查期间注意这一点之外,是否有人对强制执行此错误的好方法有任何建议?

I had the exact same thought -- It's a pervasive issue in code reviews and seeing your question was enough to motivate me to create a PR request in the eslint-plugin-react-hooks package to get this functionality added.我有完全相同的想法——这是代码审查中普遍存在的问题,看到您的问题足以激励我在 eslint-plugin-react-hooks package 中创建 PR 请求以添加此功能。

In the meantime (it'll probably take a while before it makes it into a release) you can use the interim eslint plugin on npm:同时(它可能需要一段时间才能发布)你可以使用 npm 上的临时 eslint 插件

npm install eslint-plugin-react-hooks-unreliable-deps --save-dev

and add the following to your .eslintrc.js :并将以下内容添加到您的.eslintrc.js

...
extends: [
    "plugin:react-hooks-unreliable-deps/recommended",
    ...
],
...

I do hope this suits your needs, and please submit any feedback to the issues page !我希望这能满足您的需求,请向问题页面提交任何反馈!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM