Skip to content

useComponentExportOnlyModules

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"style": {
"useComponentExportOnlyModules": "error"
}
}
}
}

¥Description

强制仅在仅导出 React Components 的模块中声明组件。

¥Enforce declaring components only within modules that export React Components exclusively.

这是启用 React Fast Refresh 功能所必需的,该功能可以提高开发效率。判断某个元素是否为组件取决于命名约定。组件应使用 PascalCase 编写,常规函数应使用 camelCase 编写。如果框架已有既定约定,可以考虑选择性地指定例外情况。

¥This is necessary to enable the React Fast Refresh feature, which improves development efficiency. The determination of whether something is a component depends on naming conventions. Components should be written in PascalCase and regular functions in camelCase. If the framework already has established conventions, consider optionally specifying exceptions.

¥Examples

¥Invalid

export const foo = () => {};
export const Bar = () => <></>;
code-block.jsx:1:14 lint/style/useComponentExportOnlyModules ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Exporting a non-component with components is not allowed.

> 1 │ export const foo = () => {};
^^^
2 │ export const Bar = () => <></>;
3 │

Fast Refresh only works when a file only exports components.

Consider separating non-component exports into a new file.

If it is a component, it may not be following the variable naming conventions.

const Tab = () => {};
export const tabs = [<Tab />, <Tab />];
code-block.jsx:1:7 lint/style/useComponentExportOnlyModules ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Components should be exported.

> 1 │ const Tab = () => {};
^^^
2 │ export const tabs = [<Tab />, <Tab />];
3 │

Fast Refresh only works when a file only exports components.

Consider separating component exports into a new file.

If it is not a component, it may not be following the variable naming conventions.

const App = () => {}
createRoot(document.getElementById("root")).render(<App />);
code-block.jsx:1:7 lint/style/useComponentExportOnlyModules ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexported components are not allowed.

> 1 │ const App = () => {}
^^^
2 │ createRoot(document.getElementById(“root”)).render(<App />);
3 │

Fast Refresh only works when a file only exports components.

Consider separating component exports into a new file.

If it is not a component, it may not be following the variable naming conventions.

¥Valid

export default function Foo() {
return <></>;
}
const foo = () => {};
export const Bar = () => <></>;
import { App } from "./App";
createRoot(document.getElementById("root")).render(<App />);

返回标准 React 组件的函数也是允许的。

¥Functions that return standard React components are also permitted.

import { memo } from 'react';
const Component = () => <></>
export default memo(Component);

¥Options

某些工具(例如 Vite)允许导出常量以及组件。启用以下选项后,该规则将支持此模式。

¥Some tools, such as Vite, allow exporting constants along with components. By enabling the following, the rule will support the pattern.

biome.json
{
"linter": {
"rules": {
"style": {
"useComponentExportOnlyModules": {
"options": {
"allowConstantExport": true
}
}
}
}
}
}

如果你使用的框架处理某些特定导出项的 热模块替换 (HMR),则可以使用此选项来避免这些导出项的警告。

¥If you use a framework that handles Hot Module Replacement(HMR) of some specific exports, you can use this option to avoid warning for them.

Remix 示例:

¥Example for Remix:

biome.json
{
"linter": {
"rules": {
"style": {
"useComponentExportOnlyModules": {
"options": {
"allowExportNames": [
"json",
"loader",
"headers",
"meta",
"links",
"scripts"
]
}
}
}
}
}
}

¥Related links