File tree Expand file tree Collapse file tree 1 file changed +12
-12
lines changed Expand file tree Collapse file tree 1 file changed +12
-12
lines changed Original file line number Diff line number Diff line change @@ -432,24 +432,24 @@ const Greet = (props: Props) => {
432
432
/* ...*/
433
433
};
434
434
Greet .defaultProps = defaultProps
435
+ ```
435
436
436
- // ////////////////
437
- // class components
438
- // ////////////////
439
- export type Props = {
440
- name: string ;
441
- };
437
+ For ** Class components** , there are [ a couple ways to do it] ( https://.com/sw-yx/react-typescript-cheatsheet/pull/103#issuecomment-481061483 ) (including using the ` Pick ` utility type) but the recommendation is to "reverse" the props definition:
442
438
443
- export class Greet extends React .Component <Props > {
444
- render() {
445
- const { name } = this .props ;
446
- return <div >Hello ${ name .toUpperCase ()} !</div >;
439
+ ``` tsx
440
+ type GreetProps = typeof Greet .defaultProps & {
441
+ age: number
442
+ }
443
+
444
+ class Greet extends React .Component <GreetProps > {
445
+ static defaultProps = {
446
+ name: ' world'
447
447
}
448
- static defaultProps : Partial < Props > = { name: ' world ' };
448
+ /* ... */
449
449
}
450
450
451
451
// Type-checks! No type assertions needed!
452
- let el = <Greet />;
452
+ let el = <Greet age = { 3 } />;
453
453
```
454
454
<details >
455
455
<summary >Why does React.FC break defaultProps?</summary >
You can’t perform that action at this time.
0 commit comments