useComponentExportOnlyModules
¥Summary
-
规则生效日期:
v1.9.2¥Rule available since:
v1.9.2 -
诊断类别:
lint/style/useComponentExportOnlyModules¥Diagnostic Category:
lint/style/useComponentExportOnlyModules -
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 warning。
¥The default severity of this rule is warning.
-
此规则属于以下域:
¥This rule belongs to the following domains:
-
来源:
¥Sources:
-
灵感来自
react-refresh/only-export-components¥Inspired from
react-refresh/only-export-components
-
¥How to configure
{ "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
allowConstantExport
Section titled “allowConstantExport”某些工具(例如 Vite)允许导出常量以及组件。启用以下选项后,该规则将支持此模式。
¥Some tools, such as Vite, allow exporting constants along with components. By enabling the following, the rule will support the pattern.
{ "linter": { "rules": { "style": { "useComponentExportOnlyModules": { "options": { "allowConstantExport": true } } } } }}allowExportNames
Section titled “allowExportNames”如果你使用的框架处理某些特定导出项的 热模块替换 (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:
{ "linter": { "rules": { "style": { "useComponentExportOnlyModules": { "options": { "allowExportNames": [ "json", "loader", "headers", "meta", "links", "scripts" ] } } } } }}¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号