-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAPIAsyncDataList.js
44 lines (37 loc) · 1.04 KB
/
APIAsyncDataList.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
//npm install axios
import React, { Component } from 'react';
import { axios } from 'axios';
class TodoList extends Component {
constructor(props) {
super(props);
this.state = {
todos: []
};
}
async componentDidMount() {
await this.getData()
}
getData = async () => {
const response = await axios.get("api route");
this.setState({todos: response.data});
setTimeout(this.getData, 1000); /*auto request to endpoint every second to fetch data*/
};
render() {
const { todos } = this.state;
return (
<div className="container">
<div className="row">
<div className="col-lg-8 offset-lg-2">
<h2>Todo List</h2>
{todos.map((todo, index) => (
<div key={index}>
<p> {todo.body}</p>
</div>
))}
</div>
</div>
</div>
);
}
}
export default TodoList;