source: imaps-frontend/node_modules/eslint-plugin-react-refresh/README.md@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 5.1 KB
Line 
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
3Validate 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.
8If 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
16The 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
26npm i -D eslint-plugin-react-refresh
27```
28
29## Usage
30
31This plugin provides a single rule, `react-refresh/only-export-components`. There are multiple ways to enable it.
32
33### Recommended config
34
35```js
36import reactRefresh from "eslint-plugin-react-refresh";
37
38export default [
39 /* Main config */
40 reactRefresh.configs.recommended,
41];
42```
43
44### Vite config
45
46This enables the `allowConstantExport` option which is supported by Vite React plugins.
47
48```js
49import reactRefresh from "eslint-plugin-react-refresh";
50
51export default [
52 /* Main config */
53 reactRefresh.configs.vite,
54];
55```
56
57### Without config
58
59```js
60import reactRefresh from "eslint-plugin-react-refresh";
61
62export 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
88These examples are from enabling `react-refresh/only-exports-components`.
89
90### Fail
91
92```jsx
93export const foo = () => {};
94export const Bar = () => <></>;
95```
96
97```jsx
98export default function () {}
99export default compose()(MainComponent)
100```
101
102```jsx
103export * from "./foo";
104```
105
106```jsx
107const Tab = () => {};
108export const tabs = [<Tab />, <Tab />];
109```
110
111```jsx
112const App = () => {};
113createRoot(document.getElementById("root")).render(<App />);
114```
115
116### Pass
117
118```jsx
119export default function Foo() {
120 return <></>;
121}
122```
123
124```jsx
125const foo = () => {};
126export const Bar = () => <></>;
127```
128
129```jsx
130import { App } from "./App";
131createRoot(document.getElementById("root")).render(<App />);
132```
133
134## Options
135
136These options are all present on `react-refresh/only-exports-components`.
137
138```ts
139interface Options {
140 allowExportNames?: string[];
141 allowConstantExport?: boolean;
142 customHOCs?: string[];
143 checkJS?: boolean;
144}
145
146const 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
158If you use a framework that handles HMR of some specific exports, you can use this option to avoid warning for them.
159
160Example 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
175Don't warn when a constant (string, number, boolean, templateLiteral) is exported aside one or more components.
176
177This 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
188Enabling this option allows code such as the following:
189
190```jsx
191export const CONSTANT = 3;
192export const Foo = () => <></>;
193```
194
195### checkJS <small>(v0.3.3)</small>
196
197> Default: `false`
198
199If 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
209If 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```
Note: See TracBrowser for help on using the repository browser.