Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

ESLint warns that a React file exports more than components

ErrorFast refresh only works when a file only exports components. Use a new file to share constants or functions between components.

Short answer

The warning comes from eslint-plugin-react-refresh. Fast Refresh keeps state only when a file exports React components alone. Move shared hooks, helpers and context out into their own file. Constants may stay with allowConstantExport, and framework exports such as loader can be listed in allowExportNames.

  • Applies to Vite 7, Vite 8
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the linter on that file and read which export it names. The cause depends on what is exported next to the component.

    npx eslint src/components/Button.tsx
  2. 2Match what you see, then jump to its fix

    1. 1The warning points at export const useThing or export const ThemeContext in a .tsx file that also exports a componentA helper, hook or context is exported next to a componentFix
    2. 2The warning names an exported string, number or booleanA plain constant is exported with a componentFix
    3. 3The warning names loader, action, meta, links or headers in a Remix-style routeA framework requires extra exports from route filesFix
    4. 4The warning mentions export *, an anonymous default export or a class componentAn export shape the plugin cannot handleFix

What does the error look like?

  • Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
  • react-refresh/only-export-components

Cause 1 of 4

A helper, hook or context is exported next to a component

1Check

The plugin README says the rule enforces that components are structured the way react-refresh expects. A file that exports a function or object besides components breaks that.

grep -n "^export" src/context/ThemeContext.tsx

2Fix

Move the non-component export to its own file and import it where used. The component file then exports components only.

// theme-context.ts  (new file)
export const ThemeContext = createContext('light')

// ThemeProvider.tsx
import { ThemeContext } from './theme-context'
export function ThemeProvider({ children }) { /* ... */ }

3Verify

Lint again, then edit the component in the browser and see state survive.

npx eslint src

Confirmed by github.com

Did this fix it?

Cause 2 of 4

A plain constant is exported with a component

1Check

The vite preset turns allowConstantExport on, the default does not. If you wrote the rule by hand, it is off.

grep -n -B2 -A4 "only-export-components" eslint.config.*

2Fix

Use the vite preset, or set allowConstantExport. Per the plugin docs it stops warning when a constant (string, number, boolean, template literal) is exported beside components.

// eslint.config.js
import { defineConfig } from "eslint/config";
import { reactRefresh } from "eslint-plugin-react-refresh";

export default defineConfig(
  reactRefresh.configs.vite(),
);

3Verify

Run ESLint; the warning for the constant is gone.

npx eslint src

Confirmed by github.com

Did this fix it?

Cause 3 of 4

A framework requires extra exports from route files

1Check

Some frameworks handle refresh of specific exports themselves. The plugin has an option for exactly that.

grep -n "export const \(loader\|action\|meta\)" app/routes/*.tsx

2Fix

List those export names in allowExportNames.

"react-refresh/only-export-components": [
  "warn",
  { allowExportNames: ["meta", "links", "headers", "loader", "action"] },
]

3Verify

Lint the route file again.

npx eslint app/routes

Confirmed by github.com

Did this fix it?

Cause 4 of 4

An export shape the plugin cannot handle

1Check

The plugin reports export * as an error and does not support anonymous default exports or class components.

grep -n "export \*\|export default (\|export default function (" src -r

2Fix

Name the component and export it by name or as a named default, and replace export * with explicit exports.

export default function Dashboard() {
  return <main />
}

3Verify

Lint again; the shape warning is gone.

npx eslint src

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Keep hooks, contexts and helpers in files that export no components.
  • Use the vite preset in a Vite project instead of writing the rule from scratch.
  • Do not disable the rule file-wide; a disabled warning means state resets on every edit.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.