@@ -2,38 +2,47 @@ import * as React from 'react';
2
2
import * as ReactDOM from 'react-dom' ;
3
3
import { Provider , connect } from 'react-redux' ;
4
4
import PropTypes from 'prop-types' ;
5
- import { createStore , applyMiddleware } from 'redux' ;
5
+ import { createStore , applyMiddleware , combineReducers } from 'redux' ;
6
6
import logger from 'redux-logger' ;
7
7
8
8
9
9
// ACTIONS:
10
10
11
- const INCREMENT_ACTION = 'INCREMENT_ACTION ' ;
12
- function incrementAction ( ) {
11
+ const ACTION_1 = 'ACTION_1 ' ;
12
+ function action1 ( ) {
13
13
return {
14
- type : INCREMENT_ACTION
14
+ type : ACTION_1
15
15
} ;
16
16
}
17
17
18
- const DECREMENT_ACTION = 'DECREMENT_ACTION ' ;
19
- function decrementAction ( ) {
18
+ const ACTION_2 = 'ACTION_2 ' ;
19
+ function action2 ( ) {
20
20
return {
21
- type : DECREMENT_ACTION
21
+ type : ACTION_2
22
22
} ;
23
23
}
24
24
25
25
// REDUCERS:
26
26
27
- const INITIAL_STATE = 2 ;
28
-
29
- function rootReducer ( state = INITIAL_STATE , action ) {
30
- const STEP = 1 ;
27
+ function foo ( state = { a : 1 } , action ) {
28
+ switch ( action . type ) {
29
+ case ACTION_1 :
30
+ return {
31
+ ...state ,
32
+ a : state . a + 1
33
+ } ;
34
+ default :
35
+ return state ;
36
+ }
37
+ }
31
38
39
+ function bar ( state = { b : 2 } , action ) {
32
40
switch ( action . type ) {
33
- case INCREMENT_ACTION :
34
- return state + STEP ;
35
- case DECREMENT_ACTION :
36
- return state - STEP ;
41
+ case ACTION_2 :
42
+ return {
43
+ ...state ,
44
+ b : state . b + 1
45
+ } ;
37
46
default :
38
47
return state ;
39
48
}
@@ -44,40 +53,40 @@ function rootReducer(state = INITIAL_STATE, action) {
44
53
class Counter$ extends React . Component {
45
54
constructor ( props ) {
46
55
super ( props ) ;
47
- this . onIncrementHandler = this . onIncrementHandler . bind ( this ) ;
48
- this . onDecrementHandler = this . onDecrementHandler . bind ( this ) ;
56
+ this . fireAction1 = this . fireAction1 . bind ( this ) ;
57
+ this . fireAction2 = this . fireAction2 . bind ( this ) ;
49
58
}
50
59
51
- onIncrementHandler ( ) {
52
- this . props . dispatch ( incrementAction ( ) ) ;
60
+ fireAction1 ( ) {
61
+ this . props . dispatch ( action1 ( ) ) ;
53
62
}
54
63
55
- onDecrementHandler ( ) {
56
- this . props . dispatch ( decrementAction ( ) ) ;
64
+ fireAction2 ( ) {
65
+ this . props . dispatch ( action2 ( ) ) ;
57
66
}
58
67
59
68
render ( ) {
60
69
return (
61
70
< div >
62
- < div > { this . props . counter } </ div >
63
- < button onClick = { this . onIncrementHandler } > increment</ button >
64
- < button onClick = { this . onDecrementHandler } > decrement</ button >
71
+ < div > { JSON . stringify ( this . props . foo ) } </ div >
72
+ < div > { JSON . stringify ( this . props . bar ) } </ div >
73
+ < button onClick = { this . fireAction1 } > fireAction1</ button >
74
+ < button onClick = { this . fireAction2 } > fireAction2</ button >
65
75
</ div >
66
76
) ;
67
77
}
68
78
}
69
79
70
80
Counter$ . propTypes = {
71
- dispatch : PropTypes . func . isRequired ,
72
- counter : PropTypes . number . isRequired
81
+ foo : PropTypes . shape ( ) . isRequired ,
82
+ bar : PropTypes . shape ( ) . isRequired ,
83
+ dispatch : PropTypes . func . isRequired
73
84
} ;
74
85
75
- const Counter = connect ( state => ( {
76
- counter : state
77
- } ) ) ( Counter$ ) ;
86
+ const Counter = connect ( ( { foo, bar} ) => ( { foo, bar} ) ) ( Counter$ ) ; // eslint-disable-line no-shadow
78
87
79
88
const store = createStore (
80
- rootReducer ,
89
+ combineReducers ( { foo , bar } ) ,
81
90
applyMiddleware ( logger )
82
91
) ;
83
92
0 commit comments