Skip to content

Commit 9750bd7

Browse files
swyxioeps1lonswyx
authored
document new VoidFunctionComponent type (#269)
* document new VoidFunctionComponent type closes #268 * Update docs/basic/getting-started/function-components.md Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com> * Update function-components.md * Update docs/basic/getting-started/function-components.md Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com> * Update function-components.md * fix format Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com> Co-authored-by: swyx <wanshawn@amazon.com>
1 parent d467788 commit 9750bd7

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const App = ({ message }: AppProps) => <div>{message}</div>;
1212

1313
<details>
1414

15-
<summary><b>What about `React.FC`/`React.FunctionComponent`?</b></summary>
15+
<summary><b>What about `React.FC`/`React.FunctionComponent`/`React.VoidFunctionComponent`?</b></summary>
1616

1717
You can also write components with `React.FunctionComponent` (or the shorthand `React.FC` - they are the same):
1818

@@ -39,6 +39,44 @@ const Title: React.FunctionComponent<{ title: string }> = ({
3939
}) => <div title={title}>{children}</div>;
4040
```
4141

42+
<details>
43+
<summary>
44+
45+
As of [@types/react PR #46643](https://github.com/DefinitelyTyped/DefinitelyTyped/pull/46643) (TODO: update with @types/react version when merged), you can use a new `React.VoidFunctionComponent` or `React.VFC` type if you wish to declare the accepted `children` explicitly. This is an interim solution until FunctionComponent will accept no children by default (planned for React 18).
46+
47+
</summary>
48+
49+
```ts
50+
type Props = { foo: string };
51+
52+
// OK now, in future, error
53+
const FunctionComponent: React.FunctionComponent<Props> = ({
54+
foo,
55+
children,
56+
}: Props) => {
57+
return (
58+
<div>
59+
{foo} {children}
60+
</div>
61+
); // OK
62+
};
63+
64+
// Error now, in future, deprecated
65+
const VoidFunctionComponent: React.VoidFunctionComponent<Props> = ({
66+
foo,
67+
children,
68+
}) => {
69+
return (
70+
<div>
71+
{foo}
72+
{children}
73+
</div>
74+
);
75+
};
76+
```
77+
78+
</details>
79+
4280
- _In the future_, it may automatically mark props as `readonly`, though that's a moot point if the props object is destructured in the parameter list.
4381

4482
In most cases it makes very little difference which syntax is used, but you may prefer the more explicit nature of `React.FunctionComponent`.

0 commit comments

Comments
 (0)