Skip to content

Commit 47b41d4

Browse files
committed
Add TIL for react component props
1 parent 933f555 commit 47b41d4

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

typescript/react-component-prop.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# React get a components prop types
2+
3+
When using TS with React you may want to get the types of another component.
4+
5+
```tsx
6+
const MyButton: React.FC<{
7+
value: string;
8+
onChange: (newValue: string) => void;
9+
}> = () => {
10+
return <button value={value} onchange={onChange} />;
11+
};
12+
type ButtonProps = React.ComponentProps<typeof MyButton>;
13+
14+
// equivalent:
15+
16+
type ButtonProps = { value: string; onChange: (newValue: string) => void };
17+
```
18+
19+
This can be particularly useful when using an external library that does not expose the types for a component.

0 commit comments

Comments
 (0)