eslint-plugin-react-hooks
eslint-plugin-react-hooks 提供 ESLint 规则以强制执行 React 规则。
这个插件可以帮助你在构建时捕捉违反 React 规则的情况,确保你的组件和钩子遵循 React 的正确性和性能规则。lint检查既包括基本的 React 模式(如 exhaustive-deps 和 rules-of-hooks),也包括 React 编译器标记的问题。React 编译器的诊断信息会自动通过这个 ESLint 插件显示,即使你的应用还没有采用该编译器,也可以使用这些诊断信息。
🌐 This plugin helps you catch violations of React’s rules at build time, ensuring your components and hooks follow React’s rules for correctness and performance. The lints cover both fundamental React patterns (exhaustive-deps and rules-of-hooks) and issues flagged by React Compiler. React Compiler diagnostics are automatically surfaced by this ESLint plugin, and can be used even if your app hasn’t adopted the compiler yet.
推荐规则
🌐 Recommended Rules
这些规则包含在 eslint-plugin-react-hooks 中的 recommended 预设中:
🌐 These rules are included in the recommended preset in eslint-plugin-react-hooks:
exhaustive-deps- 验证 React 钩子的依赖数组是否包含所有必要的依赖rules-of-hooks- 验证组件和钩子是否遵循钩子规则component-hook-factories- 验证定义嵌套组件或钩子的高阶函数config- 验证编译器配置选项error-boundaries- 验证使用错误边界而不是 try/catch 来处理子组件错误gating- 验证门控模式的配置globals- 验证在渲染期间对全局变量的赋值/修改immutability- 验证是否修改 props、state 及其他不可变值incompatible-library- 验证是否使用与记忆化不兼容的库preserve-manual-memoization- 验证编译器是否保留了现有的手动记忆化purity- 通过检查已知的不纯函数来验证组件/钩子是否为纯粹的refs- 验证 refs 的正确使用,而不是在渲染期间读写set-state-in-effect- 验证在 effect 中同步调用 setStateset-state-in-render- 验证在渲染期间设置状态static-components- 验证组件是静态的,而不是每次渲染都重新创建unsupported-syntax- 验证 React 编译器不支持的语法use-memo- 验证useMemo钩子的使用是否没有返回值