-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Add ComponentProps reference as first React Types example #626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
--- | ||
title: ComponentProps<Element> | ||
--- | ||
|
||
`ComponentProps<Element>` is a utility type that lets you grab all valid props of an element, or infer prop type of a component. | ||
|
||
:::note | ||
|
||
Prefer `ComponentPropsWithRef<Element>` if ref is forwarded and `ComponentPropsWithoutRef<Element>` when ref is not forwarded. | ||
|
||
::: | ||
|
||
## Parameters | ||
|
||
- `T`: An element type. Examples include: | ||
- An HTML or SVG element such as `"div"`, `"h1"` or `"path"`. | ||
- A component type, such as `typeof Component`. | ||
|
||
## Usage | ||
|
||
### Getting all valid props of an element | ||
|
||
`ComponentProps<Element>` can be used to create a type that includes all valid `div` props. | ||
|
||
```tsx | ||
interface Props extends ComponentProps<"div"> { | ||
text: string; | ||
} | ||
|
||
function Component({ className, children, text, ...props }: Props) { | ||
// `Props` includes `text` in addition to all valid `div` props | ||
} | ||
``` | ||
|
||
### Infer prop types from a component | ||
|
||
In some cases, you might want to infer the type of a component's props. | ||
|
||
```tsx | ||
interface Props { | ||
text: string; | ||
} | ||
|
||
function Component(props: Props) { | ||
// ... | ||
} | ||
|
||
type MyType = ComponentProps<typeof Component>; | ||
// ^? type MyType = Props | ||
``` | ||
|
||
#### Inferring specific prop type | ||
|
||
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: | ||
|
||
```tsx | ||
type IconName = "warning" | "checkmark"; | ||
``` | ||
|
||
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: | ||
|
||
```tsx | ||
import { Icon } from "component-library"; | ||
|
||
type IconName = ComponentProps<typeof Icon>["name"]; | ||
// ^? type IconName = "warning" | "checkmark" | ||
``` | ||
|
||
You can also use the `Pick<Type, Keys>` utility type to accomplish the same thing: | ||
|
||
```tsx | ||
import { Icon } from "component-library"; | ||
|
||
type IconName = Pick<ComponentProps<typeof Icon>, "name">; | ||
// ^? type IconName = "warning" | "checkmark" | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
title: React Types | ||
--- | ||
|
||
`@types/react` makes some types available that can be very useful. Here's a list in alphabetical order with links to the detailed reference pages. | ||
|
||
- [`ComponentProps<T>`](/docs/react-types/ComponentProps) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.