-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lazy Loading react component get some warning #25
Comments
You can check the Vite output in console for something like:
Your component is breaking fast refresh if this message is present I guess. |
No, it does not output
HMR Fast Refresh actually works, but the eslint react-refresh gives a warning on my IDE
Should this be fix on this eslint plugin? |
Can I have more code that trigger the issue. I don't think this two lines triggers a warning |
Yes, here is the code for my // router.ts
import { createBrowserRouter } from "react-router-dom";
import { lazy } from "react";
const Login = lazy(() => import("/src/pages/Login")); // warning here
const Home = lazy(() => import("/src/pages/Home")); // and here
const router = createBrowserRouter([
{
path: "/login",
element: <Login />,
},
{
path: "",
element: <Home />,
},
]);
export default router; Login component looks like so import ...
function Login() {
...
return <>...</>
}
export default Login; Importing them without |
Yeah this kind of a false warning, I will see what I can do.
The advantage of the second solution is that if react-rouder handle runtime change in the router config, you could HMR updates to the config |
I'll try implementing the second solution first. Thanks for the quick feedback. |
Second option works fine for me 👍 Example: import {
RouteObject,
RouterProvider,
createBrowserRouter,
} from 'react-router-dom'
const MyComponent = React.lazy(() => import('./MyComponent'))
const routes: RouteObject[] = [
{
element: <MyComponent />,
path: 'example',
},
]
const router = createBrowserRouter(routes)
export const Routes: React.FC = () => {
return <RouterProvider router={router} />
} |
I looked at it more carefully and both the Babel plugin and SWC are generating some registration code for |
Second option works for me also. Thanks |
@netcoding87 thanks for the example. |
@kurpav In that case you can have a Be careful to where you import the router so it doesn't create circular imports, which are bad for HMR. |
@ArnaudBarre I can't create two browser routers. my route files are very similar to React bulletproof. |
Same as the answer above, you need to put the the lazy wrappers into a separate file so that the router file doesn't contains any component and can safely be exported |
I was able to get around this by using react's // routesPublic.tsx
import { lazy } from "react";
import { RouteObject } from "react-router-dom";
// Fast refresh warning!
const Login = lazy(() => import("@/pages/Login"));
const routesPublic: RouteObject[] = [
{
path: "/",
element: <Login />,
},
];
export default routesPublic; to this: // routesPublic.ts
import { createElement, lazy } from "react";
import { RouteObject } from "react-router-dom";
// No warning!
const Login = lazy(() => import("@/pages/Login"));
const routesPublic: RouteObject[] = [
{
path: "/",
element: createElement(Login),
},
];
export default routesPublic; In my case, I'm using separate files for public and private routes. I wrapped them in a // routes.tsx
import { RouteObject } from "react-router-dom";
import { RouteAuthRequired } from "../components";
import routesPrivate from "./routesPrivate";
import routesPublic from "./routesPublic";
export const routes: RouteObject[] = [
...routesPublic,
{
element: <RouteAuthRequired />,
children: routesPrivate,
},
];
export default routes; // router.ts
import { createBrowserRouter } from "react-router-dom";
import routes from ".";
export const router = createBrowserRouter(routes);
export default router; |
- using ArnaudBarre/eslint-plugin-react-refresh#25 fix eslint warn of react-refresh
- using ArnaudBarre/eslint-plugin-react-refresh#25 fix eslint warn of react-refresh
any updates? |
Maybe I should close the issue, I don't think I can do much than adding a section to the documentation explaining that |
I've pinned the issue because I think other people will run into it, but yeah the limitation is annoying here but necessary to get nice DX. You can choose to ignore the warning on this router file, but I think exporting the route provider is what will get the trade off between number of files and DX: #25 (comment) |
I am building a Vite react ts app. I wanted to code split my components by using React.lazy
I tried import the component like so
Is this supported? or am I doing something wrong
The text was updated successfully, but these errors were encountered: