Comments (14)
Yeah this kind of a false warning, I will see what I can do.
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
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
from eslint-plugin-react-refresh.
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} />
}
from eslint-plugin-react-refresh.
I looked at it more carefully and both the Babel plugin and SWC are generating some registration code for const Login = lazy(() => import("./pages/Login"));
, so actually the warning may not be wrong. I need to look at it in more details, I will comeback to it affer other issues but I think exporting a wrapper of the RouterProvider is a good way to fix the issue for now
from eslint-plugin-react-refresh.
@kurpav In that case you can have a lazy-routes.tsx
which contains exports like const ViewA = React.lazy(() => import('./views/ViewA.tsx'))
and then have a router.tsx
which can import all components and export the router.
Be careful to where you import the router so it doesn't create circular imports, which are bad for HMR.
from eslint-plugin-react-refresh.
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
from eslint-plugin-react-refresh.
You can check the Vite output in console for something like:
[vite] hmr invalidate /src/components/Login.tsx Could not Fast Refresh. Learn more at https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react#consistent-components-exports
Your component is breaking fast refresh if this message is present I guess.
from eslint-plugin-react-refresh.
No, it does not output
[vite] hmr invalidate /src/components/Login.tsx Could not Fast Refresh. Learn more at https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react#consistent-components-exports
HMR Fast Refresh actually works, but the eslint react-refresh gives a warning on my IDE
ESLint: Fast refresh only works when a file only exports components. Move your component(s) to a separate file.(react-refresh/only-export-components)
Should this be fix on this eslint plugin?
from eslint-plugin-react-refresh.
Can I have more code that trigger the issue. I don't think this two lines triggers a warning
from eslint-plugin-react-refresh.
Yes, here is the code for my router.ts
that I want to lazy load some components.
// 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 React.lazy
does not trigger a warning
from eslint-plugin-react-refresh.
I'll try implementing the second solution first. Thanks for the quick feedback.
from eslint-plugin-react-refresh.
Second option works for me also. Thanks
from eslint-plugin-react-refresh.
@netcoding87 thanks for the example.
I wonder how to export router then to do navigation outside of react components.
Are there any ideas?
from eslint-plugin-react-refresh.
@ArnaudBarre
I want to implement option two. But what if I have two route files (public and protected) like React Bulletproof?
I can't create two browser routers. my route files are very similar to React bulletproof.
from eslint-plugin-react-refresh.
Related Issues (20)
- Warning is not appearing for enums HOT 1
- Fast Refresh and Hooks HOT 1
- Throwing error for typescript type exports. HOT 2
- Correct use with Context API and Vite - docs missing? HOT 2
- Consider not warning on `export *` when re-exported file is passing HOT 5
- Capitalized non-function not triggering rule HOT 2
- Possible false-positive with memo default export HOT 4
- Please add better description what it does HOT 2
- Add option for allowList of export names HOT 3
- False positives in v0.4.4 HOT 12
- How to use in flat config HOT 1
- Support for styled components HOT 1
- Warn exports instead of components when using only components HOT 3
- How to configure your plugin to work with a Vite+React project? HOT 2
- Possible false positive/negative HOT 1
- Allow constant exports doesn't work with objects. HOT 6
- Question about potential problems with barrel files HOT 1
- Bug: ts declaration types are not shipped with the package HOT 8
- feature request: Allow lazy loading HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eslint-plugin-react-refresh.