Skip to content

Commit 5dc930b

Browse files
committed
refactored examples
1 parent dfbdfec commit 5dc930b

File tree

9 files changed

+36
-12
lines changed

9 files changed

+36
-12
lines changed

examples/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<title>React, Redux, Typescript Guide</title>
88
</head>
99
<body>
10+
<div id="app"></div>
11+
1012
<script src="/out/index.js"></script>
1113
</body>
1214
</html>

examples/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
{
2+
"name": "react-redux-typescript-guide",
3+
"version": "1.0.0",
4+
"author": "Piotr Witek <[email protected]> (http://piotrwitek.github.io/)",
5+
"repository": "https://github.com/piotrwitek/react-redux-typescript-guide.git",
6+
"license": "MIT",
27
"scripts": {
38
"rei": "rm -rf node_modules && yarn install",
49
"clear": "rm -rf out",
510
"build": "npm run clear && tsc -p ./",
11+
"check": "npm run tsc & npm run lint",
612
"tsc": "tsc -p ./ --noEmit",
713
"lint": "tslint -p ./"
814
},

examples/src/app.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import * as React from 'react';
22
import { render } from 'react-dom';
33

4-
import { ConnectedComponent } from './containers';
4+
import { StatelessComponent, ClassComponent } from './components';
5+
import { ConnectedStatelessComponent } from './containers';
56

6-
render(
7-
<ConnectedComponent label="Primary" />,
8-
document.getElementById('app'),
7+
export const App = (
8+
<div>
9+
<ClassComponent label="ClassComponent" initialCount={10} />
10+
<ConnectedStatelessComponent label="ConnectedStatelessComponent" />
11+
</div>
912
);

examples/src/components/class-component.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import * as React from 'react';
33
type Props = {
44
className?: string,
55
style?: React.CSSProperties,
6+
label: string,
67
initialCount?: number,
7-
title: string,
88
};
99

1010
type State = {
@@ -29,15 +29,20 @@ export class ClassComponent extends React.Component<Props, State> {
2929
}
3030

3131
// handlers using Class Fields with arrow functions
32-
increaseCounter = () => { this.setState({ counter: this.state.counter + 1 }); };
32+
handleIncrement = () => { this.setState({ counter: this.state.counter + 1 }); };
3333

3434
render() {
35-
const { children, title, initialCount, ...restProps } = this.props;
35+
const { children, label, initialCount, ...restProps } = this.props;
36+
const { counter } = this.state;
3637

3738
return (
38-
<div {...restProps} onClick={this.increaseCounter} >
39-
<h2>{title}</h2>
40-
Clicks: {this.state.counter}
39+
<div {...restProps} >
40+
<div>
41+
{label}: {counter}
42+
<button type="button" onClick={this.handleIncrement}>
43+
Increment
44+
</button>
45+
</div>
4146
<hr />
4247
{children}
4348
</div>

examples/src/components/stateless-component.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type Props = {
1010

1111
export const StatelessComponent: React.StatelessComponent<Props> = (props) => {
1212
const { children, label, counter, incrementCounter, ...restProps } = props;
13+
1314
const handleIncrement = () => { incrementCounter(); };
1415

1516
return (
@@ -20,6 +21,7 @@ export const StatelessComponent: React.StatelessComponent<Props> = (props) => {
2021
Increment
2122
</button>
2223
</div>
24+
<hr />
2325
{children}
2426
</div>
2527
);

examples/src/containers/connected-component.tsx renamed to examples/src/containers/connected-stateless-component.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ const dispatchProps = {
1818
increaseCounter: actionCreators.increaseCounter,
1919
};
2020

21-
export const ConnectedComponent = connect(mapStateToProps, dispatchProps)(StatelessComponent);
21+
export const ConnectedStatelessComponent = connect(mapStateToProps, dispatchProps)(StatelessComponent);

examples/src/containers/home.tsx

Whitespace-only changes.

examples/src/containers/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './connected-component';
1+
export * from './connected-stateless-component';

examples/src/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as React from 'react';
2+
import { render } from 'react-dom';
3+
4+
import { App } from './app';
5+
6+
render(App, document.getElementById('app'));

0 commit comments

Comments
 (0)