I am not sure how to iterate through each order and show them as individual orders. The api response looks like this…
0:
Crust: “THIN”
Flavor: “CHEESE”
Order_ID: 2
Size: “S”
Table_No: 5
Timestamp: “2019-12-03T18:21:08.708470”
And can go on to have as many orders as possible. I do not need the Order_ID showing so maybe that is a way to iterate through?
JavaScript
x
41
41
1
import React, { Component } from 'react'
2
import axios from 'axios';
3
4
export default class Orders extends Component {
5
constructor() {
6
super();
7
this.state = {
8
order: "no orders yet",
9
crust: "no crust selected",
10
// flavor: "no flavor selected",
11
// size: "no size selected",
12
// table: "place an order first",
13
// timestamp: "place an order first"
14
};
15
}
16
17
handleButtonClick = () => {
18
axios.get("/orders").then(response => {
19
this.setState({
20
orders: response,
21
crust: response.data[0].Crust
22
// flavor: response.data[0].Flavor
23
//etc..
24
});
25
})
26
}
27
28
render() {
29
return(
30
<div>
31
<button onClick={this.handleButtonClick}></button>
32
<h1>orders are:</h1>
33
<p>Crust: {this.state.crust}</p>
34
</div>
35
);
36
}
37
38
}
39
40
export { Orders }
41
Advertisement
Answer
From what I could figure out from your codeblock, this should work. Just set the orders array to the response.data and then iterate over the orders array in your render function.
JavaScript
1
30
30
1
constructor() {
2
super();
3
this.state = {
4
orders: []
5
};
6
}
7
8
handleButtonClick = () => {
9
axios.get("/orders").then(response => {
10
this.setState({
11
orders: response.data
12
});
13
})
14
}
15
16
render() {
17
const orderData = this.state.orders.map(order => {
18
return })
19
return(
20
<div>
21
<button onClick={this.handleButtonClick}></button>
22
<h1>orders are:</h1>
23
{this.state.orders.map((order) => (
24
<p>Flavor: {order.flavor}</p>
25
<p>Crust: {order.crust}</p>
26
))}
27
</div>
28
);
29
}
30