Skip to content

Commit 5728701

Browse files
Merge pull request #501 from typescript-cheatsheets/react-18
Simplify React.FC implicit children section
2 parents 4e7b7d8 + 8c5a0da commit 5728701

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

README.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ const el2 = <MyArrayComponent />; // throws an error
402402
Unfortunately just annotating the function type will not help so if you really need to return other exotic types that React supports, you'd need to perform a type assertion:
403403

404404
```tsx
405-
const MyArrayComponent = () => Array(5).fill(<div />) as any as JSX.Element;
405+
const MyArrayComponent = () => (Array(5).fill(<div />) as any) as JSX.Element;
406406
```
407407

408408
[See commentary by @ferdaber here](https://github.com/typescript-cheatsheets/react-typescript-cheatsheet/issues/57).
@@ -1792,16 +1792,14 @@ This was done [on purpose](https://github.com/DefinitelyTyped/DefinitelyTyped/pu
17921792
```tsx
17931793
type Props = { children: React.ReactNode; type: "submit" | "button" };
17941794
export type Ref = HTMLButtonElement;
1795-
export const FancyButton = React.forwardRef(
1796-
(
1797-
props: Props,
1798-
ref: React.Ref<Ref> // <-- here!
1799-
) => (
1800-
<button ref={ref} className="MyClassName" type={props.type}>
1801-
{props.children}
1802-
</button>
1803-
)
1804-
);
1795+
export const FancyButton = React.forwardRef((
1796+
props: Props,
1797+
ref: React.Ref<Ref> // <-- here!
1798+
) => (
1799+
<button ref={ref} className="MyClassName" type={props.type}>
1800+
{props.children}
1801+
</button>
1802+
));
18051803
```
18061804

18071805
</details>

docs/basic/getting-started/function-components.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ Some differences from the "normal function" version:
4545

4646
- Note that there are some known issues using `defaultProps` with `React.FunctionComponent`. See [this issue for details](https://github.com/typescript-cheatsheets/react-typescript-cheatsheet/issues/87). We maintain a separate `defaultProps` section you can also look up.
4747

48-
- It provides an implicit definition of `children` (see below) - however there are some issues with the implicit `children` type (e.g. [DefinitelyTyped#33006](https://github.com/DefinitelyTyped/DefinitelyTyped/issues/33006)), and it might be better to be explicit about components that consume `children`, anyway. With the [React 18 type updates](https://github.com/DefinitelyTyped/DefinitelyTyped/pull/56210), `children` is no longer implicitly defined.
48+
- Before the [React 18 type updates](https://github.com/DefinitelyTyped/DefinitelyTyped/pull/56210), `React.FunctionComponent` provided an implicit definition of `children` (see below), which was heavily debated and is one of the reasons [`React.FC` was removed from the Create React App TypeScript template](https://github.com/facebook/create-react-app/pull/8177).
4949

5050
```tsx
51+
// before React 18 types
5152
const Title: React.FunctionComponent<{ title: string }> = ({
5253
children,
5354
title,

0 commit comments

Comments
 (0)