Skip to content

Commit dfde467

Browse files
authored
add notes on ComponentProps usage
1 parent 8ffe6e5 commit dfde467

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ const MyArrayComponent = () => (Array(5).fill(<div />) as any) as JSX.Element;
192192

193193
## Hooks
194194

195-
Hooks are supported in `@types/react` from v16.8 up.
195+
Hooks are [supported in `@types/react` from v16.8 up](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/5565fe5e46e329a5ee02ddf739abe11bf16f278d/types/react/index.d.ts#L765-L973).
196196

197197
**useState**
198198

@@ -232,8 +232,8 @@ When using `useEffect`, take care not to return anything other than a function o
232232

233233
```ts
234234
function DelayedEffect(props: { timerMs: number }) {
235-
// bad! setTimeout implicitly returns a number because the arrow function body isn't wrapped in curly braces
236235
const { timerMs } = props;
236+
// bad! setTimeout implicitly returns a number because the arrow function body isn't wrapped in curly braces
237237
useEffect(() => setTimeout(() => {/* do stuff */}, timerMs), [timerMs])
238238
return null
239239
}
@@ -694,6 +694,8 @@ export const FancyButton = React.forwardRef<Ref, Props>((props, ref) => (
694694
));
695695
```
696696

697+
If you are grabbing the props of a component that forwards refs, use [`ComponentPropsWithRef`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L735).
698+
697699
More info: https://medium.com/@martin_hotell/react-refs-with-typescript-a32d56c4d315
698700

699701
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
@@ -944,17 +946,19 @@ Note that there are some TS users who don't agree with using `Partial` as it beh
944946

945947
This can be annoying but here are ways to grab the types!
946948

947-
- Grabbing the Prop types of a component: Use `typeof`, and optionally `Omit` any overlapping types
949+
- Grabbing the Prop types of a component: Use `React.ComponentProps` and `typeof`, and optionally `Omit` any overlapping types
948950

949951
```tsx
950-
import { Button } from 'library'; // but doesn't export ButtonProps
951-
type ButtonProps = React.ComponentProps<typeof Button>; // grab your own
952+
import { Button } from 'library'; // but doesn't export ButtonProps! oh no!
953+
type ButtonProps = React.ComponentProps<typeof Button>; // no problem! grab your own!
952954
type AlertButtonProps = Omit<ButtonProps, 'onClick'>; // modify
953955
const AlertButton: React.FC<AlertButtonProps> = props => (
954956
<Button onClick={() => alert('hello')} {...props} />
955957
);
956958
```
957959

960+
You may also use [`ComponentPropsWithoutRef`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/5565fe5e46e329a5ee02ddf739abe11bf16f278d/types/react/index.d.ts#L739) (instead of ComponentProps) and [`ComponentPropsWithRef`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/5565fe5e46e329a5ee02ddf739abe11bf16f278d/types/react/index.d.ts#L735) (if your component specifically forwards refs)
961+
958962
- Grabbing the return type of a function: use `ReturnType`:
959963

960964
```tsx

0 commit comments

Comments
 (0)