Hello Devs,
In this tutorial, we will learn React Axios Get Request Example
Axios is a npm package and the provide to make http request from your application. in this example we will use "jsonplaceholder" api to get data using axios package.
Follow this step by step guide below.
Example Code:
import React from 'react';
import axios from 'axios';
export default class PostList extends React.Component {
state = {
posts: []
}
componentDidMount() {
axios.get(`https://jsonplaceholder.typicode.com/posts`)
.then(res => {
const posts = res.data;
this.setState({ posts });
})
}
render() {
return (
<div>
<h1>React Axios Get Request Example - rathorji.in</h1>
<table className="table table-bordered">
<tr>
<th>ID</th>
<th>Title</th>
<th>Body</th>
</tr>
{this.state.posts.map((post) => (
<tr>
<td>{post.id}</td>
<td>{post.title}</td>
<td>{post.body}</td>
</tr>
))}
</table>
</div>
)
}
}
May this example help you.