As per this (currently) open issue on the eslint-plugin-react-refresh project, this is most likely a false positive.

The author of the project suggests these work-arounds

There are two solutions:

  • Put a disable comment for this rule for all the file. This will not actually break fast refresh so this ok>
  • Instead of exporting the router, export a component that render the Router provider
🌐
DEV Community
dev.to › md_belayethossain_56e787 › fast-refresh-only-works-when-a-file-only-exports-components-why-does-this-problem-occur-and-how-1b4
Fast refresh only works when a file only exports components. Why does this problem occur? And how do we solve it? - DEV Community
May 9, 2025 - React Fast Refresh identifies React components to efficiently reload and retain their state when you make code changes. If the file contains non-component exports, Fast Refresh cannot reliably determine which parts of the code should be treated as components, leading to this ESLint warning.
🌐
npm
npmjs.com › package › eslint-plugin-react-refresh
eslint-plugin-react-refresh - npm
import { defineConfig } from "eslint/config"; import { reactRefresh } from "eslint-plugin-react-refresh"; export default defineConfig({ // in main config for TSX/JSX source files plugins: { "react-refresh": reactRefresh.plugin, }, rules: { "react-refresh/only-export-components": "error", }, });
      » npm install eslint-plugin-react-refresh
    
Published   Feb 23, 2026
Version   0.5.2
Author   Arnaud Barré
🌐
GitHub
github.com › shadcn-ui › ui › issues › 1534
Export pattern causes warning from `eslint-plugin-react-refresh` · Issue #1534 · shadcn-ui/ui
September 16, 2023 - After initializing in a new React Vite TS Storybook starter, that happened to have this eslint plugin installed, I get warnings on a number of files that export functions in addition to a component. Also, although documentation about "Fast Refresh" is a bit scant unfortunately, some resources on it also say that each export requires its own export statement... https://github.com/ArnaudBarre/eslint-plugin-react-refresh · ...from my eslint config: { plugins: ['react-refresh'], rules: { 'react-refresh/only-export-components': [ 'warn', { allowConstantExport: true }, ], }, } Reactions are currently unavailable ·
Author   lunelson
🌐
GitHub
github.com › yannickcr › eslint-plugin-react › issues › 3176
new rule: forbid exporting React Components and non-components ...
January 12, 2022 - You switched accounts on another tab or window. Reload to refresh your session. ... There was an error while loading. Please reload this page. ... If you edit a module that only exports React component(s), Fast Refresh will update the code only for that module, and re-render your component.
Author   Alloyed
🌐
UNPKG
app.unpkg.com › eslint-plugin-react-refresh@0.3.4 › files › README.md
eslint-plugin-react-refresh
It can be disabled locally when it's effectively a React component: ```jsx // eslint-disable-next-line react-refresh/only-export-components export const CMS = () => <></>; ``` I may publish a rule base on type information from [typescript-eslint](https://github.com/typescript-eslint/typesc...
🌐
Gatsby
gatsbyjs.com › documentation › reference guides
Fast Refresh | Gatsby
The file you’re editing might have other exports in addition to a React component. Gatsby includes an ESLint rule by default to warn against this pattern in pages: Page templates must only export one default export (the page) and query as a named export.
🌐
GitHub
github.com › ArnaudBarre › eslint-plugin-react-refresh
GitHub - ArnaudBarre/eslint-plugin-react-refresh: Validate that your components can safely be updated with Fast Refresh · GitHub
import { defineConfig } from "eslint/config"; import { reactRefresh } from "eslint-plugin-react-refresh"; export default defineConfig({ // in main config for TSX/JSX source files plugins: { "react-refresh": reactRefresh.plugin, }, rules: { "react-refresh/only-export-components": "error", }, });
Starred by 329 users
Forked by 23 users
Languages   TypeScript 95.3% | JavaScript 4.7%
🌐
The Candid Startup
thecandidstartup.org › 2024 › 07 › 15 › bootstrapping-eslint.html
Bootstrapping ESLint
July 15, 2024 - In my case, the problem was with unit test setup code that does some fancy wrapping and re-exporting. There’s no need for fast refresh of React components in unit tests so I used an eslint comment to disable the warning for that file. /* eslint-disable react-refresh/only-export-components */
Find elsewhere
🌐
Reddit
reddit.com › r/react › how is this done in real life work? react context warning "fast refresh only works..."
r/react on Reddit: How is this done in real life work? React Context warning "Fast refresh only works..."
April 19, 2025 -

SOLVED: I ended up making a context.jsx and a provider.jsx, which seems kinda weird to me, but what do i know im just a jr.

Hello! I'm trying to learn to use contexts but i get this warning "Fast refresh only works when a file only exports components. Move your component(s) to a separate file."

I get it, its because im exporting twice. But how should i do it correctly? 2 files? One for context and one for provider??

This is my context.jsx, then i have a Cart.jsx where i plan to use it, and a Layout.jsx that wraps the outlet with the context

import {useState, createContext } from "react";

export const CartContext = createContext()

export function CartProvider({children}){
    const [cart, setCart] = useState([])

    const handleCart = (new) =>{
        setCart((prevCart) => [...prevCart, new])
    }

    return(
        <CartContext.Provider value={{cart, handleCart }}>
            {children}
        </CartContext.Provider>
    )
}
🌐
Biome
biomejs.dev › linter › rules › use-component-export-only-modules
useComponentExportOnlyModules | Biome
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.
🌐
GitHub
github.com › shadcn-ui › ui › issues › 7736
[bug]: Components are exporting constants and functions causing Fast Refresh lint issue · Issue #7736 · shadcn-ui/ui
July 4, 2025 - Some components, like the badge and the button, are exporting constants and maybe even functions. This causes ESLint to throw the following error 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)
Author   SayakMukhopadhyay
🌐
Lightrun
lightrun.com › answers › jsx-eslint-eslint-plugin-react-new-rule-forbid-exporting-react-components-and-non-components-from-the-same-module
new rule: forbid exporting React Components and non-components from the same module
According to the official react-fast-refresh docs: * If you edit a module that only exports React component(s), Fast Refresh will update the code only for ...
🌐
GitHub
github.com › shadcn-ui › ui › issues › 8489
[bug]: Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components. eslint(react-refresh/only-export-components · Issue #8489 · shadcn-ui/ui
October 16, 2025 - [bug]: Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components. eslint(react-refresh/only-export-components#8489
Author   BinLee1226
🌐
ESLint
eslint.org › docs › latest › rules › no-restricted-exports
no-restricted-exports - ESLint - Pluggable JavaScript Linter
Note that this option does not apply to export default or any default named exports. If you want to also restrict default exports, use the restrictDefaultExports option. This option allows you to restrict certain default declarations. The option works only if the restrictedNamedExports option does not contain the "default" value.