<?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: Anunoy Dussa</title>
    <description>The latest articles on Forem by Anunoy Dussa (@adussa3).</description>
    <link>https://forem.com/adussa3</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%2F1257542%2Fbcf0a38c-52a9-4138-bac4-67945657d176.jpeg</url>
      <title>Forem: Anunoy Dussa</title>
      <link>https://forem.com/adussa3</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/adussa3"/>
    <language>en</language>
    <item>
      <title>How to use Async/Await with Fetch API in React Component?</title>
      <dc:creator>Anunoy Dussa</dc:creator>
      <pubDate>Wed, 13 Mar 2024 22:30:39 +0000</pubDate>
      <link>https://forem.com/adussa3/how-to-use-asyncawait-with-fetch-api-in-react-component-696</link>
      <guid>https://forem.com/adussa3/how-to-use-asyncawait-with-fetch-api-in-react-component-696</guid>
      <description>&lt;p&gt;Hello,&lt;/p&gt;

&lt;p&gt;I'm trying to fetch restaurants data in React using async and await and update my restaurants state with the returned restaurants response&lt;/p&gt;

&lt;p&gt;In the code below (under Method 2) I created an async function and called &lt;code&gt;await fetchRestaurants()&lt;/code&gt; which returns a response&lt;/p&gt;

&lt;p&gt;I don't understand why within the aync function, I'm able to access the list of restaurants data. However, when I return the data, it's a promise&lt;/p&gt;

&lt;p&gt;Can someone explain why this happens?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const fetchRestaurants = () =&amp;gt; {
    ...
    // returns {Promise&amp;lt;Array&amp;lt;{ name: string, rating: number,  location: string}&amp;gt;}
  }

   const [restaurants, setRestaurants] = useState([]);

    useEffect(() =&amp;gt; {
        // Method 1 - works!
        const promise = fetchRestaurants();

        promise
             .then((data) =&amp;gt; {
                 setRestaurants(data);
             })
             .catch((error) =&amp;gt; {
                 console.error(error);
             });

        // Method 2 - doesn't work
        const asyncFetchRestaurants = async () =&amp;gt; {
            try {
                const data = await asyncFetchRestaurants();
                console.log("INSIDE", data); // this prints out the list of restaurants
                return data; // why does this return a promise?
            } catch (error) {
                console.error(error);
            }
        };

        const data = asyncFetchRestaurants(); // why is data a promise and not a list of restaurants
        console.log("OUTSIDE", data); // this prints out a promise!
        setRestaurants(data) // this doesn't work! 
    }, []);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>help</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>React useState Help - Update item in Array of Objects</title>
      <dc:creator>Anunoy Dussa</dc:creator>
      <pubDate>Thu, 08 Feb 2024 18:13:47 +0000</pubDate>
      <link>https://forem.com/adussa3/react-usestate-help-update-item-in-array-of-objects-118n</link>
      <guid>https://forem.com/adussa3/react-usestate-help-update-item-in-array-of-objects-118n</guid>
      <description>&lt;p&gt;Hello,&lt;/p&gt;

&lt;p&gt;I'm creating a game where you have to roll ten dice and get their numbers to match. You can reroll the dice and you can click on a die to freeze it (keep it's number/don't reroll)&lt;/p&gt;

&lt;p&gt;I'm storing dice data in a useState that holds an array of objects&lt;/p&gt;

&lt;p&gt;When I try to update the useState object by directly reversing the die's &lt;strong&gt;isFrozen&lt;/strong&gt;  boolean, it doesn't work. Instead, it works when I make a copy of the die object, update it, and return it&lt;/p&gt;

&lt;p&gt;I don't understand why I can't directly update the die object in the array. Why do I have to create a copy of the die object?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;useState&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    const [dice, setDice] = useState(() =&amp;gt; {
        const diceArray = [];
        for (let i = 1; i &amp;lt;= 10; i++) {
            diceArray.push({ id: nanoid(), num: rollDice(), isFrozen: false });
        }
        return diceArray;
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;freezeDie code - Doesn't work&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    const freezeDie = (id) =&amp;gt; {
        setDice((oldDice) =&amp;gt;
            oldDice.map((die) =&amp;gt; {
                if (die.id === id) {
                    console.log("die", die);
                    console.log("");
                    die.isFrozen = !die.isFrozen; // THIS LINE
                }
                return die;
            })
        );
    };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the first click  reverses the &lt;strong&gt;isFrozen&lt;/strong&gt; boolean (it freezes the die - intended)&lt;/li&gt;
&lt;li&gt;future clicks reverses the &lt;strong&gt;isFrozen&lt;/strong&gt; boolean twice (basically nothing changes - not intended)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fppvhwno2yfc6gqeakc4z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fppvhwno2yfc6gqeakc4z.png" alt="Image description" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;freezeDie code - Works&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    const freezeDie = (id) =&amp;gt; {
        setDice((oldDice) =&amp;gt;
            oldDice.map((die) =&amp;gt; {
                if (die.id === id) {
                    console.log("die", die);
                    console.log("");
                    return { ...die, isFrozen: !die.isFrozen };  // THIS LINE
                }
                return die;
            })
        );
    };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the first click  reverses the &lt;strong&gt;isFrozen&lt;/strong&gt; boolean (it freezes the die - intended)&lt;/li&gt;
&lt;li&gt;future clicks also reverse the &lt;strong&gt;isFrozen&lt;/strong&gt; boolean but call the function twice (not sure why it's being called twice)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvf3pozt5xezlpnomd5yx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvf3pozt5xezlpnomd5yx.png" alt="Image description" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>question</category>
      <category>react</category>
    </item>
    <item>
      <title>How to host websites for free?</title>
      <dc:creator>Anunoy Dussa</dc:creator>
      <pubDate>Tue, 16 Jan 2024 00:40:23 +0000</pubDate>
      <link>https://forem.com/adussa3/how-to-host-websites-for-free-31pp</link>
      <guid>https://forem.com/adussa3/how-to-host-websites-for-free-31pp</guid>
      <description>&lt;p&gt;Hi,&lt;/p&gt;

&lt;p&gt;I started learning web development 3 months ago and I created a couple of applications.&lt;/p&gt;

&lt;p&gt;I want to host my websites online, but there are so many services that I don’t know where to start.&lt;/p&gt;

&lt;p&gt;What I’m looking for is a service that offers a free plan where I can host my front-end and full-stack projects.&lt;/p&gt;

&lt;p&gt;I’ve heard that you can host front-end websites on GitHub pages. I wasn’t sure if this is the recommended way to host websites, or if there’s a better free service.&lt;/p&gt;

&lt;p&gt;For full-stack applications, I tried using Render’s free plan. However, my website spins down after 15 minutes of inactivity and it takes a couple of minutes to spin back up when it receives a request.&lt;/p&gt;

&lt;p&gt;I want to show my applications to employers without having to take minutes to load the webpage. Is there another free alternative to host a full-stack application that offers a faster load time?&lt;/p&gt;

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