useComponentExportOnlyModules
此内容尚不支持你的语言。
Diagnostic Category: lint/nursery/useComponentExportOnlyModules
Since: v1.9.2
Sources:
- Inspired from:
react-refresh/only-export-components
Enforce declaring components only within modules that export React Components exclusively.
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
Section titled ExamplesInvalid
Section titled Invalidcode-block.jsx:1:14 lint/nursery/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.
code-block.jsx:1:7 lint/nursery/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.
code-block.jsx:1:7 lint/nursery/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
Section titled ValidFunctions that return standard React components are also permitted.
Options
Section titled OptionsallowConstantExport
Section titled allowConstantExportSome tools, such as Vite, allow exporting constants along with components. By enabling the following, the rule will support the pattern.
allowExportNames
Section titled allowExportNamesIf you use a framework that handles Hot Module Replacement(HMR) of some specific exports, you can use this option to avoid warning for them.
Example for Remix: