File tree Expand file tree Collapse file tree 1 file changed +25
-31
lines changed Expand file tree Collapse file tree 1 file changed +25
-31
lines changed Original file line number Diff line number Diff line change 1
- import React from 'react' ;
1
+ import React , { Component } from 'react' ;
2
2
import SelectedFoods from './SelectedFoods' ;
3
3
import FoodSearch from './FoodSearch' ;
4
4
5
- const App = React . createClass ( {
6
- getInitialState : function ( ) {
7
- return {
8
- selectedFoods : [ ] ,
9
- } ;
10
- } ,
11
- render : function ( ) {
5
+ class App extends Component {
6
+ state = {
7
+ selectedFoods : [ ]
8
+ }
9
+
10
+ removeFoodItem = ( itemIndex ) => {
11
+ const filteredFoods = this . state . selectedFoods . filter ( ( item , idx ) => itemIndex !== idx ) ;
12
+ this . setState ( { selectedFoods : filteredFoods } ) ;
13
+ }
14
+
15
+ addFood = ( food ) => {
16
+ const newFoods = this . state . selectedFoods . concat ( food ) ;
17
+ this . setState ( { selectedFoods : newFoods } ) ;
18
+ }
19
+
20
+ render ( ) {
21
+ const { selectedFoods} = this . state ;
22
+
12
23
return (
13
24
< div className = 'App' >
14
25
< div className = 'ui text container' >
15
- < SelectedFoods
16
- foods = { this . state . selectedFoods }
17
- onFoodClick = {
18
- ( idx ) => (
19
- this . setState ( {
20
- selectedFoods : [
21
- ...this . state . selectedFoods . slice ( 0 , idx ) ,
22
- ...this . state . selectedFoods . slice (
23
- idx + 1 , this . state . selectedFoods . length
24
- ) ,
25
- ] ,
26
- } )
27
- )
28
- }
26
+ < SelectedFoods
27
+ foods = { selectedFoods }
28
+ onFoodClick = { this . removeFoodItem }
29
29
/>
30
30
< FoodSearch
31
- onFoodClick = {
32
- ( food ) => (
33
- this . setState ( {
34
- selectedFoods : this . state . selectedFoods . concat ( food ) ,
35
- } )
36
- )
37
- }
31
+ onFoodClick = { this . addFood }
38
32
/>
39
33
</ div >
40
34
</ div >
41
35
) ;
42
- } ,
43
- } ) ;
36
+ }
37
+ }
44
38
45
39
export default App ;
You can’t perform that action at this time.
0 commit comments