react/capitalized-calls Suspicious
功能
禁止在渲染期间直接调用首字母大写的函数或方法,而不是使用 JSX 渲染它们,因为首字母大写的名称是为组件保留的。
由 React Compiler 提供支持,该编译器每个文件运行一次,并与其他 React Compiler 规则共享。移植自 react-hooks/capitalized-calls。
为什么这是个问题?
将组件作为普通函数调用会使其对 React 不可见:它没有状态隔离,也没有自己的 Hooks 上下文,并且会破坏记忆化。
示例
此规则的错误代码示例:
jsx
import Child from "./Child";
function Component() {
return <div>{Child()}</div>;
}此规则的正确代码示例:
jsx
import Child from "./Child";
function Component() {
return (
<div>
<Child />
</div>
);
}如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["react"],
"rules": {
"react/capitalized-calls": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["react"],
rules: {
"react/capitalized-calls": "error",
},
});bash
oxlint --deny react/capitalized-calls --react-plugin版本
此规则在 vnext 中添加。
