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
» npm install eslint-plugin-react-refresh
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 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