File tree 5 files changed +90
-5
lines changed
5 files changed +90
-5
lines changed Original file line number Diff line number Diff line change
1
+ export const ADD_COUNTER = 'ADD_COUNTER' ;
2
+ export const REMOVE_COUNTER = 'REMOVE_COUNTER' ;
3
+
4
+ export function add ( ) {
5
+ return {
6
+ type : ADD_COUNTER
7
+ } ;
8
+ }
9
+
10
+ export function remove ( ) {
11
+ return {
12
+ type : REMOVE_COUNTER
13
+ } ;
14
+ }
Original file line number Diff line number Diff line change
1
+ import React , { Component , PropTypes } from 'react' ;
2
+ import Counter from './Counter' ;
3
+
4
+ class List extends Component {
5
+
6
+ increment ( index ) {
7
+ let action = this . props . counterActions . increment ( )
8
+ action . index = index
9
+ this . props . dispatch ( action )
10
+ }
11
+
12
+ decrement ( index ) {
13
+ let action = this . props . counterActions . decrement ( )
14
+ action . index = index
15
+ this . props . dispatch ( action )
16
+ }
17
+
18
+ render ( ) {
19
+ const { add, remove, counterList, counterActions } = this . props ;
20
+ return (
21
+ < p >
22
+ { ' ' }
23
+ < button onClick = { add } > Add counter</ button >
24
+ { ' ' }
25
+ < button onClick = { remove } > Remove counter</ button >
26
+ { counterList . map ( ( counter , counterId ) => {
27
+ return < Counter key = { counterId } id = { counterId } counter = { counter } increment = { this . increment . bind ( this , counterId ) } decrement = { this . decrement . bind ( this , counterId ) } > </ Counter >
28
+ } ) }
29
+ </ p >
30
+ ) ;
31
+ }
32
+ }
33
+
34
+ List . propTypes = {
35
+ add : PropTypes . func . isRequired ,
36
+ remove : PropTypes . func . isRequired ,
37
+ counterList : PropTypes . array . isRequired ,
38
+ dispatch : PropTypes . func . isRequired
39
+ } ;
40
+
41
+ export default List ;
Original file line number Diff line number Diff line change 1
1
import { bindActionCreators } from 'redux' ;
2
2
import { connect } from 'react-redux' ;
3
- import Counter from '../components/Counter ' ;
3
+ import List from '../components/List ' ;
4
4
import * as CounterActions from '../actions/counter' ;
5
+ import * as ListActions from '../actions/list' ;
5
6
6
7
function mapStateToProps ( state ) {
7
8
return {
8
- counter : state . counter
9
+ counterList : state . counterList
9
10
} ;
10
11
}
11
12
12
13
function mapDispatchToProps ( dispatch ) {
13
- return bindActionCreators ( CounterActions , dispatch ) ;
14
+ let actions = bindActionCreators ( ListActions , dispatch ) ;
15
+ actions . counterActions = bindActionCreators ( CounterActions , dispatch ) ;
16
+ actions . dispatch = dispatch
17
+ return actions ;
14
18
}
15
19
16
- export default connect ( mapStateToProps , mapDispatchToProps ) ( Counter ) ;
20
+ export default connect ( mapStateToProps , mapDispatchToProps ) ( List ) ;
Original file line number Diff line number Diff line change 1
1
import { combineReducers } from 'redux' ;
2
2
import counter from './counter' ;
3
+ import list from './list'
3
4
4
5
const rootReducer = combineReducers ( {
5
- counter
6
+ counterList : list ( counter , {
7
+ add : 'ADD_COUNTER' ,
8
+ remove : 'REMOVE_COUNTER'
9
+ } )
6
10
} ) ;
7
11
8
12
export default rootReducer ;
Original file line number Diff line number Diff line change
1
+ export default function list ( reducer , actionTypes ) {
2
+ return function ( state = [ ] , action ) {
3
+ switch ( action . type ) {
4
+ case actionTypes . add :
5
+ return [ ...state , reducer ( undefined , action ) ] ;
6
+ case actionTypes . remove :
7
+ return [ ...state . slice ( 0 , action . index ) , ...state . slice ( action . index + 1 ) ] ;
8
+ default :
9
+ const { index, ...rest } = action ;
10
+ if ( typeof index !== 'undefined' ) {
11
+ return state . map ( ( item , i ) => {
12
+ if ( index == i ) {
13
+ return reducer ( item , rest )
14
+ } else {
15
+ return item
16
+ }
17
+ } ) ;
18
+ }
19
+ return state ;
20
+ }
21
+ }
22
+ }
You can’t perform that action at this time.
0 commit comments