Skip to content

Commit ab4a14b

Browse files
docs(no-unstable-default-props): add new safeDefaultProps option
1 parent 09983fd commit ab4a14b

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

packages/plugins/eslint-plugin-react-x/src/rules/no-unstable-default-props.mdx

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ react-x/no-unstable-default-props
2020
`strict-typescript`
2121
`strict-type-checked`
2222

23+
**Features**
24+
25+
`⚙️`
26+
2327
## Description
2428

2529
Prevents using referential-type values as default props in object destructuring.
@@ -30,6 +34,12 @@ This harms performance as it means that React will have to re-evaluate hooks and
3034

3135
To fix the violations, the easiest way is to use a referencing variable in module scope instead of using the literal values.
3236

37+
## Rule Options
38+
39+
This rule has a single options object with the following property:
40+
41+
- `safeDefaultProps` (default: `[]`): An array of identifier names or regex patterns that are safe to use as default props.
42+
3343
## Examples
3444

3545
### Failing
@@ -167,6 +177,134 @@ function MyComponent({ num = 3, str = "foo", bool = true }: MyComponentProps) {
167177
}
168178
```
169179

180+
## Examples with `safeDefaultProps`
181+
182+
This option allows you to allowlist specific constructor or factory method identifiers that create value-type objects safe to use as default props.
183+
184+
### Configuration
185+
186+
```tsx
187+
{
188+
"@eslint-react/no-unstable-default-props": ["error", {
189+
"safeDefaultProps": ["Vector3", "Color3", "vector", "/^Immutable.*/"]
190+
}]
191+
}
192+
```
193+
194+
### Failing
195+
196+
```tsx
197+
import React from "react";
198+
199+
interface MyComponentProps {
200+
position: Vector3;
201+
}
202+
203+
// Without configuration, this would fail
204+
function MyComponent({ position = new Vector3(0, 0, 0) }: MyComponentProps) {
205+
// ^^^^^^^^^^^^^^^^^^^^^^
206+
// - A/an 'new expression' as default prop.
207+
return null;
208+
}
209+
```
210+
211+
```tsx
212+
import React from "react";
213+
214+
interface MyComponentProps {
215+
cache: Cache;
216+
}
217+
218+
// CustomCache is not in the allowlist, so this still fails
219+
function MyComponent({ cache = new CustomCache() }: MyComponentProps) {
220+
// ^^^^^^^^^^^^^^^^^
221+
// - A/an 'new expression' as default prop.
222+
return null;
223+
}
224+
```
225+
226+
```tsx
227+
import React from "react";
228+
229+
interface MyComponentProps {
230+
items: string[];
231+
}
232+
233+
// Object and array literals always fail regardless of configuration
234+
function MyComponent({ items = [] }: MyComponentProps) {
235+
// ^^
236+
// - A/an 'array expression' as default prop.
237+
return null;
238+
}
239+
```
240+
241+
### Passing
242+
243+
```tsx
244+
import React from "react";
245+
246+
interface MyComponentProps {
247+
position: Vector3;
248+
}
249+
250+
// Vector3 is in the allowlist, so constructor calls are allowed
251+
function MyComponent({ position = new Vector3(0, 0, 0) }: MyComponentProps) {
252+
return null;
253+
}
254+
```
255+
256+
```tsx
257+
import React from "react";
258+
259+
interface MyComponentProps {
260+
color: Color3;
261+
}
262+
263+
// Color3 is in the allowlist, so factory methods are allowed
264+
function MyComponent({ color = Color3.Red() }: MyComponentProps) {
265+
return null;
266+
}
267+
```
268+
269+
```tsx
270+
import React from "react";
271+
272+
interface MyComponentProps {
273+
position: Vector3;
274+
}
275+
276+
// 'vector' is in the allowlist, so member expression calls are allowed
277+
function MyComponent({ position = vector.create(0, 0, 0) }: MyComponentProps) {
278+
return null;
279+
}
280+
```
281+
282+
```tsx
283+
import React from "react";
284+
285+
interface MyComponentProps {
286+
data: ImmutableMap<string, number>;
287+
}
288+
289+
// Matches the regex pattern /^Immutable.*/
290+
function MyComponent({ data = ImmutableMap.of() }: MyComponentProps) {
291+
return null;
292+
}
293+
```
294+
295+
```tsx
296+
import React from "react";
297+
298+
interface MyComponentProps {
299+
list: ImmutableList<string>;
300+
}
301+
302+
// Also matches the regex pattern /^Immutable.*/
303+
function MyComponent({ list = ImmutableList.of("a", "b") }: MyComponentProps) {
304+
return null;
305+
}
306+
```
307+
170308
## Implementation
171309

172310
- [Rule Source](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x/src/rules/no-unstable-default-props.ts)

0 commit comments

Comments
 (0)