Skip to content

Commit df6608f

Browse files
committed
update React.FC and defaultProps recommendations
update React.FC and defaultProps recommendations with reference to typescript-cheatsheets/react#87
1 parent dfd69eb commit df6608f

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

README.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ The former pattern is shorter, so why would people use `React.FunctionComponent`
139139
- If you need to use `children` property inside the function body, in the former case it has to be added explicitly. `FunctionComponent<T>` already includes the correctly typed `children` property which then doesn't have to become part of your type.
140140
- Typing your function explicitly will also give you typechecking and autocomplete on its static properties, like `displayName`, `propTypes`, and `defaultProps`.
141141
- _In future_, it will also set `readonly` on your props just like `React.Component<T>` does.
142+
- HOWEVER, there are currently known issues using `defaultProps` with `React.FunctionComponent`. See [this issue for details](https://github.com/sw-yx/react-typescript-cheatsheet/issues/87) - scroll down to our `defaultProps` section for typing recommendations there.
142143

143144
```tsx
144145
const Title: React.FunctionComponent<{ title: string }> = ({
@@ -387,9 +388,25 @@ class App extends React.Component<{
387388

388389
## Typing defaultProps
389390

390-
For Typescript 3.0+, type inference [should work](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html), although [some edge cases are still problematic](https://github.com/sw-yx/react-typescript-cheatsheet/issues/61). Just type your props like normal.
391+
For Typescript 3.0+, type inference [should work](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html), although [some edge cases are still problematic](https://github.com/sw-yx/react-typescript-cheatsheet/issues/61). Just type your props like normal, except don't use `React.FC`.
391392

392393
```tsx
394+
// ////////////////
395+
// function components
396+
// ////////////////
397+
type Props = { age: number } & typeof defaultProps;
398+
const defaultProps = {
399+
who: 'Johny Five',
400+
};
401+
402+
const Greet = (props: Props) => {
403+
/*...*/
404+
};
405+
Greet.defaultProps = defaultProps
406+
407+
// ////////////////
408+
// class components
409+
// ////////////////
393410
export type Props = {
394411
name: string;
395412
};
@@ -405,6 +422,18 @@ export class Greet extends React.Component<Props> {
405422
// Type-checks! No type assertions needed!
406423
let el = <Greet />;
407424
```
425+
<details>
426+
<summary>Why does React.FC break defaultProps?</summary>
427+
428+
You can check the discussions here:
429+
430+
- https://medium.com/@martin_hotell/10-typescript-pro-tips-patterns-with-or-without-react-5799488d6680
431+
- https://github.com/DefinitelyTyped/DefinitelyTyped/issues/30695
432+
- https://github.com/sw-yx/react-typescript-cheatsheet/issues/87
433+
434+
This is just the current state and may be fixed in future.
435+
436+
</details>
408437

409438
<details>
410439
<summary>Typescript 2.9 and earlier</summary>

0 commit comments

Comments
 (0)