Skip to content

Commit 928b59b

Browse files
committed
updated examples
1 parent d9faef7 commit 928b59b

File tree

11 files changed

+73
-53
lines changed

11 files changed

+73
-53
lines changed

examples/package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
"repository": "https://github.com/piotrwitek/react-redux-typescript-guide.git",
66
"license": "MIT",
77
"scripts": {
8-
"rei": "rm -rf node_modules && yarn install",
9-
"clear": "rm -rf out",
10-
"build": "npm run clear && tsc -p ./",
11-
"check": "npm run tsc & npm run lint",
8+
"prereinstall": "rm -rf node_modules",
9+
"reinstall": "yarn install",
10+
"prebuild": "rm -rf out",
11+
"build": "tsc -p ./",
12+
"pretest": "npm run tsc & npm run lint",
13+
"test": "echo \"Don't worry about tests yet!\"",
1214
"tsc": "tsc -p ./ --noEmit",
1315
"lint": "tslint -p ./"
1416
},

examples/src/components/class-component.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,3 @@ export class ClassComponent extends React.Component<ClassComponentProps, State>
4747
);
4848
}
4949
}
50-
51-
export default ClassComponent;

examples/src/components/stateless-component.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,3 @@ export const StatelessComponent: React.StatelessComponent<StatelessComponentProp
2424
</div>
2525
);
2626
};
27-
28-
export default StatelessComponent;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Action Types - could be kept in separate file...
2+
export const INCREASE_COUNTER = 'INCREASE_COUNTER';
3+
export const CHANGE_BASE_CURRENCY = 'CHANGE_BASE_CURRENCY';
4+
5+
// Action Creators
6+
export default {
7+
increaseCounter: () => ({
8+
type: INCREASE_COUNTER as typeof INCREASE_COUNTER,
9+
}),
10+
changeBaseCurrency: (payload: string) => ({
11+
type: CHANGE_BASE_CURRENCY as typeof CHANGE_BASE_CURRENCY, payload,
12+
}),
13+
};
Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,3 @@
1-
import { RootAction } from '../../types';
2-
3-
// Action Types
4-
export const INCREASE_COUNTER = 'INCREASE_COUNTER';
5-
export const CHANGE_BASE_CURRENCY = 'CHANGE_BASE_CURRENCY';
6-
7-
// Action Creators
8-
export const actionCreators = {
9-
increaseCounter: () => ({
10-
type: INCREASE_COUNTER as typeof INCREASE_COUNTER,
11-
}),
12-
changeBaseCurrency: (payload: string) => ({
13-
type: CHANGE_BASE_CURRENCY as typeof CHANGE_BASE_CURRENCY, payload,
14-
}),
15-
};
16-
17-
// State
18-
export type State = {
19-
readonly counter: number,
20-
readonly baseCurrency: string,
21-
};
22-
export const initialState: State = {
23-
counter: 0,
24-
baseCurrency: 'EUR',
25-
};
26-
27-
// Reducer
28-
export function reducer(state: State = initialState, action: RootAction): State {
29-
switch (action.type) {
30-
case INCREASE_COUNTER:
31-
return {
32-
...state, counter: state.counter + 1, // no payload
33-
};
34-
case CHANGE_BASE_CURRENCY:
35-
return {
36-
...state, baseCurrency: action.payload, // payload: string
37-
};
38-
39-
default: return state;
40-
}
41-
}
1+
// public API
2+
export { default as actionCreators } from './actions';
3+
export { default as reducer } from './reducer';
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { RootAction } from '../../types';
2+
import {
3+
INCREASE_COUNTER,
4+
CHANGE_BASE_CURRENCY,
5+
} from './actions';
6+
7+
// State
8+
export type State = {
9+
readonly counter: number,
10+
readonly baseCurrency: string,
11+
};
12+
export const initialState: State = {
13+
counter: 0,
14+
baseCurrency: 'EUR',
15+
};
16+
17+
// Reducer
18+
export default function reducer(state: State = initialState, action: RootAction): State {
19+
switch (action.type) {
20+
case INCREASE_COUNTER:
21+
return {
22+
...state, counter: state.counter + 1, // no payload
23+
};
24+
case CHANGE_BASE_CURRENCY:
25+
return {
26+
...state, baseCurrency: action.payload, // payload: string
27+
};
28+
29+
default: return state;
30+
}
31+
}

examples/src/types/globals.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
declare interface Window {
2+
__REDUX_DEVTOOLS_EXTENSION__: any;
3+
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__: any;
4+
}
5+
6+
declare interface NodeModule {
7+
hot?: { accept: (path: string, callback: () => void) => void };
8+
}
9+
10+
declare interface System {
11+
import<T = any>(module: string): Promise<T>
12+
}
13+
declare var System: System;

examples/src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './root-action';
22
export * from './root-state';
3+
export * from './store';

examples/src/types/root-action.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { returntypeof } from 'react-redux-typescript';
2-
import { Dispatch } from 'redux';
32

43
import { actionCreators as converterActionCreators } from '../modules/converter';
54

5+
// merging actions returned from all action creators
66
const actions = Object.values({
77
...converterActionCreators,
88
}).map(returntypeof);
99

1010
export type RootAction = typeof actions[number];
11-
12-
export type Dispatch = Dispatch<RootAction>;

examples/src/types/root-state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { State as ConverterState } from '../modules/converter';
1+
import { State as ConverterState } from '../modules/converter/reducer';
22

33
export type RootState = {
44
converter: ConverterState;

0 commit comments

Comments
 (0)