Skip to content

Commit c1785bb

Browse files
Mark React.FC as "not needed" instead of "discouraged" (typescript-cheatsheets#639)
Co-authored-by: Filip Tammergård <[email protected]>
1 parent f0bd7c5 commit c1785bb

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,19 @@ const App = ({ message }: AppProps): JSX.Element => <div>{message}</div>;
194194

195195
// you can also inline the type declaration; eliminates naming the prop types, but looks repetitive
196196
const App = ({ message }: { message: string }) => <div>{message}</div>;
197+
198+
// Alternatively, you can use `React.FunctionComponent` (or `React.FC`), if you prefer.
199+
// With latest React types and TypeScript 5.1. it's mostly a stylistic choice, otherwise discouraged.
200+
const App: React.FunctionComponent<{ message: string }> = ({ message }) => (
201+
<div>{message}</div>
202+
);
197203
```
198204

199205
> Tip: You might use [Paul Shen's VS Code Extension](https://marketplace.visualstudio.com/items?itemName=paulshen.paul-typescript-toolkit) to automate the type destructure declaration (incl a [keyboard shortcut](https://twitter.com/_paulshen/status/1392915279466745857?s=20)).
200206
201207
<details>
202208

203-
<summary><b>Why is <code>React.FC</code> discouraged? What about <code>React.FunctionComponent</code>/<code>React.VoidFunctionComponent</code>?</b></summary>
209+
<summary><b>Why is <code>React.FC</code> not needed? What about <code>React.FunctionComponent</code>/<code>React.VoidFunctionComponent</code>?</b></summary>
204210

205211
You may see this in many React+TypeScript codebases:
206212

@@ -210,7 +216,7 @@ const App: React.FunctionComponent<{ message: string }> = ({ message }) => (
210216
);
211217
```
212218

213-
However, the general consensus today is that `React.FunctionComponent` (or the shorthand `React.FC`) is [discouraged](https://github.com/facebook/create-react-app/pull/8177). This is a nuanced opinion of course, but if you agree and want to remove `React.FC` from your codebase, you can use [this jscodeshift codemod](https://github.com/gndelia/codemod-replace-react-fc-typescript).
219+
However, the general consensus today is that `React.FunctionComponent` (or the shorthand `React.FC`) is not needed. If you're still using React 17 or TypeScript lower than 5.1, it is even [discouraged](https://github.com/facebook/create-react-app/pull/8177). This is a nuanced opinion of course, but if you agree and want to remove `React.FC` from your codebase, you can use [this jscodeshift codemod](https://github.com/gndelia/codemod-replace-react-fc-typescript).
214220

215221
Some differences from the "normal function" version:
216222

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@ const App = ({ message }: AppProps): JSX.Element => <div>{message}</div>;
1919

2020
// you can also inline the type declaration; eliminates naming the prop types, but looks repetitive
2121
const App = ({ message }: { message: string }) => <div>{message}</div>;
22+
23+
// Alternatively, you can use `React.FunctionComponent` (or `React.FC`), if you prefer.
24+
// With latest React types and TypeScript 5.1. it's mostly a stylistic choice, otherwise discouraged.
25+
const App: React.FunctionComponent<{ message: string }> = ({ message }) => (
26+
<div>{message}</div>
27+
);
2228
```
2329

2430
> Tip: You might use [Paul Shen's VS Code Extension](https://marketplace.visualstudio.com/items?itemName=paulshen.paul-typescript-toolkit) to automate the type destructure declaration (incl a [keyboard shortcut](https://twitter.com/_paulshen/status/1392915279466745857?s=20)).
2531
2632
<details>
2733

28-
<summary><b>Why is <code>React.FC</code> discouraged? What about <code>React.FunctionComponent</code>/<code>React.VoidFunctionComponent</code>?</b></summary>
34+
<summary><b>Why is <code>React.FC</code> not needed? What about <code>React.FunctionComponent</code>/<code>React.VoidFunctionComponent</code>?</b></summary>
2935

3036
You may see this in many React+TypeScript codebases:
3137

@@ -35,7 +41,7 @@ const App: React.FunctionComponent<{ message: string }> = ({ message }) => (
3541
);
3642
```
3743

38-
However, the general consensus today is that `React.FunctionComponent` (or the shorthand `React.FC`) is [discouraged](https://github.com/facebook/create-react-app/pull/8177). This is a nuanced opinion of course, but if you agree and want to remove `React.FC` from your codebase, you can use [this jscodeshift codemod](https://github.com/gndelia/codemod-replace-react-fc-typescript).
44+
However, the general consensus today is that `React.FunctionComponent` (or the shorthand `React.FC`) is not needed. If you're still using React 17 or TypeScript lower than 5.1, it is even [discouraged](https://github.com/facebook/create-react-app/pull/8177). This is a nuanced opinion of course, but if you agree and want to remove `React.FC` from your codebase, you can use [this jscodeshift codemod](https://github.com/gndelia/codemod-replace-react-fc-typescript).
3945

4046
Some differences from the "normal function" version:
4147

0 commit comments

Comments
 (0)