Skip to content

Commit dcb978c

Browse files
committed
Functional Components
1 parent 51f8b9c commit dcb978c

File tree

3 files changed

+30
-52
lines changed

3 files changed

+30
-52
lines changed

confusion/src/components/DishdetailComponent.js

+11-26
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
1-
import React, { Component } from "react";
1+
import React from "react";
22
import { Card, CardImg, CardText, CardBody, CardTitle } from "reactstrap";
33

4-
class DishDetail extends Component{
5-
constructor(props) {
6-
super(props);
7-
8-
console.log(props);
9-
10-
// stores iproperties of this component
11-
this.state = {
12-
selectedDishDetail: this.props.dsdetail
13-
};
14-
15-
16-
}
17-
18-
renderDish(dish) {
19-
4+
function RenderDish({dish}) {
205
if (dish != null) {
216
return (
227
<div className='col-12 col-md-5 m-1'>
@@ -37,7 +22,7 @@ class DishDetail extends Component{
3722
}
3823
}
3924

40-
renderComments(comments){
25+
function RenderComments({comments}){
4126
if (comments == null) {
4227
return (<div></div>)
4328
}
@@ -68,26 +53,26 @@ class DishDetail extends Component{
6853
}
6954

7055

71-
render(){
72-
const dish = this.props.dish
73-
56+
const DishDetail = (props) => {
7457

58+
const dish = props.dish
59+
7560

7661
if (dish == null) {
7762
return (<div></div>);
7863
}
7964

80-
const dishItem = this.renderDish(dish);
81-
const dishComment = this.renderComments(dish.comments);
65+
66+
8267

8368
return (
8469
<div className='row'>
85-
{dishItem}
86-
{dishComment}
70+
<RenderDish dish={ props.dish } />
71+
<RenderComments comments={ props.dish.comments } />
8772
</div>
8873
)
8974
}
9075

91-
}
76+
9277

9378
export default DishDetail;

confusion/src/components/MainComponent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Main extends Component {
3838
</Navbar>
3939

4040
<Menu dishes={this.state.dishes}
41-
onClickP={ (dishId) => this.onDishSelect(dishId) } />
41+
onClick={ (dishId) => this.onDishSelect(dishId) } />
4242

4343
<DishDetail dish={ this.state.dishes.filter( (dish)=> dish.id === this.state.selectedDish )[0] } />
4444

+18-25
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
1-
import React, { Component } from "react";
1+
import React from "react";
22
import { Card, CardImg, CardImgOverlay, CardTitle } from "reactstrap";
33

4-
class Menu extends Component{
4+
55

6-
constructor(props){
7-
super(props);
8-
9-
10-
11-
console.log('Menu component constructed');
12-
6+
function RenderMenuItem({ dish, onClick }) {
7+
return(
8+
<Card onClick={() => onClick(dish.id)}>
9+
<CardImg width="100%" src={dish.image} alt={dish.name} />
10+
<CardImgOverlay>
11+
<CardTitle> {dish.name}</CardTitle>
12+
</CardImgOverlay>
13+
</Card>
14+
);
1315
}
1416

17+
const Menu = (props) => {
1518

16-
17-
render(){
18-
console.log('renders menu component');
19-
20-
const menu = this.props.dishes.map((dish) => {
19+
const menu = props.dishes.map((dish) => {
2120
return (
2221
<div key={ dish.id } className="col-12 col-md-5 m-1">
23-
<Card onClick={() => this.props.onClickP(dish.id) }>
24-
<CardImg width="100%" src={ dish.image } alt={ dish.name } />
25-
<CardImgOverlay>
26-
<CardTitle> { dish.name }</CardTitle>
27-
</CardImgOverlay>
28-
</Card>
22+
<RenderMenuItem dish={dish} onClick={props.onClick} />
2923
</div>
3024
);
3125
});
@@ -37,12 +31,11 @@ class Menu extends Component{
3731
</div>
3832
</div>
3933
);
40-
}
4134

42-
componentDidMount(){
43-
console.log('Menu component componentDidMounbt is invoked');
44-
4535
}
46-
}
36+
37+
38+
39+
4740

4841
export default Menu;

0 commit comments

Comments
 (0)