javascript - ESLint rule for Object & array dependencies in the React useEffect Hook - Stack Overflow

The app that I work on is having an issue with using non-memoized versions of reference types such as a

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). 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. 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 municating it effectively with the entire development team and helping to make sure that everyone is aware to watch for this during code reviews?

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). 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. 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 municating it effectively with the entire development team and helping to make sure that everyone is aware to watch for this during code reviews?

Share Improve this question asked Jun 7, 2021 at 16:58 SuergSuerg 1338 bronze badges 7
  • 3 Does this ESLint plugin from the React team satisfy what you need? npmjs./package/eslint-plugin-react-hooks – evelynhathaway Commented Jun 8, 2021 at 2:04
  • 4 @evelynhathaway Thanks for the ment! Unfortunately, though that plugin doesn't solve this issue (we are actually already using it). One of the reasons that this is such a gnarly problem is that it isn't technically violating any of the Rules of Hooks, it's just not technically correct or performant. – Suerg Commented Jun 8, 2021 at 14:00
  • 1 So to clarify the issue, people keep adding full objects/arrays to the dependency list of hooks, and you'd like to warn them about not doing this (automatically, by a linter), and suggest alternatives? Am I understanding this correctly? – Balázs Édes Commented Nov 20, 2021 at 9:43
  • Yes, you're at least understanding it the way I understood it :) We have the same need and would love a linter that warns any dev adding an object or array to the dependency list of hooks. – pir Commented Nov 20, 2021 at 10:28
  • 3 @pir I did find github./yannickcr/eslint-plugin-react/pull/2848 which is pretty close, but for a different use case. For our use case with hooks described in this question, we ended up making a checkbox on our PR template that explains the issue to give visibility to all the developers and keep it top of mind. – Suerg Commented Nov 21, 2021 at 11:36
 |  Show 2 more ments

1 Answer 1

Reset to default 7

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.

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 install eslint-plugin-react-hooks-unreliable-deps --save-dev

and add the following to your .eslintrc.js:

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

I do hope this suits your needs, and please submit any feedback to the issues page!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745649225a4638159.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信