Skip to content

Commit df15810

Browse files
Update based on feedback
1 parent aad23fb commit df15810

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

docs/react-types/ComponentProps.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22
title: ComponentProps<T>
33
---
44

5-
`ComponentProps<T>` is a utility type that lets you grab all valid props of an HTML or SVG element, or infer prop type for a component.
5+
`ComponentProps<T>` is a utility type that lets you grab all valid props of an element, or infer prop type of a component.
66

7-
## Parameters
7+
:::note
8+
9+
Prefer `ComponentPropsWithRef<T>` if ref is forwarded and `ComponentPropsWithoutRef<T>` when ref is not forwarded.
810

9-
- `T extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>`
11+
:::
1012

11-
`keyof JSX.IntrinsicElements` is a set of all HTML and SVG elements, such as `"div"`, `"h1"` and `"path"`.
13+
## Parameters
1214

13-
`JSXElementConstructor<any>` is the type of a function or class component.
15+
- `T`: An element type. Examples include:
16+
- An HTML or SVG element such as `"div"`, `"h1"` or `"path"`.
17+
- A component type, such as `typeof Component`.
1418

1519
## Usage
1620

17-
### Getting all valid props of an HTML or SVG element
21+
### Getting all valid props of an element
1822

1923
`ComponentProps<T>` can be used to create a type that includes all valid `div` props.
2024

@@ -23,8 +27,8 @@ interface Props extends ComponentProps<"div"> {
2327
text: string;
2428
}
2529

26-
function Component({ className, children, text }: Props) {
27-
// ...
30+
function Component({ className, children, text, ...props }: Props) {
31+
// `Props` includes `text` in addition to all valid `div` props
2832
}
2933
```
3034

@@ -45,13 +49,19 @@ type MyType = ComponentProps<typeof Component>;
4549
// ^? type MyType = Props
4650
```
4751

48-
The type of a specific prop can also be inferred this way. Let's say you are using an `<Icon>` component from a component library. The component takes a `name` prop that determines what icon is shown. You need to use the type of `name` in your app, but it's not made available by the library. No problem!
52+
#### Inferring specific prop type
53+
54+
The type of a specific prop can also be inferred this way. Let's say you are using an `<Icon>` component from a component library. The component takes a `name` prop that determines what icon is shown. You need to use the type of `name` in your app, but it's not made available by the library. You could create a custom type:
55+
56+
```tsx
57+
type IconName = "warning" | "checkmark";
58+
```
59+
60+
However, this type if not really reflecting the actual set of icons made available by the library. A better solution is to infer the type:
4961

5062
```tsx
5163
import { Icon } from "component-library";
5264

5365
type IconName = ComponentProps<typeof Icon>["name"];
5466
// ^? type IconName = "warning" | "checkmark" | ...
5567
```
56-
57-
Needless to say, `ComponentProps<T>` can be very useful!

0 commit comments

Comments
 (0)