mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-12 11:51:10 +08:00
33 lines
712 B
TypeScript
33 lines
712 B
TypeScript
/// <reference path="./redux.d.ts" />
|
|
|
|
function counter(state: any, action: any) {
|
|
if (!state) {
|
|
state = 0;
|
|
}
|
|
switch (action.type) {
|
|
case 'INCREMENT':
|
|
return state + 1;
|
|
case 'DECREMENT':
|
|
return state - 1;
|
|
default:
|
|
return state;
|
|
}
|
|
}
|
|
|
|
function loggingMiddleware() {
|
|
return (next: Redux.Dispatch) => (action: any) => {
|
|
console.log(action.type);
|
|
next(action);
|
|
};
|
|
}
|
|
|
|
let createStoreWithMiddleware = Redux.applyMiddleware(loggingMiddleware)(Redux.createStore);
|
|
let store = createStoreWithMiddleware(counter);
|
|
|
|
|
|
store.subscribe(() =>
|
|
console.log(store.getState())
|
|
);
|
|
|
|
store.dispatch({ type: 'INCREMENT' });
|