@@ -9,7 +9,7 @@ import { rateColor, removeColor } from './actions';
9
9
import { sortFunction } from './lib/array-helpers' ;
10
10
11
11
12
- const ColorList = ( { store} ) => {
12
+ const ColorList = ( props , { store} ) => {
13
13
const { colors, sort} = store . getState ( ) ;
14
14
const sortedColors = [ ...colors ] . sort ( sortFunction ( sort ) ) ;
15
15
return ( < div className = "color-list" >
@@ -25,6 +25,10 @@ const ColorList = ({store}) => {
25
25
</ div > )
26
26
}
27
27
28
+ ColorList . contextTypes = {
29
+ store : PropTypes . object
30
+ }
31
+
28
32
29
33
class App extends Component {
30
34
getChildContext ( ) {
@@ -34,7 +38,7 @@ class App extends Component {
34
38
}
35
39
36
40
componentWillMount ( ) {
37
- this . unsubscribe = store . subscribe ( ( ) => this . forceUpdate ( ) )
41
+ this . unsubscribe = this . props . store . subscribe ( ( ) => this . forceUpdate ( ) )
38
42
}
39
43
40
44
componentWillUnmount ( ) {
@@ -46,55 +50,18 @@ class App extends Component {
46
50
this . state = {
47
51
colors : [ ]
48
52
}
49
-
50
- this . addColor = this . addColor . bind ( this ) ;
51
- this . rateColor = this . rateColor . bind ( this ) ;
52
- this . removeColor = this . removeColor . bind ( this ) ;
53
+
53
54
}
54
55
render ( ) {
55
- const { addColor, rateColor, removeColor } = this ;
56
56
const { colors } = this . state ;
57
- const store = this . props . store ;
58
57
return (
59
58
< div className = "app" >
60
- < SortMenu store = { store } />
61
- < AddColorForm store = { store } />
62
- < ColorList store = { store } />
59
+ < SortMenu />
60
+ < AddColorForm />
61
+ < ColorList />
63
62
</ div >
64
63
)
65
64
}
66
-
67
- addColor ( title , color ) {
68
- const colors = [
69
- ...this . state . colors ,
70
- {
71
- id : v4 ( ) ,
72
- title,
73
- color,
74
- rating :0
75
- }
76
- ]
77
- this . setState ( { colors} ) ;
78
- }
79
-
80
- rateColor ( id , rating ) {
81
- const colors = this . state . colors . map ( color =>
82
- ( color . id !== id ) ?
83
- color : {
84
- ...color ,
85
- rating
86
- }
87
- )
88
- this . setState ( { colors} ) ;
89
- }
90
-
91
- removeColor ( id ) {
92
- const colors = this . state . colors . filter (
93
- color => color . id !== id
94
- )
95
- this . setState ( { colors} )
96
- }
97
-
98
65
}
99
66
100
67
App . childContextTypes = {
0 commit comments