-
Hi, I'm trying to use React Router in my React Vite app testing my page with a loader but it comes with a warning on the loader const name: I couldn't figure out what it means and how to fix it. Can you help me on understand it and where I'm wrong? import { useEffect } from "react";
import { useLoaderData, Form, redirect, useParams } from "react-router-dom";
export const loader = ({ params }: any) => async () => {
const note = params.noteId;
console.log("note1: ", note);
if (!note) throw new Response("", { status: 404 });
return note;
};
export default function NotePage() {
const note = useLoaderData();
console.log("note2: ", note);
return (
<div>
Note Test
</div>
);
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 11 replies
-
Hey! You are exporting both a component and a function from the same file. Fast Refresh only works, when you're exporting components exclusively. For Fast Refresh to work, you need to put your |
Beta Was this translation helpful? Give feedback.
-
This seems to work for me in .eslintrc.cjs: module.exports = {
rules: {
"react-refresh/only-export-components": [
"warn",
{ allowExportNames: ["loader"] },
],
},
}; |
Beta Was this translation helpful? Give feedback.
-
The documentation explicitly mentions this issue: https://reactrouter.com/explanation/hot-module-replacement#supported-exports.
// These exports are handled by the React Router Vite plugin
// to be HMR-compatible
export const meta = { title: "Home" }; // ✅
export const links = [
{ rel: "stylesheet", href: "style.css" },
]; // ✅
// These exports are removed by the React Router Vite plugin
// so they never affect HMR
export const headers = { "Cache-Control": "max-age=3600" }; // ✅
export const loader = async () => {}; // ✅
export const action = async () => {}; // ✅
// This is not a route module export, nor a component export,
// so it will cause a full reload for this route
export const myValue = "some value"; // ❌
export default function Route() {} // ✅ So this should be fine and I would follow @joelstein initial advice and do the following: module.exports = {
rules: {
"react-refresh/only-export-components": [
"warn",
{
allowExportNames: [
"loader",
"clientLoader",
"action",
"clientAction",
"ErrorBoundary",
"HydrateFallback",
"headers",
"handle",
"links",
"meta",
"shouldRevalidate",
],
},
],
},
}; |
Beta Was this translation helpful? Give feedback.
Hey! You are exporting both a component and a function from the same file. Fast Refresh only works, when you're exporting components exclusively. For Fast Refresh to work, you need to put your
NotePage
Component and theloader
into different files, e.g.,notepage.element.tsx
andnotepage.loader.ts
.