<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>frontend Archives | Mithle.sh</title>
	<atom:link href="https://mithle.sh/tag/frontend/feed/" rel="self" type="application/rss+xml" />
	<link>https://mithle.sh/tag/frontend/</link>
	<description>The Diary of a Full Stack Developer</description>
	<lastBuildDate>Sun, 19 Nov 2023 15:23:30 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.3</generator>

<image>
	<url>https://i0.wp.com/mithle.sh/wp-content/uploads/2023/03/cropped-favicon.png?fit=32%2C32&#038;ssl=1</url>
	<title>frontend Archives | Mithle.sh</title>
	<link>https://mithle.sh/tag/frontend/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">219607879</site>	<item>
		<title>How to use Async Await with For Loop in Javascript &#038; Typescript</title>
		<link>https://mithle.sh/javascript-for-loop-async-await/</link>
					<comments>https://mithle.sh/javascript-for-loop-async-await/#respond</comments>
		
		<dc:creator><![CDATA[Mithlesh]]></dc:creator>
		<pubDate>Mon, 20 Mar 2023 23:27:56 +0000</pubDate>
				<category><![CDATA[Typescript]]></category>
		<category><![CDATA[frontend]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://mithle.sh/?p=1321</guid>

					<description><![CDATA[<p>In this quick tutorial we will learn how to use async await with for loops in Javascript and Typescript using simple examples. There are mainly...</p>
<p>The post <a href="https://mithle.sh/javascript-for-loop-async-await/">How to use Async Await with For Loop in Javascript &#038; Typescript</a> appeared first on <a href="https://mithle.sh">Mithle.sh</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In this quick tutorial we will learn how to use async await with for loops in Javascript and Typescript using simple examples.</p>



<p>There are mainly three types of for loops in JavaScript:</p>



<ol>
<li><strong>for loop:</strong> A <code>for</code> loop allows you to execute a block of code repeatedly for a specified number of times.</li>



<li><strong>for…in loop:</strong> A <code>for/in</code> loop allows you to iterate over the properties of an object, executing the loop body once for each property.</li>



<li><strong>for…of loop:</strong> A <code>for/of</code> loop allows you to iterate over the values of an iterable object, executing the loop body once for each value. </li>
</ol>



<p>Consider the following object array. Suppose we want to iterate over the users array, run some time consuming task on each iteration before logging the user object to the console.</p>



<pre class="wp-block-code line-numbers language-javascript"><code>const users = &#91;
    {
        id: 1,
        name: 'Alan',
    },
    {
        id: 2,
        name: 'Joel',
    },
    {
        id: 3,
        name: 'Triss',
    }
];</code></pre>



<p>To use <code>await</code> in a for loop, you must mark the loop function as <code>async</code>, and then use <code>await</code> inside the loop to wait for each asynchronous operation to complete before moving on to the next iteration. </p>



<p>In the following example, the code is same for each type of the loops except the syntax of the loop method.</p>



<h2 class="wp-block-heading"><u>Async await with for loop</u></h2>



<pre class="wp-block-code line-numbers language-javascript"><code>async function randomWait() {
    await new Promise((resolve) =&gt; setTimeout(resolve, Math.floor(Math.random() * 100)));
    return;
}

async function printUser() {
    for(let i=0; i &lt; users.length; i++) {
        // Wait for some time consuming task to finish
        await randomWait();
        console.log(users&#91;i]);
    }
}

printUser();</code></pre>



<pre class="wp-block-code language-json"><code>OUTPUT: 
{ id: 1, name: 'Alan' }
{ id: 2, name: 'Joel' }
{ id: 3, name: 'Triss' }</code></pre>



<h2 class="wp-block-heading"><u>Async await with for…in loop</u></h2>



<pre class="wp-block-code line-numbers language-javascript"><code>async function printUser() {
    for(const index in users) {
        await randomWait();
        console.log(users&#91;index]);
    }
}</code></pre>



<pre class="wp-block-code language-json"><code>OUTPUT: 
{ id: 1, name: 'Alan' }
{ id: 2, name: 'Joel' }
{ id: 3, name: 'Triss' }</code></pre>



<h2 class="wp-block-heading"><u>Async await with for…of</u></h2>



<pre class="wp-block-code line-numbers language-javascript"><code>async function printUser() {
    for(const user of users) {
        await randomWait();
        console.log(user);
    }
}</code></pre>



<pre class="wp-block-code language-json"><code>OUTPUT: 
{ id: 1, name: 'Alan' }
{ id: 2, name: 'Joel' }
{ id: 3, name: 'Triss' }</code></pre>



<p>This code defines two async functions: <code>randomWait()</code> and <code>printUser()</code>.</p>



<p><code>randomWait()</code> uses the <code>await</code> keyword to pause execution for a random amount of time, chosen using <code>Math.random()</code> and <code>setTimeout()</code>. Once the timer expires, the <code>Promise</code> resolves and the function returns.</p>



<p><code>printUser()</code> is an async function that iterates over an array called <code>users</code>. For each element of the array, it first calls <code>randomWait()</code> to simulate some time-consuming operation, then prints the element to the console using <code>console.log()</code>.</p>



<p>In the result, you can see that the data is printed in the sequential order in each of the three for loops.</p>



<h2 class="wp-block-heading">Caveats</h2>



<p>Using <code>await</code> inside a loop can potentially slow down the loop significantly, especially if the operations being awaited are time-consuming. In some cases, it might be more efficient to use <code>Promise.all()</code> to run multiple promises in parallel rather than awaiting them one at a time in a loop.</p>



<p></p>
<p>The post <a href="https://mithle.sh/javascript-for-loop-async-await/">How to use Async Await with For Loop in Javascript &#038; Typescript</a> appeared first on <a href="https://mithle.sh">Mithle.sh</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mithle.sh/javascript-for-loop-async-await/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1321</post-id>	</item>
		<item>
		<title>How to Pass Data between Child and Parent Components in React</title>
		<link>https://mithle.sh/how-to-pass-data-between-child-and-parent-components-in-react/</link>
					<comments>https://mithle.sh/how-to-pass-data-between-child-and-parent-components-in-react/#respond</comments>
		
		<dc:creator><![CDATA[Mithlesh]]></dc:creator>
		<pubDate>Tue, 14 Mar 2023 11:43:18 +0000</pubDate>
				<category><![CDATA[React]]></category>
		<category><![CDATA[frontend]]></category>
		<category><![CDATA[nextjs]]></category>
		<guid isPermaLink="false">https://blog.mithle.sh/?p=1233</guid>

					<description><![CDATA[<p>In this quick tutorial, we will learn how to pass data from the child component to the parent component and vice versa. Parent-to-Child Passing data...</p>
<p>The post <a href="https://mithle.sh/how-to-pass-data-between-child-and-parent-components-in-react/">How to Pass Data between Child and Parent Components in React</a> appeared first on <a href="https://mithle.sh">Mithle.sh</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In this quick tutorial, we will learn how to pass data from the child component to the parent component and vice versa.</p>



<h2 class="wp-block-heading" id="parent-to-child">Parent-to-Child</h2>



<p>Passing data from parent to child component is very straightforward. You need to define <code>props</code> on the child component and then pass the values to it from the parent component. In the following example, we are rendering the avatar component (child) from the menu component(parent) by passing a <code>criminal</code> object to it.</p>



<h4 class="wp-block-heading" id="child-component">Child Component</h4>



<pre class="wp-block-code line-numbers language-typescript"><code>interface IAvatarProps {
    avatarUrl: string;
    alt: string;
    title: string;
}
const Avatar = (props: IAvatarProps) =&gt; {
  return (
    &lt;img 
        src={props.avatarUrl} 
        title={props.title} alt={props.alt} 
        className="avatar" 
    /&gt;
  );
};

export default Avatar;
</code></pre>



<h4 class="wp-block-heading" id="parent-component">Parent Component</h4>



<pre class="wp-block-code line-numbers language-typescript"><code>const criminal = {
        avatar: 'https://hindugenocide.com/islamic-jihad/9000-hindus-converted-in-madrasa/',
        name: 'Pir Ayub Jan Sarhandi'
    };
const Menu = () =&gt; {
    return (
        &lt;Menu&gt;
          &lt;Avatar 
              avatarUrl={criminal.avatar} 
              title={criminal.name} 
              alt="Hindu Genocide" 
          /&gt;
        &lt;/Menu&gt;
    );
};

export default Menu;
</code></pre>



<h2 class="wp-block-heading" id="child-to-parent">Child-to-Parent</h2>



<p>To pass the data from the child to its parent, we need to first define a <code>callback function</code> on the parent component which would then be passed via props on the child component. The child component would then call that function to pass data to the parent component.</p>



<p>For example, let us create a child component called <code>GenocideCard</code> which links to an article on <a href="https://hindugenocide.com/islamic-jihad/mirpur-massacre-of-hindus-and-sikhs-1947/" target="_blank" rel="noreferrer noopener">Mirpur Massacre.</a> We want that when this card is clicked, the article url should be passed to the parent component from where it is rendered.</p>



<ol>
<li>Define a callback function on the parent component called <code>handleGenocideCardClick</code> that accepts a parameter called <code>articleUrl</code>.</li>



<li>Pass that function in the <code>onCardClick</code> prop on the child component.</li>



<li>On the child component, we execute the <code>callback</code> function passed from the parent component when the <code>&lt;img&gt;</code> tag is clicked. The parameter of the <code>callback</code> function is passed as data to the parent component.</li>
</ol>



<h4 class="wp-block-heading" id="parent-component">Parent Component</h4>



<pre class="wp-block-code line-numbers language-typescript"><code>const handleGenocideCardClick = (articleUrl) =&gt; {
    console.log("You can read about this massacre at: " + articleUrl);
}

const Layout = () =&gt; {
  return (
        &lt;Grid&gt;
          &lt;GenocideCard onCardClick={handleGenocideCardClick}/&gt;
        &lt;/Grid&gt;
    );
};

export default Layout;
</code></pre>



<h4 class="wp-block-heading" id="child-component">Child Component</h4>



<pre class="wp-block-code line-numbers language-typescript"><code>interface IGenocideCardProps {
    onCardClick: any;
}
const HinduMassacre = {
      name: "Mirpur Massacre"
      image: "https://www.dailyexcelsior.com/wp-content/uploads/2020/11/1-176.jpg",
      url: "https://hindugenocide.com/islamic-jihad/mirpur-massacre-of-hindus-and-sikhs-1947/"
}
const GenocideCard = (props: IGenocideCardProps) =&gt; {
  return (
  &lt;div className="card"&gt;
    &lt;img 
        src={HinduMassacre.image} 
        title={HinduMassacre.name} 
        alt="Hindu Genocide" 
        onClick={() =&gt; props.onCardClick(HinduMassacre.url)} 
    /&gt;
    &lt;span class="text-red-400 font-medium"&gt;{HinduMassacre.name}&lt;/span&gt;
  );
};

export default GenocideCard;
</code></pre>
<p>The post <a href="https://mithle.sh/how-to-pass-data-between-child-and-parent-components-in-react/">How to Pass Data between Child and Parent Components in React</a> appeared first on <a href="https://mithle.sh">Mithle.sh</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mithle.sh/how-to-pass-data-between-child-and-parent-components-in-react/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1233</post-id>	</item>
	</channel>
</rss>
