-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfetch-axios.js
62 lines (44 loc) · 1.33 KB
/
fetch-axios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//note : npm install axios
import React, { Component } from 'react';
import axios from 'axios';
class api extends Component {
constructor(props) {
super(props);
this.state = {
todos: [],
axiostodos: []
};
}
componentDidMount(){
fetch("http://127.0.0.1:5000/todos/") //api endpoint
.then(response => response.json()
.then(data => this.setState({ todos:data}))
);
axios({
method:'get',
url: 'http://127.0.0.1:5000/todos/' //api endpoint
}).then(response => this.setState({ axiostodos: response.data }))
}
render() {
let { todos, axiostodos} = this.state;
return (
<div>
{todos.map((todo , index)=>
<div key={index}>
<h1> {todo.title}</h1>
<p> {todo.body}</p>
</div>
)
}
{axiostodos.map((todo, index) =>
<div key={index}>
<h1> {todo.title}</h1>
<p> {todo.body}</p>
</div>
)
}
</div>
);
}
}
export default api;