{"id":55758,"date":"2022-11-11T15:23:15","date_gmt":"2022-11-11T09:53:15","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=55758"},"modified":"2022-11-11T15:23:15","modified_gmt":"2022-11-11T09:53:15","slug":"how-to-fetch-api-data-in-reactjs","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-fetch-api-data-in-reactjs\/","title":{"rendered":"How to fetch API data in ReactJS ?"},"content":{"rendered":"<p>We can fetch data by using the Javascript fetch() method. The Fetch API is a new standard to server requests with Promises, it also includes additional features. This blog will help you understand the implementation of fetch API in both functional and class components by walking through each one with sample code. Let\u2019s start &#8211;<\/p>\n<h2><strong>Consider the below object as a sample response from API:<\/strong><\/h2>\n<pre>{\r\ndata: [\r\n{ \"title\": \"Di\", \"date\": \"22 Oct 2022\" },\r\n{ \"title\": \"Diii\", \"date\": \"23 Oct 2022\" }\r\n]\r\n}<\/pre>\n<h2>For Functional Component:<\/h2>\n<p>Below are the simple steps &#8211;<\/p>\n<p><strong>Step 1<\/strong><br \/>\nImport useState &amp; useEffect<\/p>\n<p><strong>Step 2<\/strong><br \/>\ndefine state variables to store the API response using useState method.<\/p>\n<p><strong>Step 3<\/strong><br \/>\nIn the last step, call the API in the useEffect method.<\/p>\n<p><strong>Below is an example<\/strong>:<\/p>\n<pre><span style=\"font-weight: 400;\">import React, { useState, useEffect } from \"react\";<\/span>\r\n\r\n<span style=\"font-weight: 400;\">const App = () =&gt; {<\/span>\r\n<span style=\"font-weight: 400;\">const [data, setData] = useState([]);<\/span>\r\n<span style=\"font-weight: 400;\">useEffect(() =&gt; {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0fetch(\"https:\/\/apidata.com\")<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0.then(res =&gt; res.json())<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0.then(<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(result) =&gt; {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0setData(result);<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0)<\/span>\r\n<span style=\"font-weight: 400;\">}, []);<\/span>\r\n<span style=\"font-weight: 400;\">return (<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0&lt;div className=\u201dlist\u201d&gt;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0{data.map((item,index) =&gt; (<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;div key={index}&gt;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{item.title} {item.date}<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/div&gt;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0))}<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0&lt;\/div&gt;<\/span>\r\n<span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">};<\/span>\r\n<span style=\"font-weight: 400;\">export default App;<\/span><\/pre>\n<h2>For Class Component:<\/h2>\n<p><strong>Step 1<\/strong><br \/>\nImport Component<\/p>\n<p><strong>Step 2<\/strong><br \/>\ndefine state inside the class constructor<\/p>\n<p><strong>Step 3<\/strong><br \/>\nIn the last step call the API inside componentDidMount method.<\/p>\n<p><strong>Below is an example:<\/strong><\/p>\n<pre><span style=\"font-weight: 400;\">import React, { Component } from \"react\";<\/span>\r\n<span style=\"font-weight: 400;\">\r\nclass App extends Component {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0constructor(props) {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0super(props);<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0this.state = {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0data: []<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0};<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0}<\/span>\r\n <span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0componentDidMount() {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0fetch(\"https:\/\/apidata.com\")<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0.then(res =&gt; res.json())<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0.then(<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(result) =&gt; {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.setState({<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0data: result.data<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0});<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0)<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0}<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0render() {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0const { data } = this.state;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0return (<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;div className=\u201dlist\u201d&gt;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{data.map((item,index) =&gt; (<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;div key={index}&gt;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{item.title} {item.date}<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/div&gt;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0))}<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/div&gt;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0);<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0}<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0}<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0export default App;<\/span><\/pre>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>We can fetch data by using the Javascript fetch() method. The Fetch API is a new standard to server requests with Promises, it also includes additional features. This blog will help you understand the implementation of fetch API in both functional and class components by walking through each one with sample code. Let\u2019s start &#8211; [&hellip;]<\/p>\n","protected":false},"author":228,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[3429],"tags":[4842,4064],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55758"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/228"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=55758"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55758\/revisions"}],"predecessor-version":[{"id":55760,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55758\/revisions\/55760"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=55758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=55758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=55758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}