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
reactjs - How to avoid ESLint warning in React: Fast refresh only works when a file only exports components? - Stack Overflow
reactjs - Error: Fast refresh only works when a file only exports components - Stack Overflow
How is this done in real life work? React Context warning "Fast refresh only works..."
Loader warning "Fast refresh only works when a file only exports components..."
» npm install eslint-plugin-react-refresh
I just got the same error, a bit different use, but I also had a Utilities.tsx file where I abstracted away functions, error happened when I tried to have both function exports and component exports at the same time.
export const sum = (a,b) => {
return a + b
}
export const Component = () => {
return <p>Hello World</p>
}
This would cause an error since it has both functions and components being exported. You must have a separate file that exports your reusable or abstracted components in order for Fast Refresh to work.
import { lazy, Suspense } from "react";
import { ICustomRoute } from "../types";
import Loader from '../loader'
const LoginPage = lazy(() => import("../pages/auth/login"));
const LoginSuccessPage = lazy(() => import("../pages/auth/login/success"));
function lazyComponent (element: ReactNode): ReactNode {
return (
<Suspense fallback={<Loader />}>
{element}
</Suspense>
)
}
const authRoutes: ICustomRoute[] = [
{
path: "/login",
element: lazyComponent(<LoginPage />),
children: [
{
path: "welcome",
element: lazyComponent(<LoginSuccessPage />),
},
],
},
];
const router = createHashRouter(authRoutes)
function Router () {
return <RouterProvider router={router} />
}
export default Router
// App.tsx
function App () {
return (
<div>
<Routes />
</div>
)
}
This is how I solved it. If it's not appropriate, thank you very much for pointing it out
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>
)
}I've been getting "Fast Refresh had to perform a full reload. See https://nextjs.org/docs/basic-features/fast-refresh#how-it-works" warnings in my dev logs and it'd be nice to not.
I'm going to guess the culprit is exporting the props types along with the component. However, I'm guilty of doing this a lot and refactoring would be a pain unless I know it is in fact the issue.
Is there a good way to determine the actual cause? a lint rule? a verbose log?
» npm install @types/eslint-plugin-react-refresh