Skip to content

Commit 4b46316

Browse files
committed
updated class component default props pattern
1 parent 8bd2809 commit 4b46316

18 files changed

+114
-120
lines changed

README.md

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ You should check Playground Project located in the `/playground` folder. It is a
1919
- [React Types Cheatsheet](#react-types-cheatsheet) 🌟 __NEW__
2020
- [Component Typing Patterns](#component-typing-patterns)
2121
- [Stateless Components - SFC](#stateless-components---sfc)
22-
- [Stateful Components - Class](#stateful-components---class)
22+
- [Stateful Components - Class](#stateful-components---class) 📝 __UPDATED__
2323
- [Generic Components](#generic-components)
2424
- [Higher-Order Components](#higher-order-components) 📝 __UPDATED__
2525
- [Redux Connected Components](#redux-connected-components)
@@ -239,7 +239,7 @@ export class StatefulCounter extends React.Component<StatefulCounterProps, State
239239
```tsx
240240
import * as React from 'react';
241241

242-
export interface StatefulCounterWithInitialCountProps {
242+
export interface StatefulCounterWithDefaultProps {
243243
label: string;
244244
initialCount?: number;
245245
}
@@ -248,53 +248,52 @@ interface DefaultProps {
248248
initialCount: number;
249249
}
250250

251-
type PropsWithDefaults = StatefulCounterWithInitialCountProps & DefaultProps;
252-
253251
interface State {
254252
count: number;
255253
}
256254

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+
};
264261

265-
state: State = {
266-
count: this.props.initialCount,
267-
};
262+
props: StatefulCounterWithDefaultProps & DefaultProps;
268263

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+
};
274267

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 });
277271
}
272+
}
278273

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+
}
283277

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+
}
294293

295294
```
296295
297-
[⟩⟩⟩ demo](https://piotrwitek.github.io/react-redux-typescript-guide/#statefulcounterwithinitialcount)
296+
[⟩⟩⟩ demo](https://piotrwitek.github.io/react-redux-typescript-guide/#statefulcounterwithdefault)
298297
299298
[⇧ back to top](#table-of-contents)
300299

docs/build/0.7272607c.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/build/0.8d8a3f08.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/build/bundle.0b1b071d.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/build/bundle.5da79f21.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
</head>
88
<body>
99
<div id="app"></div>
10-
<script type="text/javascript" src="build/bundle.0b1b071d.js"></script></body>
10+
<script type="text/javascript" src="build/bundle.5da79f21.js"></script></body>
1111
</html>

docs/markdown/1_react.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ const handleChange: React.ReactEventHandler<HTMLInputElement> = (ev) => { ...
8787
8888
#### - with default props
8989
90-
::example='../../playground/src/components/stateful-counter-with-initial-count.tsx'::
90+
::example='../../playground/src/components/stateful-counter-with-default.tsx'::
9191
92-
[⟩⟩⟩ demo](https://piotrwitek.github.io/react-redux-typescript-guide/#statefulcounterwithinitialcount)
92+
[⟩⟩⟩ demo](https://piotrwitek.github.io/react-redux-typescript-guide/#statefulcounterwithdefault)
9393
9494
[⇧ back to top](#table-of-contents)
9595

docs/markdown/_toc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
- [React Types Cheatsheet](#react-types-cheatsheet) 🌟 __NEW__
44
- [Component Typing Patterns](#component-typing-patterns)
55
- [Stateless Components - SFC](#stateless-components---sfc)
6-
- [Stateful Components - Class](#stateful-components---class)
6+
- [Stateful Components - Class](#stateful-components---class) 📝 __UPDATED__
77
- [Generic Components](#generic-components)
88
- [Higher-Order Components](#higher-order-components) 📝 __UPDATED__
99
- [Redux Connected Components](#redux-connected-components)

playground/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"reselect": "3.0.1",
3131
"rxjs": "5.5.6",
3232
"tslib": "1.8.1",
33-
"typesafe-actions": "1.0.0",
33+
"typesafe-actions": "1.1.0",
3434
"uuid": "3.1.0"
3535
},
3636
"devDependencies": {
@@ -46,7 +46,7 @@
4646
"react-styleguidist": "6.1.0",
4747
"tslint": "5.8.0",
4848
"tslint-react": "3.2.0",
49-
"typescript": "2.7.0-dev.20171216",
49+
"typescript": "2.6.2",
5050
"webpack": "3.10.0",
5151
"webpack-blocks": "1.0.0-rc.2"
5252
}

playground/src/components/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ export * from './generic-list';
33
export * from './sfc-counter';
44
export * from './sfc-spread-attributes';
55
export * from './stateful-counter';
6-
export * from './stateful-counter-with-initial-count';
6+
export * from './stateful-counter-with-default';

0 commit comments

Comments
 (0)