@@ -65,7 +65,7 @@ type Props = {
65
65
style? : React .CSSProperties ,
66
66
};
67
67
68
- const MyComponent : React .SFC <Props > = (props ) => {
68
+ const StatelessComponent : React .SFC <Props > = (props ) => {
69
69
const { children, ... restProps } = props ;
70
70
return (
71
71
<div { ... restProps } >
@@ -74,7 +74,7 @@ const MyComponent: React.SFC<Props> = (props) => {
74
74
);
75
75
};
76
76
77
- export default MyComponent ;
77
+ export default StatelessComponent ;
78
78
```
79
79
80
80
---
@@ -85,49 +85,44 @@ export default MyComponent;
85
85
import * as React from ' react' ;
86
86
87
87
type Props = {
88
- className? : string ,
89
- style? : React .CSSProperties ,
90
88
initialCount? : number ,
91
89
};
92
90
93
91
type State = {
94
- counter : number ,
92
+ count : number ,
95
93
};
96
94
97
- class MyComponent extends React .Component <Props , State > {
98
- // default props using Property Initializers
95
+ class ClassComponent extends React .Component <Props , State > {
99
96
static defaultProps: Partial <Props > = {
100
- className: ' default-class ' ,
97
+ initialCount: 0 ,
101
98
};
102
99
103
- // initial state using Property Initializers
104
100
state: State = {
105
- counter : this .props .initialCount || 0 ,
101
+ count : this .props .initialCount ! ,
106
102
};
107
103
108
- // lifecycle methods should be declared as normal instance methods and it's fine
104
+ // declare lifecycle methods as normal instance methods
109
105
componentDidMount() {
110
106
// tslint:disable-next-line:no-console
111
107
console .log (' Mounted!' );
112
108
}
113
109
114
- // handlers using Class Fields with arrow functions
115
- increaseCounter = () => { this .setState ({ counter : this .state .counter + 1 }); };
110
+ // declare handlers as Class Fields arrow functions
111
+ handleIncrement = () => { this .setState ({ count : this .state .count + 1 }); };
116
112
117
113
render() {
118
- const { children, initialCount, ... restProps } = this .props ;
114
+ const { count } = this .state ;
119
115
120
116
return (
121
- <div { ... restProps } onClick = { this .increaseCounter } >
122
- Clicks: { this .state .counter }
123
- <hr />
124
- { children }
125
- </div >
117
+ <section >
118
+ <div >{ count } </div >
119
+ <button onClick = { this .handleIncrement } >Increment</button >
120
+ </section >
126
121
);
127
122
}
128
123
}
129
124
130
- export default MyComponent ;
125
+ export default ClassComponent ;
131
126
```
132
127
133
128
---
@@ -159,35 +154,19 @@ interface IUser {
159
154
name: string ,
160
155
}
161
156
162
- type UserItemProps = {
163
- item: IUser ,
164
- };
165
-
166
- const UserItem: React .SFC <UserItemProps > = ({item }) => {
167
- const {name, id} = item ;
168
-
169
- return (
170
- <div >
171
- <b >{ name } </b >
172
- <small >({ id } )</small >
173
- </div >
174
- );
175
- }
176
- ```
177
-
178
- ``` tsx
179
- const UserList = class extends GenericList <IUser > { };
180
-
181
- const users = [{
157
+ const users: IUser [] = [{
182
158
id: ' uuidv4' ,
183
159
name: ' Dude' ,
184
160
}];
185
161
162
+ const UserList = class extends GenericList <IUser > { };
163
+
164
+ // notice that "items" and "itemRenderer" will infer "IUser" type and guard type errors
186
165
ReactDOM .render (
187
166
<UserList
188
167
items = { users }
189
- itemRenderer = { (item ) => <UserItem key = { item .id } item = { item } / >} // <- notice that "items" and "itemRenderer" has inferred "IUser" type
190
- >
168
+ itemRenderer = { (item ) => <div key = { item .id } > { item . name } </ div >}
169
+ >
191
170
</UserList >
192
171
);
193
172
```
@@ -223,9 +202,8 @@ class CounterContainer extends React.Component<Props, {}> {
223
202
return (
224
203
<section >
225
204
<h2 >{ title } </h2 >
226
- <input type = " number " value = { counter } / >
205
+ <div > { counter } </ div >
227
206
<button onClick = { this .handleIncrement } >Increment</button >
228
- ...
229
207
</section >
230
208
);
231
209
}
0 commit comments