@@ -19,7 +19,7 @@ You should check Playground Project located in the `/playground` folder. It is a
19
19
- [ React Types Cheatsheet] ( #react-types-cheatsheet ) 🌟 __ NEW__
20
20
- [ Component Typing Patterns] ( #component-typing-patterns )
21
21
- [ Stateless Components - SFC] ( #stateless-components---sfc )
22
- - [ Stateful Components - Class] ( #stateful-components---class )
22
+ - [ Stateful Components - Class] ( #stateful-components---class ) 📝 __ UPDATED __
23
23
- [ Generic Components] ( #generic-components )
24
24
- [ Higher-Order Components] ( #higher-order-components ) 📝 __ UPDATED__
25
25
- [ Redux Connected Components] ( #redux-connected-components )
@@ -239,7 +239,7 @@ export class StatefulCounter extends React.Component<StatefulCounterProps, State
239
239
` ` ` tsx
240
240
import * as React from ' react' ;
241
241
242
- export interface StatefulCounterWithInitialCountProps {
242
+ export interface StatefulCounterWithDefaultProps {
243
243
label: string ;
244
244
initialCount? : number ;
245
245
}
@@ -248,53 +248,52 @@ interface DefaultProps {
248
248
initialCount: number ;
249
249
}
250
250
251
- type PropsWithDefaults = StatefulCounterWithInitialCountProps & DefaultProps ;
252
-
253
251
interface State {
254
252
count: number ;
255
253
}
256
254
257
- export const StatefulCounterWithInitialCount: React .ComponentClass <StatefulCounterWithInitialCountProps > =
258
- class extends React .Component <PropsWithDefaults , State > {
259
- // to make defaultProps strictly typed we need to explicitly declare their type
260
- // @see https://github.com/DefinitelyTyped/DefinitelyTyped/issues/11640
261
- static defaultProps: DefaultProps = {
262
- initialCount: 0 ,
263
- };
255
+ export class StatefulCounterWithDefault extends React .Component <StatefulCounterWithDefaultProps , State > {
256
+ // to make defaultProps strictly typed we need to explicitly declare their type
257
+ // @see https://github.com/DefinitelyTyped/DefinitelyTyped/issues/11640
258
+ static defaultProps: DefaultProps = {
259
+ initialCount: 0 ,
260
+ };
264
261
265
- state: State = {
266
- count: this .props .initialCount ,
267
- };
262
+ props: StatefulCounterWithDefaultProps & DefaultProps ;
268
263
269
- componentWillReceiveProps({ initialCount }: PropsWithDefaults ) {
270
- if (initialCount != null && initialCount !== this .props .initialCount ) {
271
- this .setState ({ count: initialCount });
272
- }
273
- }
264
+ state: State = {
265
+ count: this .props .initialCount ,
266
+ };
274
267
275
- handleIncrement = () => {
276
- this .setState ({ count: this .state .count + 1 });
268
+ componentWillReceiveProps({ initialCount }: StatefulCounterWithDefaultProps ) {
269
+ if (initialCount != null && initialCount !== this .props .initialCount ) {
270
+ this .setState ({ count: initialCount });
277
271
}
272
+ }
278
273
279
- render() {
280
- const { handleIncrement } = this ;
281
- const { label } = this .props ;
282
- const { count } = this .state ;
274
+ handleIncrement = () => {
275
+ this .setState ({ count: this .state .count + 1 });
276
+ }
283
277
284
- return (
285
- <div >
286
- <span >{ label } : { count } </span >
287
- <button type = " button" onClick = { handleIncrement } >
288
- { ` Increment ` }
289
- </button >
290
- </div >
291
- );
292
- }
293
- };
278
+ render() {
279
+ const { handleIncrement } = this ;
280
+ const { label } = this .props ;
281
+ const { count } = this .state ;
282
+
283
+ return (
284
+ <div >
285
+ <span >{ label } : { count } </span >
286
+ <button type = " button" onClick = { handleIncrement } >
287
+ { ` Increment ` }
288
+ </button >
289
+ </div >
290
+ );
291
+ }
292
+ }
294
293
295
294
` ` `
296
295
297
- [⟩⟩⟩ demo](https://piotrwitek.github.io/react-redux-typescript-guide/#statefulcounterwithinitialcount )
296
+ [⟩⟩⟩ demo](https://piotrwitek.github.io/react-redux-typescript-guide/#statefulcounterwithdefault )
298
297
299
298
[⇧ back to top](#table-of-contents)
300
299
0 commit comments