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
As per [this tweet](https://twitter.com/dan_abramov/status/1133878326358171650), defaultProps will eventually be deprecated. You can check the discussions here:
8
+
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:
16
18
17
19
```tsx
18
20
typeGreetProps= { age?:number };
19
21
20
-
const Greet = ({ age=21 }:GreetProps) =>// etc
22
+
const Greet = ({ age=21 }:GreetProps) => {
23
+
return <div>{`Hello, I am ${age} years old.`}</div>;
24
+
};
21
25
```
22
26
23
-
Class Components:
27
+
### Class Components: `defaultProps` Still Supported
28
+
29
+
For class components, `defaultProps` is still a valid way to set default values:
24
30
25
31
```tsx
26
32
typeGreetProps= {
27
33
age?:number;
28
34
};
29
35
30
36
classGreetextendsReact.Component<GreetProps> {
37
+
static defaultProps = {
38
+
age: 21,
39
+
};
40
+
31
41
render() {
32
-
const { age =21 } =this.props;
33
-
/*...*/
42
+
return <div>{`Hello, I am ${this.props.age} years old.`}</div>;
34
43
}
35
44
}
36
45
37
46
let el = <Greetage={3} />;
38
47
```
39
48
40
-
## Typing `defaultProps`
49
+
## Typing `defaultProps` in TypeScript
41
50
42
-
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).
51
+
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.
43
52
44
-
**Function Components**
53
+
### Function Components
45
54
46
55
```tsx
47
-
// using typeof as a shortcut; note that it hoists!
48
-
// you can also declare the type of DefaultProps if you choose
49
-
// e.g. https://github.com/typescript-cheatsheets/react/issues/415#issuecomment-841223219
return <div>{`Hello, I am ${props.age} years old.`}</div>;
58
64
};
65
+
59
66
Greet.defaultProps=defaultProps;
60
67
```
61
68
62
-
_[See this in TS Playground](https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAKjgQwM5wEoFNkGN4BmUEIcARFDvmQNwBQdMAnmFnAOKVYwAKxY6ALxwA3igDmWAFxwAdgFcQAIyxQ4AXzgAyOM1YQCcACZYCyeQBte-VPVwRZqeCbOXrEAXGEi6cCdLgAJgBGABo6dXo6e0d4TixuLzgACjAbGXjuPg9UAEovAD5RXzhKGHkoWTgAHiNgADcCkTScgDpkSTgAeiQFZVVELvVqrrrGiPpMmFaXcytsz2FZtwXbOiA)_
63
-
64
-
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>
84
-
85
-
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`.
86
-
87
-
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.
107
-
108
-
</details>
109
-
110
-
## Consuming Props of a Component with defaultProps
111
-
112
-
A component with `defaultProps` may seem to have some required props that actually aren't.
113
-
114
-
### Problem Statement
115
-
116
-
Here's what you want to do:
117
-
118
-
```tsx
119
-
interfaceIProps {
120
-
name:string;
121
-
}
122
-
const defaultProps = {
123
-
age: 25,
124
-
};
125
-
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)
158
-
159
-
## Misc Discussions and Knowledge
160
-
161
-
<details>
162
-
<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>;
81
+
}
188
82
}
189
83
```
190
84
191
-
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.
87
+
For more advanced use cases, you can explore the following links:
207
88
208
-
[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).
211
92
212
-
[Something to add? File an issue](https://github.com/typescript-cheatsheets/react/issues/new).
0 commit comments