-
Notifications
You must be signed in to change notification settings - Fork 1
/
redux-example.js
43 lines (34 loc) · 997 Bytes
/
redux-example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict';
var redux = require('./redux.js');
function middleware (store) {
return function (dispatch) {
return function (action) {
console.log('hello middleware');
return dispatch(action);
};
};
}
var createStoreSubstitute = redux.applyMiddleware(middleware)(redux.createStore);
function reducer (state, action) {
if (action.type === 'one') return 'one';
return 'none';
}
function actionCreator () { return { type:'two' }; }
var store = createStoreSubstitute(reducer, 'start');
store.subscribe(function () { console.log('hello listener') });
store.dispatch({type:'one'});
console.log(store.getState());
// 'hello middleware'
// 'hello listener'
// -> 'one'
store.dispatch(actionCreator());
console.log(store.getState());
// 'hello middleware'
// 'hello listener'
// -> 'none'
var dispatchAction = redux.bindActionCreator(actionCreator,store.dispatch);
dispatchAction();
console.log(store.getState());
// 'hello middleware'
// 'hello listener'
// -> 'none'