Skip to content

Commit 599b0fd

Browse files
docs: refresh README.md with explainer and options types (#63)
1 parent ba9bb7b commit 599b0fd

File tree

1 file changed

+58
-23
lines changed

1 file changed

+58
-23
lines changed

README.md

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

33
Validate that your components can safely be updated with fast refresh.
44

5-
## Limitations
5+
## Explainer
66

7-
⚠️ To avoid false positive, by default this plugin is only applied on `tsx` & `jsx` files. See options to run on JS files. ⚠️
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.
89

9-
The plugin rely on naming conventions (i.e. use PascalCase for components, camelCase for util functions). This is why there are some limitations:
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:
1017

1118
- `export *` are not supported and will be reported as an error
1219
- Anonymous function are not supported (i.e `export default function() {}`)
@@ -21,16 +28,7 @@ npm i -D eslint-plugin-react-refresh
2128

2229
## Usage
2330

24-
```json
25-
{
26-
"plugins": ["react-refresh"],
27-
"rules": {
28-
"react-refresh/only-export-components": "warn"
29-
}
30-
}
31-
```
32-
33-
### Flat config
31+
This plugin provides a single rule, `react-refresh/only-export-components`.
3432

3533
```js
3634
import reactRefresh from "eslint-plugin-react-refresh";
@@ -48,7 +46,22 @@ export default [
4846
];
4947
```
5048

51-
## Fail
49+
### Legacy config
50+
51+
```jsonc
52+
{
53+
"plugins": ["react-refresh"],
54+
"rules": {
55+
"react-refresh/only-export-components": "warn"
56+
}
57+
}
58+
```
59+
60+
## Examples
61+
62+
These examples are from enabling `react-refresh/only-exports-components`.
63+
64+
### Fail
5265

5366
```jsx
5467
export const foo = () => {};
@@ -74,14 +87,7 @@ const App = () => {};
7487
createRoot(document.getElementById("root")).render(<App />);
7588
```
7689

77-
## Pass with allowConstantExport
78-
79-
```jsx
80-
export const CONSTANT = 3;
81-
export const Foo = () => <></>;
82-
```
83-
84-
## Pass
90+
### Pass
8591

8692
```jsx
8793
export default function Foo() {
@@ -101,11 +107,29 @@ createRoot(document.getElementById("root")).render(<App />);
101107

102108
## Options
103109

110+
These options are all present on `react-refresh/only-exports-components`.
111+
112+
```ts
113+
interface Options {
114+
allowExportNames?: string[];
115+
allowConstantExport?: boolean;
116+
checkJS?: boolean;
117+
}
118+
119+
const defaultOptions: Options = {
120+
allowExportNames: [],
121+
allowConstantExport: false,
122+
checkJS: false,
123+
};
124+
```
125+
104126
### allowExportNames <small>(v0.4.4)</small>
105127

128+
> Default: `[]`
129+
106130
If you use a framework that handles HMR of some specific exports, you can use this option to avoid warning for them.
107131

108-
Example for [Remix](https://remix.run/docs/en/main/other-api/dev#:~:text=React%20Fast%20Refresh,-can%20only%20handle):
132+
Example for [Remix](https://remix.run/docs/en/main/discussion/hot-module-replacement#supported-exports):
109133

110134
```json
111135
{
@@ -118,6 +142,8 @@ Example for [Remix](https://remix.run/docs/en/main/other-api/dev#:~:text=React%2
118142

119143
### allowConstantExport <small>(v0.4.0)</small>
120144

145+
> Default: `false`
146+
121147
Don't warn when a constant (string, number, boolean, templateLiteral) is exported aside one or more components.
122148

123149
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.
@@ -131,8 +157,17 @@ This should be enabled if the fast refresh implementation correctly handles this
131157
}
132158
```
133159

160+
Enabling this option allows code such as the following:
161+
162+
```jsx
163+
export const CONSTANT = 3;
164+
export const Foo = () => <></>;
165+
```
166+
134167
### checkJS <small>(v0.3.3)</small>
135168

169+
> Default: `false`
170+
136171
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.
137172

138173
```json

0 commit comments

Comments
 (0)