You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -846,213 +846,94 @@ class Comp extends React.PureComponent<Props, State> {
846
846
847
847
<!--START-SECTION:default-props-->
848
848
849
-
#### You May Not Need `defaultProps`
849
+
#### You May Not Need `defaultProps` Anymore
850
850
851
-
As per [this tweet](https://twitter.com/dan_abramov/status/1133878326358171650), defaultProps will eventually be deprecated. You can check the discussions here:
851
+
According to [Dan Abramov's tweet](https://twitter.com/dan_abramov/status/1133878326358171650), `defaultProps` is on the path to deprecation for function components. Instead, it is recommended to use default values directly in the function component signature for simplicity.
Instead of using `defaultProps`, you can define default values inline for function components:
859
861
860
862
```tsx
861
863
typeGreetProps= { age?:number };
862
864
863
-
const Greet = ({ age=21 }:GreetProps) =>// etc
865
+
const Greet = ({ age=21 }:GreetProps) => {
866
+
return <div>{`Hello, I am ${age} years old.`}</div>;
867
+
};
864
868
```
865
869
866
-
Class Components:
870
+
##### Class Components: `defaultProps` Still Supported
871
+
872
+
For class components, `defaultProps` is still a valid way to set default values:
867
873
868
874
```tsx
869
875
typeGreetProps= {
870
876
age?:number;
871
877
};
872
878
873
879
classGreetextendsReact.Component<GreetProps> {
880
+
static defaultProps = {
881
+
age: 21,
882
+
};
883
+
874
884
render() {
875
-
const { age =21 } =this.props;
876
-
/*...*/
885
+
return <div>{`Hello, I am ${this.props.age} years old.`}</div>;
877
886
}
878
887
}
879
888
880
889
let el = <Greetage={3} />;
881
890
```
882
891
883
-
#### Typing `defaultProps`
892
+
#### Typing `defaultProps` in TypeScript
884
893
885
-
Type inference improved greatly for `defaultProps`in [TypeScript 3.0+](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html), although [some edge cases are still problematic](https://github.com/typescript-cheatsheets/react/issues/61).
894
+
For TypeScript 3.0 and later, type inference for `defaultProps`has improved. Below is how you can properly type `defaultProps` for both function and class components.
886
895
887
-
**Function Components**
896
+
##### Function Components
888
897
889
898
```tsx
890
-
// using typeof as a shortcut; note that it hoists!
891
-
// you can also declare the type of DefaultProps if you choose
892
-
// e.g. https://github.com/typescript-cheatsheets/react/issues/415#issuecomment-841223219
return <div>{`Hello, I am ${props.age} years old.`}</div>;
901
907
};
908
+
902
909
Greet.defaultProps=defaultProps;
903
910
```
904
911
905
-
_[See this in TS Playground](https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAKjgQwM5wEoFNkGN4BmUEIcARFDvmQNwBQdMAnmFnAOKVYwAKxY6ALxwA3igDmWAFxwAdgFcQAIyxQ4AXzgAyOM1YQCcACZYCyeQBte-VPVwRZqeCbOXrEAXGEi6cCdLgAJgBGABo6dXo6e0d4TixuLzgACjAbGXjuPg9UAEovAD5RXzhKGHkoWTgAHiNgADcCkTScgDpkSTgAeiQFZVVELvVqrrrGiPpMmFaXcytsz2FZtwXbOiA)_
906
-
907
-
For **Class components**, there are [a couple ways to do it](https://github.com/typescript-cheatsheets/react/pull/103#issuecomment-481061483) (including using the `Pick` utility type) but the recommendation is to "reverse" the props definition:
<summary><b><code>React.JSX.LibraryManagedAttributes</code> nuance for library authors</b></summary>
927
-
928
-
The above implementations work fine for App creators, but sometimes you want to be able to export `GreetProps` so that others can consume it. The problem here is that the way `GreetProps` is defined, `age` is a required prop when it isn't because of `defaultProps`.
929
-
930
-
The insight to have here is that [`GreetProps` is the _internal_ contract for your component, not the _external_, consumer facing contract](https://github.com/typescript-cheatsheets/react/issues/66#issuecomment-453878710). You could create a separate type specifically for export, or you could make use of the `React.JSX.LibraryManagedAttributes` utility:
This will work properly, although hovering over`ApparentGreetProps`may be a little intimidating. You can reduce this boilerplate with the`ComponentProps` utility detailed below.
950
-
951
-
</details>
952
-
953
-
#### Consuming Props of a Component with defaultProps
954
-
955
-
A component with `defaultProps` may seem to have some required props that actually aren't.
956
-
957
-
##### Problem Statement
958
-
959
-
Here's what you want to do:
960
-
961
-
```tsx
962
-
interfaceIProps {
963
-
name:string;
964
-
}
965
-
const defaultProps = {
966
-
age: 25,
967
-
};
968
-
const GreetComponent = ({ name, age }:IProps&typeofdefaultProps) => (
[_See this in TS Playground_](https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAKjgQwM5wEoFNkGN4BmUEIcARFDvmQNwBQdMAnmFnAMImQB2W3MABWJhUAHgAqAPjgBeOOLhYAHjD4ATdNjwwAdJ3ARe-cSyyjg3AlihwB0gD6Yqu-Tz4xzl67cl04cAH44ACkAZQANHQAZYAAjKGQoJgBZZG5kAHMsNQBBGBgoOIBXVTFxABofPzgALjheADdrejoLVSgCPDYASSEIETgAb2r0kCw61AKLDPoAXzpcQ0m4NSxOooAbQWF0OWH-TPG4ACYAVnK6WfpF7mWAcUosGFdDd1k4AApB+uQxysO4LM6r0dnAAGRwZisCAEFZrZCbbb9VAASlk0g+1VEamADUkgwABgAJLAbDYQSogJg-MZwYDoAAkg1GWFmlSZh1mBNmogA9Di8XQUfQHlgni8jLpVustn0BnJpQjZTsWrzeXANsh2gwbstxFhJhK3nIPmAdnUjfw5WIoVgYXBReKuK9+JI0TJpPs4JQYEUoNw4KIABYARjgvN8VwYargADkIIooMQoAslvBSe8JAbns7JTSsDIyAQIBAyOHJDQgA)
1001
-
1002
-
#### Misc Discussions and Knowledge
1003
-
1004
-
<details>
1005
-
<summary><b>Why does <code>React.FC</code> break <code>defaultProps</code>?</b></summary>
return <div>{`Hello, I am ${this.props.age} years old.`}</div>;
924
+
}
1031
925
}
1032
926
```
1033
927
1034
-
Our former recommendation used the `Partial type` feature in TypeScript, which means that the current interface will fulfill a partial version on the wrapped interface. In that way we can extend defaultProps without any changes in the types!
The problem with this approach is it causes complex issues with the type inference working with `React.JSX.LibraryManagedAttributes`. Basically it causes the compiler to think that when creating a JSX expression with that component, that all of its props are optional.
930
+
For more advanced use cases, you can explore the following links:
1050
931
1051
-
[See commentary by @ferdaber here](https://github.com/typescript-cheatsheets/react/issues/57) and [here](https://github.com/typescript-cheatsheets/react/issues/61).
[_See this example in TS Playground_](https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAKjgQwM5wEoFNkGN4BmUEIcARFDvmQNwBQdMAnmFnAOKVYwAKxY6ALxwA3igDmWAFxwAdgFcQAIyxQ4AXzgAyOM1YQCcACZYCyeQBte-VPVwRZqeCbOXrEAXGEi6cCdLgAJgBGABo6dXo6e0d4TixuLzgACjAbGXjuPg9UAEovAD5RXzhKGHkoWTgAHiNgADcCkTScgDpkSTgAeiQFZVVELvVqrrrGiPpMmFaXcytsz2FZtwXbOiA).
1054
935
1055
-
[Something to add? File an issue](https://github.com/typescript-cheatsheets/react/issues/new).
0 commit comments