1 | # eslint-plugin-react-refresh [![npm](https://img.shields.io/npm/v/eslint-plugin-react-refresh)](https://www.npmjs.com/package/eslint-plugin-react-refresh)
|
---|
2 |
|
---|
3 | Validate that your components can safely be updated with Fast Refresh.
|
---|
4 |
|
---|
5 | ## Explainer
|
---|
6 |
|
---|
7 | "Fast Refresh", also known as "hot reloading", is a feature in many modern bundlers.
|
---|
8 | If you update some React component(s) on disk, then the bundler will know to update only the impacted parts of your page -- without a full page reload.
|
---|
9 |
|
---|
10 | `eslint-plugin-react-refresh` enforces that your components are structured in a way that integrations such as [react-refresh](https://www.npmjs.com/package/react-refresh) expect.
|
---|
11 |
|
---|
12 | ### Limitations
|
---|
13 |
|
---|
14 | ⚠️ To avoid false positives, by default this plugin is only applied on `tsx` & `jsx` files. See [Options](#options) to run on JS files. ⚠️
|
---|
15 |
|
---|
16 | The plugin relies on naming conventions (i.e. use PascalCase for components, camelCase for util functions). This is why there are some limitations:
|
---|
17 |
|
---|
18 | - `export *` are not supported and will be reported as an error
|
---|
19 | - Anonymous function are not supported (i.e `export default function() {}`)
|
---|
20 | - Class components are not supported
|
---|
21 | - All-uppercase function export is considered an error when not using direct named export (ex `const CMS = () => <></>; export { CMS }`)
|
---|
22 |
|
---|
23 | ## Installation
|
---|
24 |
|
---|
25 | ```sh
|
---|
26 | npm i -D eslint-plugin-react-refresh
|
---|
27 | ```
|
---|
28 |
|
---|
29 | ## Usage
|
---|
30 |
|
---|
31 | This plugin provides a single rule, `react-refresh/only-export-components`. There are multiple ways to enable it.
|
---|
32 |
|
---|
33 | ### Recommended config
|
---|
34 |
|
---|
35 | ```js
|
---|
36 | import reactRefresh from "eslint-plugin-react-refresh";
|
---|
37 |
|
---|
38 | export default [
|
---|
39 | /* Main config */
|
---|
40 | reactRefresh.configs.recommended,
|
---|
41 | ];
|
---|
42 | ```
|
---|
43 |
|
---|
44 | ### Vite config
|
---|
45 |
|
---|
46 | This enables the `allowConstantExport` option which is supported by Vite React plugins.
|
---|
47 |
|
---|
48 | ```js
|
---|
49 | import reactRefresh from "eslint-plugin-react-refresh";
|
---|
50 |
|
---|
51 | export default [
|
---|
52 | /* Main config */
|
---|
53 | reactRefresh.configs.vite,
|
---|
54 | ];
|
---|
55 | ```
|
---|
56 |
|
---|
57 | ### Without config
|
---|
58 |
|
---|
59 | ```js
|
---|
60 | import reactRefresh from "eslint-plugin-react-refresh";
|
---|
61 |
|
---|
62 | export default [
|
---|
63 | {
|
---|
64 | // in main config for TSX/JSX source files
|
---|
65 | plugins: {
|
---|
66 | "react-refresh": reactRefresh,
|
---|
67 | },
|
---|
68 | rules: {
|
---|
69 | "react-refresh/only-export-components": "error",
|
---|
70 | },
|
---|
71 | },
|
---|
72 | ];
|
---|
73 | ```
|
---|
74 |
|
---|
75 | ### Legacy config
|
---|
76 |
|
---|
77 | ```jsonc
|
---|
78 | {
|
---|
79 | "plugins": ["react-refresh"],
|
---|
80 | "rules": {
|
---|
81 | "react-refresh/only-export-components": "error"
|
---|
82 | }
|
---|
83 | }
|
---|
84 | ```
|
---|
85 |
|
---|
86 | ## Examples
|
---|
87 |
|
---|
88 | These examples are from enabling `react-refresh/only-exports-components`.
|
---|
89 |
|
---|
90 | ### Fail
|
---|
91 |
|
---|
92 | ```jsx
|
---|
93 | export const foo = () => {};
|
---|
94 | export const Bar = () => <></>;
|
---|
95 | ```
|
---|
96 |
|
---|
97 | ```jsx
|
---|
98 | export default function () {}
|
---|
99 | export default compose()(MainComponent)
|
---|
100 | ```
|
---|
101 |
|
---|
102 | ```jsx
|
---|
103 | export * from "./foo";
|
---|
104 | ```
|
---|
105 |
|
---|
106 | ```jsx
|
---|
107 | const Tab = () => {};
|
---|
108 | export const tabs = [<Tab />, <Tab />];
|
---|
109 | ```
|
---|
110 |
|
---|
111 | ```jsx
|
---|
112 | const App = () => {};
|
---|
113 | createRoot(document.getElementById("root")).render(<App />);
|
---|
114 | ```
|
---|
115 |
|
---|
116 | ### Pass
|
---|
117 |
|
---|
118 | ```jsx
|
---|
119 | export default function Foo() {
|
---|
120 | return <></>;
|
---|
121 | }
|
---|
122 | ```
|
---|
123 |
|
---|
124 | ```jsx
|
---|
125 | const foo = () => {};
|
---|
126 | export const Bar = () => <></>;
|
---|
127 | ```
|
---|
128 |
|
---|
129 | ```jsx
|
---|
130 | import { App } from "./App";
|
---|
131 | createRoot(document.getElementById("root")).render(<App />);
|
---|
132 | ```
|
---|
133 |
|
---|
134 | ## Options
|
---|
135 |
|
---|
136 | These options are all present on `react-refresh/only-exports-components`.
|
---|
137 |
|
---|
138 | ```ts
|
---|
139 | interface Options {
|
---|
140 | allowExportNames?: string[];
|
---|
141 | allowConstantExport?: boolean;
|
---|
142 | customHOCs?: string[];
|
---|
143 | checkJS?: boolean;
|
---|
144 | }
|
---|
145 |
|
---|
146 | const defaultOptions: Options = {
|
---|
147 | allowExportNames: [],
|
---|
148 | allowConstantExport: false,
|
---|
149 | customHOCs: [],
|
---|
150 | checkJS: false,
|
---|
151 | };
|
---|
152 | ```
|
---|
153 |
|
---|
154 | ### allowExportNames <small>(v0.4.4)</small>
|
---|
155 |
|
---|
156 | > Default: `[]`
|
---|
157 |
|
---|
158 | If you use a framework that handles HMR of some specific exports, you can use this option to avoid warning for them.
|
---|
159 |
|
---|
160 | Example for [Remix](https://remix.run/docs/en/main/discussion/hot-module-replacement#supported-exports):
|
---|
161 |
|
---|
162 | ```json
|
---|
163 | {
|
---|
164 | "react-refresh/only-export-components": [
|
---|
165 | "error",
|
---|
166 | { "allowExportNames": ["meta", "links", "headers", "loader", "action"] }
|
---|
167 | ]
|
---|
168 | }
|
---|
169 | ```
|
---|
170 |
|
---|
171 | ### allowConstantExport <small>(v0.4.0)</small>
|
---|
172 |
|
---|
173 | > Default: `false` (`true` in `vite` config)
|
---|
174 |
|
---|
175 | Don't warn when a constant (string, number, boolean, templateLiteral) is exported aside one or more components.
|
---|
176 |
|
---|
177 | This should be enabled if the fast refresh implementation correctly handles this case (HMR when the constant doesn't change, propagate update to importers when the constant changes.). Vite supports it, PR welcome if you notice other integrations works well.
|
---|
178 |
|
---|
179 | ```json
|
---|
180 | {
|
---|
181 | "react-refresh/only-export-components": [
|
---|
182 | "error",
|
---|
183 | { "allowConstantExport": true }
|
---|
184 | ]
|
---|
185 | }
|
---|
186 | ```
|
---|
187 |
|
---|
188 | Enabling this option allows code such as the following:
|
---|
189 |
|
---|
190 | ```jsx
|
---|
191 | export const CONSTANT = 3;
|
---|
192 | export const Foo = () => <></>;
|
---|
193 | ```
|
---|
194 |
|
---|
195 | ### checkJS <small>(v0.3.3)</small>
|
---|
196 |
|
---|
197 | > Default: `false`
|
---|
198 |
|
---|
199 | If your using JSX inside `.js` files (which I don't recommend because it forces you to configure every tool you use to switch the parser), you can still use the plugin by enabling this option. To reduce the number of false positive, only files importing `react` are checked.
|
---|
200 |
|
---|
201 | ```json
|
---|
202 | {
|
---|
203 | "react-refresh/only-export-components": ["error", { "checkJS": true }]
|
---|
204 | }
|
---|
205 | ```
|
---|
206 |
|
---|
207 | ### customHOCs <small>(v0.4.15)</small>
|
---|
208 |
|
---|
209 | If you're exporting a component wrapped in a custom HOC, you can use this option to avoid false positives.
|
---|
210 |
|
---|
211 | ```json
|
---|
212 | {
|
---|
213 | "react-refresh/only-export-components": [
|
---|
214 | "error",
|
---|
215 | { "customHOCs": ["observer", "withAuth"] }
|
---|
216 | ]
|
---|
217 | }
|
---|
218 | ```
|
---|