HDTuto.com

React JS Http Get Request Example



I am going to explain you example of http get request in react js. you can see react js http get request example. i explained simply about axios http request react js. This tutorial will give you simple example of react js axios http request example.

If you want to learn how to send http get request with react then i will help you step by step instruction for sending http request using axios react. i will give you very simple example to send http get request using axios and react.

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.

So, let's see bellow example code and preview:

Preview:

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 - HDTuto.com</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>

)

}

}

I hope it can help you...




July 3, 2020 | Category : React



Blog Search