<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Moumita Dhar</title>
    <description>The latest articles on Forem by Moumita Dhar (@dhar3428).</description>
    <link>https://forem.com/dhar3428</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1006586%2F6b53916d-b126-4a21-98a7-3141f29198dd.png</url>
      <title>Forem: Moumita Dhar</title>
      <link>https://forem.com/dhar3428</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/dhar3428"/>
    <language>en</language>
    <item>
      <title>Fetching Data with fetch api for a React application</title>
      <dc:creator>Moumita Dhar</dc:creator>
      <pubDate>Fri, 12 Apr 2024 13:06:20 +0000</pubDate>
      <link>https://forem.com/dhar3428/fetching-data-with-fetch-api-for-a-react-application-32p</link>
      <guid>https://forem.com/dhar3428/fetching-data-with-fetch-api-for-a-react-application-32p</guid>
      <description>&lt;p&gt;Suppose you need to fetch some data from a source say data.json for your react application&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const getData = () =&amp;gt; {
    return (
        fetch('data.json').then((res)=&amp;gt;{
            if (res.status == 200){
               return  res.json()

            }else{
                throw Error('Something went wrong')

            }

        }).then((data) =&amp;gt; {
        console.log(data)
        return data

    })
        .catch((error) =&amp;gt; {
            console.log(error)

    }))


    }

export default getData
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;.then((data) =&amp;gt; {&lt;br&gt;
   console.log(data)&lt;br&gt;
   return data }&lt;/code&gt; part returns the data that we need.  Now since then block automatically returns a promise so even if the data is returned explicitly it gets wrapped into a promise. So when we use this function to fetch data in our React app&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; useEffect(()=&amp;gt;{
   if(!data)){
    console.log("Get the data")
    getData().then((data)=&amp;gt;{

      setdata(data)
    })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to resolve this promise . To resolve the promise we need another then block and then can be chained only in a function that return a promise. So, the getData function has to return the fetch api call which returns a promise .Finally now we can&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;getData().then((data)=&amp;gt;{

      ...do whatever is needed with the data
    })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>react</category>
    </item>
  </channel>
</rss>
