Skip to content

Commit 4c0f56d

Browse files
authored
docs: improve examples in react-x/no-unused-props (#1179)
1 parent 09cca7b commit 4c0f56d

File tree

1 file changed

+51
-19
lines changed

1 file changed

+51
-19
lines changed

packages/plugins/eslint-plugin-react-x/src/rules/no-unused-props.md

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,43 +38,75 @@ This is the TypeScript-only version of [`eslint-plugin-react/no-unused-prop-type
3838
### Failing
3939

4040
```tsx
41-
interface Props {
42-
abc: string; // used
43-
hello: string; // NOT used
41+
// 'onClick' is defined in 'ButtonProps' but never used in the 'Button' component.
42+
type ButtonProps = {
43+
children: React.ReactNode;
44+
onClick: () => void;
45+
};
46+
47+
function Button({ children }: ButtonProps) {
48+
return <button type="button">{children}</button>;
49+
}
50+
```
51+
52+
```tsx
53+
// 'avatarUrl' is defined in 'UserProfileProps' but never used in the 'UserProfile' component.
54+
interface UserProfileProps {
55+
username: string;
56+
avatarUrl: string;
4457
}
4558

46-
function Component(props: Props) {
47-
const { abc } = props; // `hello` isn't accessed from `props`
48-
return null;
59+
function UserProfile({ username }: UserProfileProps) {
60+
return <div>{username}</div>;
4961
}
5062
```
5163

5264
### Passing
5365

5466
```tsx
55-
interface Props {
56-
abc: string; // used
57-
hello: string; // used
67+
// All props are used.
68+
type ButtonProps = {
69+
children: React.ReactNode;
70+
onClick: () => void;
71+
};
72+
73+
function Button({ children, onClick }: ButtonProps) {
74+
return (
75+
<button type="button" onClick={onClick}>
76+
{children}
77+
</button>
78+
);
79+
}
80+
```
81+
82+
```tsx
83+
// 'className' is passed to the underlying <p> element with the rest of the props.
84+
// '...rest' is considered a use of all props that are not destructured.
85+
interface TextProps extends React.HTMLAttributes<HTMLParagraphElement> {
86+
children: React.ReactNode;
87+
className?: string;
5888
}
5989

60-
function Component(props: Props) {
61-
const { abc, hello } = props;
62-
return null;
90+
function Text({ children, ...rest }: TextProps) {
91+
return <p {...rest}>{children}</p>;
6392
}
6493
```
6594

6695
```tsx
67-
interface Props {
68-
abc: string; // used by Component1
69-
hello: string; // used by Component2
96+
// Both components use props from the same type definition.
97+
interface ProfileProps {
98+
userId: string; // Used by ProfilePage
99+
theme: "light" | "dark"; // Used by ProfileAvatar
70100
}
71101

72-
function Component1({ abc }: Props) {
73-
return null;
102+
function ProfilePage({ userId }: ProfileProps) {
103+
// ...
104+
return <div>User ID: {userId}</div>;
74105
}
75106

76-
function Component2({ hello }: Props) {
77-
return null;
107+
function ProfileAvatar({ theme }: ProfileProps) {
108+
// ...
109+
return <div className={theme}>...</div>;
78110
}
79111
```
80112

0 commit comments

Comments
 (0)