You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+9-5Lines changed: 9 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -192,7 +192,7 @@ const MyArrayComponent = () => (Array(5).fill(<div />) as any) as JSX.Element;
192
192
193
193
## Hooks
194
194
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).
196
196
197
197
**useState**
198
198
@@ -232,8 +232,8 @@ When using `useEffect`, take care not to return anything other than a function o
232
232
233
233
```ts
234
234
function DelayedEffect(props: { timerMs:number }) {
235
-
// bad! setTimeout implicitly returns a number because the arrow function body isn't wrapped in curly braces
236
235
const { timerMs } =props;
236
+
// bad! setTimeout implicitly returns a number because the arrow function body isn't wrapped in curly braces
237
237
useEffect(() =>setTimeout(() => {/* do stuff */}, timerMs), [timerMs])
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
+
697
699
More info: https://medium.com/@martin_hotell/react-refs-with-typescript-a32d56c4d315
698
700
699
701
[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
944
946
945
947
This can be annoying but here are ways to grab the types!
946
948
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
948
950
949
951
```tsx
950
-
import {Button} from 'library'; // but doesn't export ButtonProps
951
-
type ButtonProps = React.ComponentProps<typeofButton>; // grab your own
952
+
import {Button} from 'library'; // but doesn't export ButtonProps! oh no!
953
+
type ButtonProps = React.ComponentProps<typeofButton>; // no problem! grab your own!
952
954
type AlertButtonProps = Omit<ButtonProps, 'onClick'>; // modify
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
+
958
962
- Grabbing the return type of a function: use `ReturnType`:
0 commit comments