eslint-plugin-react-hooks
eslint-plugin-react-hooks 提供 ESLint 规则来强制执行 React 规则。
这个插件帮助你在构建时捕获对 React 规则的违反,确保你的组件和 hooks 遵循 React 的正确性和性能规则。这些 lint 检查涵盖了基础的 React 模式(exhaustive-deps 和 rules-of-hooks)以及由 React Compiler 标记的问题。React Compiler 的诊断会通过这个 ESLint 插件自动显示,即使你的应用尚未采用 compiler,也可以使用。
推荐规则
这些规则包含在 eslint-plugin-react-hooks 的 recommended 预设中:
exhaustive-deps- 验证 React hooks 的依赖数组是否包含所有必要的依赖项rules-of-hooks- 验证组件和 hooks 是否遵循 Hooks 规则component-hook-factories- 验证定义嵌套组件或 hooks 的高阶函数config- 验证 compiler 配置选项error-boundaries- 验证在子错误处理中使用 Error Boundaries,而不是 try/catchgating- 验证 gating 模式的配置globals- 验证在渲染期间对全局变量的赋值/修改immutability- 验证不要修改 props、state 和其他不可变值incompatible-library- 验证不要使用与 memoization 不兼容的库preserve-manual-memoization- 验证 compiler 保留现有的手动 memoizationpurity- 通过检查已知的不纯函数来验证组件/hooks 是否是纯的refs- 验证 refs 的正确使用,不要在渲染期间读取/写入set-state-in-effect- 验证不要在 effect 中同步调用 setStateset-state-in-render- 验证不要在渲染期间设置 statestatic-components- 验证组件是静态的,不会在每次渲染时重新创建unsupported-syntax- 验证不要使用 React Compiler 不支持的语法use-memo- 验证useMemohook 的使用是否没有返回值