Skip to content

Commit eddacc8

Browse files
authored
Merge pull request #16 from markerikson/patch-1
Rewrite App.js to use ES6 syntax
2 parents 4bfd253 + c4a925b commit eddacc8

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

client/src/App.js

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,39 @@
1-
import React from 'react';
1+
import React, {Component} from 'react';
22
import SelectedFoods from './SelectedFoods';
33
import FoodSearch from './FoodSearch';
44

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+
1223
return (
1324
<div className='App'>
1425
<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}
2929
/>
3030
<FoodSearch
31-
onFoodClick={
32-
(food) => (
33-
this.setState({
34-
selectedFoods: this.state.selectedFoods.concat(food),
35-
})
36-
)
37-
}
31+
onFoodClick={this.addFood}
3832
/>
3933
</div>
4034
</div>
4135
);
42-
},
43-
});
36+
}
37+
}
4438

4539
export default App;

0 commit comments

Comments
 (0)