如何配置规则避免不必要的组件重新渲染

解读

在国内一线/二线大厂的 React/Vue 技术栈面试中,"性能优化"是高频考点。面试官抛出"用 ESLint 避免不必要的组件重新渲染"并不是让你手写 React.memo,而是考察三件事:

  1. 你是否知道"渲染"问题的本质是"依赖"问题——依赖数组、props 引用、事件函数引用;
  2. 你是否能把 React/Vue 官方或社区沉淀下来的最佳实践,沉淀成可自动化、可强制落地的 Lint 规则;
  3. 你是否能把规则集成到团队现有的研发流程(VS Code、Git Hooks、CI、MR 门禁)。

因此,回答必须给出"规则名 + 配置片段 + 落地路径",并解释每条规则到底拦截了哪一类"多余渲染"场景。

知识点

  1. React 相关核心规则(eslint-plugin-react、eslint-plugin-react-hooks)

    • react/prop-types:避免隐式 any,防止因类型错误导致父组件传新引用。
    • react/no-array-index-key:阻止用 index 做 key,降低列表重排时子组件误刷新概率。
    • react-hooks/exhaustive-deps:官方唯一指定,强制 deps 数组完整,99% 的"useEffect 死循环"或"忘记监听"都能被提前暴露。
    • react-hooks/rules-of-hooks:保证 Hook 调用顺序稳定,间接减少因 Hook 条件调用导致的异常更新。
    • react/jsx-no-bind:阻止在 JSX 中直接写箭头函数,避免每次渲染生成新函数引用。
    • react/jsx-no-constructed-context-values:防止 {value: {...}} 每次新建对象,触发 Context 消费组件大面积刷新。
  2. React 性能专项规则(eslint-plugin-react-perf)

    • react-perf/jsx-no-new-object-as-prop
    • react-perf/jsx-no-new-array-as-prop
    • react-perf/jsx-no-new-function-as-prop 三条规则分别检测"字面量对象/数组/函数作为 props",从源头消灭"引用变化"导致的子组件重渲染。
  3. Vue3 组合式 API 场景(eslint-plugin-vue)

    • vue/no-unused-properties:清理未使用的 data/props,减少响应式系统负担。
    • vue/no-side-effects-in-computed-properties:阻止在计算属性里产生副作用,避免计算属性频繁重新求值。
    • vue/require-emit-validator:规范 emit,降低因参数变化导致父组件刷新。
    • @typescript-eslint/no-unused-vars:配合 Vue3 + TS,消灭未使用响应式变量,防止"随意声明 reactive"引发的级联更新。
  4. 通用 JavaScript/TypeScript 规则

    • prefer-const + no-var:保证变量引用稳定,降低因重复赋值导致的监听变化。
    • @typescript-eslint/no-explicit-any:any 会跳过类型检查,容易把"新引用"传下去。
    • import/no-cycle:循环依赖会让模块多次执行,触发 React/Vue 组件多次静态初始化,间接造成重复挂载副作用。
  5. 工程落地

    • 共享配置:封装成 @your-company/eslint-config-react-performance,发布到私有 npm。
    • 分层校验:本地 IDE 走 cache,Git Hooks 走 lint-staged,CI 走 --max-warnings=0 直接中断流水线。
    • 渐进升级:通过 eslint-annotate 把存量警告标成 "warn + 注释",只拦截增量错误,避免业务线反弹。

答案

下面给出一份可直接拷贝到 .eslintrc.js 的"国内团队生产级"配置,并逐条说明如何"避免不必要的组件重新渲染"。

module.exports = {
  root: true,
  parser: '@typescript-eslint/parser',
  plugins: [
    'react',
    'react-hooks',
    'react-perf',
    'vue',
    '@typescript-eslint'
  ],
  extends: [
    'eslint:recommended',
    'plugin:react/recommended',
    'plugin:react-hooks/recommended',
    'plugin:react-perf/recommended',
    'plugin:vue/vue3-recommended',
    '@typescript-eslint/recommended'
  ],
  parserOptions: {
    ecmaVersion: 2022,
    sourceType: 'module',
    ecmaFeatures: { jsx: true }
  },
  rules: {
    // 1) 依赖数组必须写全,防止 useEffect 忘记监听或死循环
    'react-hooks/exhaustive-deps': ['error', { additionalHooks: 'useRecoilCallback|useSWR' }],

    // 2) 禁止在 JSX 里当场 bind 或写箭头函数,消灭"新函数引用"
    'react/jsx-no-bind': ['error', {
      allowArrowFunctions: false,
      allowBind: false,
      ignoreRefs: true
    }],

    // 3) 禁止把新建对象/数组/函数直接作为 props
    'react-perf/jsx-no-new-object-as-prop': 'error',
    'react-perf/jsx-no-new-array-as-prop': 'error',
    'react-perf/jsx-no-new-function-as-prop': 'error',

    // 4) 禁止在 JSX 里构造新 Context value
    'react/jsx-no-constructed-context-values': 'error',

    // 5) 禁止用 index 做 key,降低列表抖动时子组件误刷新
    'react/no-array-index-key': 'error',

    // 6) Vue3 组合式 API:计算属性必须纯函数,避免频繁重求值
    'vue/no-side-effects-in-computed-properties': 'error',

    // 7) 清理未使用变量,防止"随意 reactive"导致级联更新
    '@typescript-eslint/no-unused-vars': ['error', { vars: 'all', args: 'after-used' }],

    // 8) 拒绝 any,避免把"新引用"传下去
    '@typescript-eslint/no-explicit-any': 'error',

    // 9) 强制 const,保证引用稳定
    'prefer-const': 'error'
  },
  settings: {
    react: { version: 'detect' }
  },
  overrides: [
    {
      files: ['*.vue'],
      parser: 'vue-eslint-parser',
      parserOptions: { parser: '@typescript-eslint/parser' }
    }
  ]
};

落地步骤(按国内习惯):

  1. 把上述配置封装成 npm 包 @your-org/eslint-config-perf,发到公司私有 Nexus。
  2. 在业务仓库安装并 extends 即可,零配置接入。
  3. VS Code 安装 dbaeumer.vscode-eslint,开启 "eslint.format.enable": true,保存即自动修复。
  4. 新增 husky + lint-staged,仅校验 git add 的文件,平均耗时 <2s,不影响提交体验。
  5. GitLab CI 中加 job:
    lint:
      script: eslint . --max-warnings=0
    
    MR 流水线失败即无法合并,确保增量代码零警告。
  6. 存量大文件允许 // eslint-disable-next-line react-perf/xxx 过渡,但需 code review 双人同意,防止滥用。

通过"规则 + 工具链"双保险,把"不必要的组件重新渲染"消灭在编码阶段,而不是等性能 profiling 才回头补课。

拓展思考

  1. 规则不是越严越好。国内很多老旧项目用 class 组件 + decorators,若直接开 react-perf 会爆炸。正确姿势是"增量收紧":先只开 react-hooks/exhaustive-deps,三个月后存量清理完再开 jsx-no-new-*。
  2. 与 TypeScript 类型系统互补。例如用"const enum + as const"让对象引用完全不变,ESLint 规则再兜底,性能收益可量化:在美团外卖商家列表业务实测,打开 react-perf 规则后,列表滚动帧率提升 8~12 FPS。
  3. 自定义规则。若团队用自研状态管理(如基于 Proxy 的类 MobX 库),可写 ESLint 插件检测"在渲染函数里访问深层嵌套 observable",防止冗余收集依赖。自定义规则走公司 monorepo 统一管理,可随版本发版。
  4. 与自动化测试联动。把 eslint --format json 结果上传至 SonarQube,结合覆盖率做"性能债务"看板,让 leader 一眼看到哪个服务最慢、哪个模块警告最多,方便排期重构。
  5. 未来趋势。React Compiler(原 React Forget)一旦正式落地,将自动做"依赖记忆化",届时部分 react-perf 规则可能降级为 warning。但 ESLint 仍是"最后一道人工闸门",因为编译器只能解决框架级问题,业务层"错误 deps"、"随意声明 reactive"仍需 Lint 拦截。