Skip to content
← Back to rules

react/incompatible-library 正确性

功能

警告使用已知与记忆化(手动或自动)不兼容的库 API,例如 react-hook-formwatch()、TanStack Table 的 useReactTable() 和 TanStack Virtual 的 useVirtualizer()

由 React Compiler 提供支持,该编译器每个文件运行一次,并与其他 React Compiler 规则共享。移植自 react-hooks/incompatible-library

为什么这是不好的?

这些 API 依赖组件在每次更改时重新渲染; 由编译器或手动执行的记忆化会破坏其更新模型, 因此 UI 将不再反映新数据。

示例

此规则的错误代码示例:

jsx
import { useReactTable } from "@tanstack/react-table";
function Component({ columns, data }) {
  const table = useReactTable({ columns, data });
  return <div>{table.getRowModel().rows.length}</div>;
}

此规则的正确代码示例:

jsx
function Component({ rows }) {
  return <div>{rows.length}</div>;
}

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["react"],
  "rules": {
    "react/incompatible-library": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["react"],
  rules: {
    "react/incompatible-library": "error",
  },
});
bash
oxlint --deny react/incompatible-library --react-plugin

版本

此规则在 vnext 中添加。

参考