|
| 1 | +<!-- This file is auto-generated by tasks/website/src/linter/rules/doc_page.rs. Do not edit it manually. --> |
| 2 | + |
| 3 | +<script setup> |
| 4 | +import { data } from '../version.data.js'; |
| 5 | +const source = `https://github.com/oxc-project/oxc/blob/${ data }/crates/oxc_linter/src/rules/react/only_export_components.rs`; |
| 6 | +</script> |
| 7 | + |
| 8 | +# react/only-export-components <Badge type="info" text="Restriction" /> |
| 9 | + |
| 10 | +<div class="rule-meta"> |
| 11 | +</div> |
| 12 | + |
| 13 | +### What it does |
| 14 | + |
| 15 | +Ensures that modules only **export React components (and related HMR-safe items)** so |
| 16 | +that Fast Refresh (a.k.a. hot reloading) can safely preserve component state. |
| 17 | +Concretely, it validates the shape of your module’s exports and common entrypoints |
| 18 | +(e.g. `createRoot(...).render(<App />)`) to match what integrations like |
| 19 | +`react-refresh` expect. The rule name is `react-refresh/only-export-components`. |
| 20 | + |
| 21 | +### Why is this bad? |
| 22 | + |
| 23 | +Fast Refresh can only reliably retain state if a module exports components and |
| 24 | +avoids patterns that confuse the refresh runtime. Problematic patterns (like |
| 25 | +`export *`, anonymous default functions, exporting arrays of JSX, or mixing |
| 26 | +non-component exports in unsupported ways) can cause: |
| 27 | + |
| 28 | +- Components to remount and lose state on edit |
| 29 | +- Missed updates (no refresh) or overly broad reloads |
| 30 | +- Fragile HMR behavior that differs between bundlers |
| 31 | + |
| 32 | +By enforcing predictable exports, edits stay fast and stateful during development. |
| 33 | + |
| 34 | +### Examples |
| 35 | + |
| 36 | +Examples of **incorrect** code for this rule: |
| 37 | + |
| 38 | +```jsx |
| 39 | +// 1) Mixing util exports with components in unsupported ways |
| 40 | +export const foo = () => {}; // util, not a component |
| 41 | +export const Bar = () => <></>; // component |
| 42 | +``` |
| 43 | + |
| 44 | +```jsx |
| 45 | +// 2) Anonymous default export (name is required) |
| 46 | +export default function() {} |
| 47 | +``` |
| 48 | + |
| 49 | +```jsx |
| 50 | +// 3) Re-exporting everything hides what’s exported |
| 51 | +export * from "./foo"; |
| 52 | +``` |
| 53 | + |
| 54 | +```jsx |
| 55 | +// 4) Exporting JSX collections makes components non-discoverable |
| 56 | +const Tab = () => null; |
| 57 | +export const tabs = [<Tab />, <Tab />]; |
| 58 | +``` |
| 59 | + |
| 60 | +```jsx |
| 61 | +// 5) Bootstrapping a root within the same module that defines components |
| 62 | +const App = () => null; |
| 63 | +createRoot(document.getElementById("root")).render(<App />); |
| 64 | +``` |
| 65 | + |
| 66 | +Examples of **correct** code for this rule: |
| 67 | + |
| 68 | +```jsx |
| 69 | +// Named or default component exports are fine |
| 70 | +export default function Foo() { |
| 71 | + return null; |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +```jsx |
| 76 | +// Utilities may coexist if allowed by options or naming conventions |
| 77 | +const foo = () => {}; |
| 78 | +export const Bar = () => null; |
| 79 | +``` |
| 80 | + |
| 81 | +```jsx |
| 82 | +// Entrypoint files may render an imported component |
| 83 | +import { App } from "./App"; |
| 84 | +createRoot(document.getElementById("root")).render(<App />); |
| 85 | +``` |
| 86 | + |
| 87 | +### Options (or not) |
| 88 | + |
| 89 | +#### allowExportNames |
| 90 | + |
| 91 | +`{ type: string[], default: [] }` |
| 92 | + |
| 93 | +Treat specific named exports as HMR-safe (useful for frameworks that hot-replace |
| 94 | +certain exports). For example, in Remix: |
| 95 | + |
| 96 | +```json |
| 97 | +{ |
| 98 | + "react/only-export-components": [ |
| 99 | + "error", |
| 100 | + { "allowExportNames": ["meta", "links", "headers", "loader", "action"] } |
| 101 | + ] |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +#### allowConstantExport |
| 106 | + |
| 107 | +`{ type: boolean, default: false }` |
| 108 | + |
| 109 | +Allow exporting primitive constants (string/number/boolean/template literal) |
| 110 | +alongside component exports without triggering a violation. Recommended when your |
| 111 | +bundler’s Fast Refresh integration supports this (enabled by the plugin’s `vite` |
| 112 | +preset). |
| 113 | + |
| 114 | +```json |
| 115 | +{ |
| 116 | + "react/only-export-components": [ |
| 117 | + "error", |
| 118 | + { "allowConstantExport": true } |
| 119 | + ] |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +```jsx |
| 124 | +// Allowed when allowConstantExport: true |
| 125 | +export const VERSION = "3"; |
| 126 | +export const Foo = () => null; |
| 127 | +``` |
| 128 | + |
| 129 | +#### customHOCs |
| 130 | + |
| 131 | +`{ type: string[], default: [] }` |
| 132 | + |
| 133 | +If you export components wrapped in custom higher-order components, list their |
| 134 | +identifiers here to avoid false positives: |
| 135 | + |
| 136 | +```json |
| 137 | +{ |
| 138 | + "react/only-export-components": [ |
| 139 | + "error", |
| 140 | + { "customHOCs": ["observer", "withAuth"] } |
| 141 | + ] |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +#### checkJS |
| 146 | + |
| 147 | +`{ type: boolean, default: false }` |
| 148 | + |
| 149 | +Check `.js` files that contain JSX (in addition to `.tsx`/`.jsx`). To reduce |
| 150 | +false positives, only files that import React are checked when this is enabled. |
| 151 | + |
| 152 | +```json |
| 153 | +{ |
| 154 | + "react/only-export-components": ["error", { "checkJS": true }] |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +## How to use |
| 159 | + |
| 160 | +To **enable** this rule in the CLI or using the config file, you can use: |
| 161 | + |
| 162 | +::: code-group |
| 163 | + |
| 164 | +```bash [CLI] |
| 165 | +oxlint --deny react/only-export-components --react-plugin |
| 166 | +``` |
| 167 | + |
| 168 | +```json [Config (.oxlintrc.json)] |
| 169 | +{ |
| 170 | + "plugins": ["react"], |
| 171 | + "rules": { |
| 172 | + "react/only-export-components": "error" |
| 173 | + } |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +::: |
| 178 | + |
| 179 | +## References |
| 180 | + |
| 181 | +- <a v-bind:href="source" target="_blank" rel="noreferrer">Rule Source</a> |
0 commit comments