<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Arjun]]></title><description><![CDATA[Frontend Engineer @ Findr]]></description><link>https://blogsbyarjun.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 10 Nov 2026 23:26:39 GMT</lastBuildDate><atom:link href="https://blogsbyarjun.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to update Child's state from Parent in React.]]></title><description><![CDATA[Pass a value from child component to parent component with the help of forwardRef & useImperativeHandle in React]]></description><link>https://blogsbyarjun.hashnode.dev/how-to-update-childs-state-from-parent-in-react-1</link><guid isPermaLink="true">https://blogsbyarjun.hashnode.dev/how-to-update-childs-state-from-parent-in-react-1</guid><category><![CDATA[React]]></category><category><![CDATA[forwardRef]]></category><category><![CDATA[useRef]]></category><category><![CDATA[useImperativeHandle]]></category><dc:creator><![CDATA[Arjun VC]]></dc:creator><pubDate>Wed, 13 Jul 2022 16:41:12 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1657730760228/LzDUJKE2g.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>So as you have made it here, let me assume that you know React to an extent that I don't have to explain what a <code>state</code> is or how props are passed. If you aren't confident enough, head on to <a target="_blank" href="https://reactjs.org/docs/getting-started.html">reactjs.org</a> and get started with the basics of React.</p>
<p>Let's get straight to the point, No boring introduction this time.</p>
<p>Before you can perform this stunt, You need to have a basic idea on a couple of concepts.</p>
<h3 id="heading-1-forwarref">1. forwarRef</h3>
<p>In React, every native JSX element accepts a <code>ref</code> prop which returns that particular element after it is rendered. By native, I mean standard HTML element. This element(object) is usually stored in a <code>ref</code> object that is initialized using the <code>useRef</code> hook. It will look something like this</p>
<pre><code>const App <span class="hljs-operator">=</span> () <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> {
    const buttonRef <span class="hljs-operator">=</span> useRef<span class="hljs-operator">&lt;</span>HTMLButtonElement<span class="hljs-operator">&gt;</span>(null);
    <span class="hljs-keyword">return</span> <span class="hljs-operator">&lt;</span>button ref<span class="hljs-operator">=</span>{buttonRef}<span class="hljs-operator">&gt;</span>Click Me<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>button<span class="hljs-operator">&gt;</span>
}
</code></pre><p>This is for native HTML elements. For custom React components, you will have to define the ref prop. <code>forwardRef</code> is what React provides for this purpose. Using <a target="_blank" href="https://reactjs.org/docs/forwarding-refs.html">forwardRef</a> you can pass a ref through a component to one of its children.</p>
<pre><code>const CustomButton <span class="hljs-operator">=</span> forwardRef<span class="hljs-operator">&lt;</span>HTMLButtonElement<span class="hljs-operator">&gt;</span>((_props, ref) <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> {
  <span class="hljs-keyword">return</span> <span class="hljs-operator">&lt;</span>button ref<span class="hljs-operator">=</span>{ref}<span class="hljs-operator">&gt;</span>Click me<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>button<span class="hljs-operator">&gt;</span>;
});

const App <span class="hljs-operator">=</span> () <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> {
  const buttonRef <span class="hljs-operator">=</span> useRef<span class="hljs-operator">&lt;</span>HTMLButtonElement<span class="hljs-operator">&gt;</span>(null);
  <span class="hljs-keyword">return</span> <span class="hljs-operator">&lt;</span>CustomButton ref<span class="hljs-operator">=</span>{buttonRef} <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
}
</code></pre><p>Notice how we have passed the <code>button</code> element inside <code>CustomButton</code> onto the parent <code>App</code> component. Now, what if we wanted to pass some other value instead of the element itself? React provides a hook for this purpose.</p>
<h3 id="heading-2-useimparativehandle">2. useImparativeHandle</h3>
<p>React makes it possible to pass a value from child to parent through ref using the <a target="_blank" href="https://reactjs.org/docs/hooks-reference.html#useimperativehandle">useImparativeHandle</a> hook. Note that even React suggests not to use this pattern as the data flow is supposed to be unidirectional (Top to Bottom/Parent to Child).</p>
<pre><code>const Parent <span class="hljs-operator">=</span> () <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> {
  const childRef <span class="hljs-operator">=</span> useRef<span class="hljs-operator">&lt;</span>{ <span class="hljs-built_in">value</span>: <span class="hljs-keyword">string</span> }<span class="hljs-operator">&gt;</span>(null);

 useEffect(() <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> console.log(childRef.current?.<span class="hljs-built_in">value</span>), [childRef]);
<span class="hljs-comment">// I am from child</span>

  <span class="hljs-keyword">return</span> <span class="hljs-operator">&lt;</span>Child ref<span class="hljs-operator">=</span>{childRef} <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>;
};

const Child <span class="hljs-operator">=</span> forwardRef<span class="hljs-operator">&lt;</span>{ <span class="hljs-built_in">value</span>: <span class="hljs-keyword">string</span> }<span class="hljs-operator">&gt;</span>((_props, ref) <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> {
  useImperativeHandle(ref, () <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> ({ <span class="hljs-built_in">value</span>: <span class="hljs-string">"I am from child"</span> }));
  <span class="hljs-keyword">return</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">&gt;</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>;
});
</code></pre><p>And this is all we need! Let's create that typical counter app used in every React blog ever, but this time the buttons will be on the parent component.</p>
<pre><code><span class="hljs-comment">// Counter.tsx</span>
<span class="hljs-keyword">import</span> { <span class="hljs-title">forwardRef</span>, <span class="hljs-title">useState</span>, <span class="hljs-title">useImperativeHandle</span> } <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"react"</span>;

export <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">ICounter</span> </span>{
  setCount: React.Dispatch&lt;SetStateAction<span class="hljs-operator">&lt;</span>number<span class="hljs-operator">&gt;</span><span class="hljs-operator">&gt;</span>;
}

export const Counter <span class="hljs-operator">=</span> forwardRef<span class="hljs-operator">&lt;</span>ICounter <span class="hljs-operator">|</span> null<span class="hljs-operator">&gt;</span>((_props, ref) <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> {
  const [count, setCount] <span class="hljs-operator">=</span> useState(<span class="hljs-number">0</span>);

  useImperativeHandle(ref, () <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> ({
    setCount
  }));

  <span class="hljs-keyword">return</span> <span class="hljs-operator">&lt;</span>span<span class="hljs-operator">&gt;</span>{count}<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>span<span class="hljs-operator">&gt;</span>;
});
</code></pre><pre><code><span class="hljs-comment">// App.tsx</span>
<span class="hljs-keyword">import</span> { <span class="hljs-title">useRef</span> } <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> { <span class="hljs-title">Counter</span>, <span class="hljs-title">ICounter</span> } <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"./Counter.tsx"</span>;

export const App <span class="hljs-operator">=</span> () <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> {
  const counterRef <span class="hljs-operator">=</span> useRef<span class="hljs-operator">&lt;</span>ICounter <span class="hljs-operator">|</span> null<span class="hljs-operator">&gt;</span>(null);

  const handleCounterChange <span class="hljs-operator">=</span> (step: number) <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span>
    counterRef.current?.setCount((count) <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> count <span class="hljs-operator">+</span> step);

  <span class="hljs-keyword">return</span> (
      <span class="hljs-operator">&lt;</span>div<span class="hljs-operator">&gt;</span>
        <span class="hljs-operator">&lt;</span>button onClick<span class="hljs-operator">=</span>{() <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> handleCounterChange(<span class="hljs-number">-1</span>)}<span class="hljs-operator">&gt;</span><span class="hljs-operator">-</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>button<span class="hljs-operator">&gt;</span>
        <span class="hljs-operator">&lt;</span>Counter ref<span class="hljs-operator">=</span>{counterRef} <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
        <span class="hljs-operator">&lt;</span>button onClick<span class="hljs-operator">=</span>{() <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> handleCounterChange(<span class="hljs-number">1</span>)}<span class="hljs-operator">&gt;</span><span class="hljs-operator">+</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>button<span class="hljs-operator">&gt;</span>
      <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>div<span class="hljs-operator">&gt;</span>
  );
};
</code></pre><h4 id="heading-code-explaination">Code Explaination</h4>
<ul>
<li>The <code>Counter</code> component has a <code>count</code> state which is displayed on the same.</li>
<li>The <code>setCount</code> function is forwarded onto the <code>App</code> component using the <code>useImperativeHandle</code> hook.</li>
<li><code>useImperativeHandle</code> takes in the <code>ref</code> object and a callback function that returns an object that will be stored in the <code>current</code> property of the <code>ref</code> object.</li>
<li><code>setCount</code> is now accessible on <code>App</code> and it can be used to update the <code>count</code> state. </li>
</ul>
<p>There you go, The <code>count</code> state in the <code>Counter</code> component will now be updated from the <code>App</code> component.
<a target="_blank" href="https://codesandbox.io/s/hypocrite-counter-cli35p">Live demo</a></p>
]]></content:encoded></item><item><title><![CDATA[Skeleton Screens : How to?]]></title><description><![CDATA[How to create a skeleton loading screen using React.js]]></description><link>https://blogsbyarjun.hashnode.dev/skeleton-screens-how-to</link><guid isPermaLink="true">https://blogsbyarjun.hashnode.dev/skeleton-screens-how-to</guid><category><![CDATA[React]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[HTML]]></category><category><![CDATA[CSS]]></category><category><![CDATA[UI]]></category><dc:creator><![CDATA[Arjun VC]]></dc:creator><pubDate>Mon, 03 Jan 2022 17:19:02 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1641229664913/cJmUqMd1t.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>User interface designs and concepts has evolved a lot through the past few years. With Web technologies advancing rapidly, it's important to maintain a smooth and appealing UI to catch the user's attention. various methods has to be adopted in order to engage the user while the site loads and here comes Skeleton Screens to the rescue. In this article, I will walk you through the steps to create a Skeleton UI using React</p>
<h3 id="heading-prerequisites">Prerequisites</h3>
<ul>
<li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
<li>React</li>
</ul>
<h3 id="heading-setting-up">Setting Up</h3>
<p>Use 
<code>npx create-react-app</code>  to create a boilerplate and clear the unwanted files and contents. We will be creating an app that displays a list of posts from  <a target="_blank" href="https://jsonplaceholder.typicode.com/posts">https://jsonplaceholder.typicode.com/posts</a> API.</p>
<h4 id="heading-note-that-few-general-conventions-are-intentionally-avoided-to-keep-it-as-simple-as-possible">Note that <em>few general conventions are intentionally avoided to keep it as simple as possible</em>.</h4>
<h3 id="heading-card">Card</h3>
<p>Create <code>Card.js</code> file inside the <code>src</code> folder. This component will be used to display the post details.</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-title">React</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"react"</span>;

export default <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Card</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="hljs-operator">&lt;</span>div className<span class="hljs-operator">=</span><span class="hljs-string">"card"</span><span class="hljs-operator">&gt;</span>
      <span class="hljs-operator">&lt;</span>h2<span class="hljs-operator">&gt;</span>title goes here<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>h2<span class="hljs-operator">&gt;</span>
      <span class="hljs-operator">&lt;</span>p<span class="hljs-operator">&gt;</span>body goes here<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>p<span class="hljs-operator">&gt;</span>
    <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>div<span class="hljs-operator">&gt;</span>
  );
}
</code></pre><h3 id="heading-skeleton">Skeleton</h3>
<p>Create <code>Skeleton.js</code> file inside the <code>src</code> folder :</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-title">React</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"react"</span>;

export default <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Skeleton</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="hljs-operator">&lt;</span>div className<span class="hljs-operator">=</span><span class="hljs-string">"card skeleton"</span><span class="hljs-operator">&gt;</span>
      <span class="hljs-operator">&lt;</span>div className<span class="hljs-operator">=</span><span class="hljs-string">"title text"</span><span class="hljs-operator">&gt;</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>div<span class="hljs-operator">&gt;</span>
      <span class="hljs-operator">&lt;</span>div className<span class="hljs-operator">=</span><span class="hljs-string">"description text"</span><span class="hljs-operator">&gt;</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>div<span class="hljs-operator">&gt;</span>
      <span class="hljs-operator">&lt;</span>div className<span class="hljs-operator">=</span><span class="hljs-string">"description text"</span><span class="hljs-operator">&gt;</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>div<span class="hljs-operator">&gt;</span>
      <span class="hljs-operator">&lt;</span>div className<span class="hljs-operator">=</span><span class="hljs-string">"description text"</span><span class="hljs-operator">&gt;</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>div<span class="hljs-operator">&gt;</span>
      <span class="hljs-operator">&lt;</span>div className<span class="hljs-operator">=</span><span class="hljs-string">"overlay"</span><span class="hljs-operator">&gt;</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>div<span class="hljs-operator">&gt;</span>
    <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>div<span class="hljs-operator">&gt;</span>
  );
}
</code></pre><h3 id="heading-styling">Styling</h3>
<p>Let's add few stylings to the components :</p>
<pre><code>* {
  <span class="hljs-attribute">box-sizing</span>: border-box;
}
<span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#f8f9fa</span>;
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">flex-direction</span>: column;
  <span class="hljs-attribute">align-items</span>: center;
}
<span class="hljs-selector-tag">h1</span> {
  <span class="hljs-attribute">text-align</span>: center;
}
<span class="hljs-selector-class">.card</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">450px</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">30px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">min-height</span>: <span class="hljs-number">200px</span>;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#fff</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">10px</span>;
  <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">0px</span> <span class="hljs-number">0px</span> <span class="hljs-number">5px</span> <span class="hljs-number">1px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0.3</span>);
}
<span class="hljs-selector-tag">h2</span>, <span class="hljs-selector-tag">p</span>, <span class="hljs-selector-class">.title</span>, <span class="hljs-selector-class">.description</span> {
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">15px</span>;
}
<span class="hljs-selector-class">.text</span> {
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">8px</span>;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#dee2e6</span>;
}
<span class="hljs-selector-class">.title</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">20px</span>;
}
<span class="hljs-selector-class">.description</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">350px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">15px</span>;
}
</code></pre><h3 id="heading-animation">Animation</h3>
<p>The Skeleton Screen is only complete with a linear animation, so let's add a few more properties :</p>
<pre><code><span class="hljs-selector-class">.skeleton</span> {
  <span class="hljs-attribute">position</span>: relative;
}
<span class="hljs-selector-class">.overlay</span> {
  <span class="hljs-attribute">position</span>: absolute;
  <span class="hljs-attribute">top</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">left</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">20%</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-built_in">rgba</span>(<span class="hljs-number">256</span>, <span class="hljs-number">256</span>, <span class="hljs-number">256</span>, <span class="hljs-number">0.3</span>);
  <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">0px</span> <span class="hljs-number">0px</span> <span class="hljs-number">20px</span> <span class="hljs-number">30px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">256</span>, <span class="hljs-number">256</span>, <span class="hljs-number">256</span>, <span class="hljs-number">0.2</span>);
  <span class="hljs-attribute">animation</span>: slider <span class="hljs-number">1.2s</span> infinite;
}
<span class="hljs-keyword">@keyframes</span> slider {
  0% {
    <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translateX</span>(-<span class="hljs-number">100%</span>);
  }
  100% {
    <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translateX</span>(<span class="hljs-number">500%</span>);
  }
}
</code></pre><h3 id="heading-app">App</h3>
<p>Now we are all set with the components. Let's import them in <code>App.js</code>:</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-string">"./styles.css"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">Card</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"./Card"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">Skeleton</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"./Skeleton"</span>;
</code></pre><p>Let's build Logic to fetch the posts. For this we will need <code>useState</code> and <code>useEffect</code> hooks. So let's import them :</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-title">React</span>, { <span class="hljs-title">useState</span>, <span class="hljs-title">useEffect</span> } <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"react"</span>;
</code></pre><p>We will need  a state to store the posts. So initialize an empty array:</p>
<pre><code><span class="hljs-keyword">const</span> [posts, setPosts] = useState([]);
</code></pre><p>Also , initialize a boolean state to keep a track on the <code>posts</code> array :</p>
<pre><code><span class="hljs-keyword">const</span> [isEmpty, setIsEmpty] = useState(<span class="hljs-literal">true</span>);
</code></pre><p>The <code>isEmpty</code> variable is set to <code>true</code> initially since the <code>posts</code> array is empty.</p>
<p>Now lets fetch the posts :</p>
<pre><code> useEffect(() <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> {
    setTimeout(() <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> {
      fetch(<span class="hljs-string">"https://jsonplaceholder.typicode.com/posts"</span>)
        .then((response) <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> response.json())
        .then((data) <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> {
          setPosts(data);
          setIsEmpty(<span class="hljs-literal">false</span>);
        })
        .catch((err) <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> console.log(err));
    }, <span class="hljs-number">3000</span>);
 }, []);
</code></pre><p>A 3 seconds delay is added to mimic a slow server resonse. The <code>isEmpty</code> variable is updated to <code>false</code> when <code>posts</code> array is populated with data. The <code>useEffect</code> hook is provided with an empty dependency array which corresponds to the <code>componentDidMount</code> life cycle method.</p>
<p>The final part, Let's render the components :</p>
<pre><code><span class="hljs-keyword">return</span> (
    <span class="hljs-operator">&lt;</span><span class="hljs-operator">&gt;</span>
      <span class="hljs-operator">&lt;</span>h1<span class="hljs-operator">&gt;</span>Posts<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>h1<span class="hljs-operator">&gt;</span>
      {
        isEmpty <span class="hljs-operator">&amp;</span><span class="hljs-operator">&amp;</span> [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>].map((value) <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> <span class="hljs-operator">&lt;</span>Skeleton key<span class="hljs-operator">=</span>{value} <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>)
      }
      {
        isEmpty <span class="hljs-operator">|</span><span class="hljs-operator">|</span>
          posts.map((post) <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> (
            <span class="hljs-operator">&lt;</span>Card key<span class="hljs-operator">=</span>{post.id} title<span class="hljs-operator">=</span>{post.title} body<span class="hljs-operator">=</span>{post.body} <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
          ))
      }
    <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
  );
</code></pre><p>We render 4 Skeleton Cards if <code>isEmpty</code> is true. Else, we render the posts.</p>
<p>The <code>App.js</code> will look like :</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-string">"./styles.css"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">React</span>, { <span class="hljs-title">useState</span>, <span class="hljs-title">useEffect</span> } <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">Card</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"./Card"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">Skeleton</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"./Skeleton"</span>;

export default <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">//state to store posts</span>
  <span class="hljs-comment">//initializing posts as an empty array</span>
  const [posts, setPosts] <span class="hljs-operator">=</span> useState([]);
  <span class="hljs-comment">//state to determine whether posts is empty</span>
  const [isEmpty, setIsEmpty] <span class="hljs-operator">=</span> useState(<span class="hljs-literal">true</span>);

  useEffect(() <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> {
    <span class="hljs-comment">//3s timeout to mimic server delay</span>
    setTimeout(() <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> {
      fetch(<span class="hljs-string">"https://jsonplaceholder.typicode.com/posts"</span>)
        .then((response) <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> response.json())
        .then((data) <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> {
          <span class="hljs-comment">//populating posts array</span>
          setPosts(data);
          setIsEmpty(<span class="hljs-literal">false</span>);
        })
        .catch((err) <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> console.log(err));
    }, <span class="hljs-number">3000</span>);
  }, []);

  <span class="hljs-keyword">return</span> (
    <span class="hljs-operator">&lt;</span><span class="hljs-operator">&gt;</span>
      <span class="hljs-operator">&lt;</span>h1<span class="hljs-operator">&gt;</span>Posts<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>h1<span class="hljs-operator">&gt;</span>
      {
        <span class="hljs-comment">// rendering 4 skeleton cards when posts array is empty</span>
        isEmpty <span class="hljs-operator">&amp;</span><span class="hljs-operator">&amp;</span> [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>].map((value) <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> <span class="hljs-operator">&lt;</span>Skeleton key<span class="hljs-operator">=</span>{value} <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>)
      }
      {
        <span class="hljs-comment">//rendering actual cards when posts array is populated with some data</span>
        isEmpty <span class="hljs-operator">|</span><span class="hljs-operator">|</span>
          posts.map((post) <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> (
            <span class="hljs-operator">&lt;</span>Card key<span class="hljs-operator">=</span>{post.id} title<span class="hljs-operator">=</span>{post.title} body<span class="hljs-operator">=</span>{post.body} <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
          ))
      }
    <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
  );
}
</code></pre><p>A final touch left. Notice that we are passsing props to the <code>Card</code> Component? It has to be taken care of. Make the following changes in <code>Card.js</code>.</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-title">React</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"react"</span>;

export default <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Card</span>(<span class="hljs-params">{ title, body }</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="hljs-operator">&lt;</span>div className<span class="hljs-operator">=</span><span class="hljs-string">"card"</span><span class="hljs-operator">&gt;</span>
      <span class="hljs-operator">&lt;</span>h2<span class="hljs-operator">&gt;</span>{title}<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>h2<span class="hljs-operator">&gt;</span>
      <span class="hljs-operator">&lt;</span>p<span class="hljs-operator">&gt;</span>{body}<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>p<span class="hljs-operator">&gt;</span>
    <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>div<span class="hljs-operator">&gt;</span>
  );
}
</code></pre><p>We destructured the props and used them appropriately.</p>
<p>And Thats it !!</p>
<h3 id="heading-final-output">Final Output</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1641229032997/9XLJJEwgW.gif" alt="skeleton" /></p>
]]></content:encoded></item><item><title><![CDATA[Asynchronous JavaScript: Basic Intro]]></title><description><![CDATA[Understand how javaScript works under the hoods.]]></description><link>https://blogsbyarjun.hashnode.dev/asynchronous-javascript-basic-intro</link><guid isPermaLink="true">https://blogsbyarjun.hashnode.dev/asynchronous-javascript-basic-intro</guid><dc:creator><![CDATA[Arjun VC]]></dc:creator><pubDate>Thu, 23 Sep 2021 14:30:40 GMT</pubDate><content:encoded><![CDATA[<p>It is a common thing with us humans to judge someone without even knowing who they really are. We just create an impression with the first interaction itself. The thing is, that person would be way different than we thought he/she is. JavaScript can be related to that person as it is the most misunderstood  language out there. But I can guarantee you that once you know what JavaScript really is, You would love it more than anything and also would enjoy coding. For that it is essential to understand how it works under the hood. So let me give it a try to explain how it actually works.</p>
<h3 id="what-is-asynchronous-programming">What is Asynchronous Programming</h3>
<p>First of all, JavaScript is a synchronous single threaded language. Yeah, I know, It is a hell of a jargon. It just means that JavaScript can only process one thing at a time. At the first glance, this would sound awkward because if it could only execute one statement at a time, then if one statement takes longer time to execute, the others have to wait until its finished right? But that's not how it works, JavaScript is way more advanced than that. So this is when 'Asynchronous programming ' comes into picture. In the asynchronous world nobody waits for anything. That is, the statements are executed in parallel so that even if one process is longer than the others, It wouldn't stop others from executing. Now that you understood what asynchronous programming is, lets go a little deeper. Hold on tight</p>
<h3 id="the-call-stack">The Call Stack</h3>
<p>I previously mentioned that JavaScript could execute only one statement at a time, the question is who is actually executing the code? The answer is the legendary <strong>Call Stack</strong>. Each and every code in JavaScript is executed through the call stack. Consider the code below</p>
<pre><code>console.log("hello there!");
</code></pre><p>Once you execute this code, it is pushed into the call stack and is processed from there. After complete execution of the code, it is popped out of the stack and the program ends.</p>
<p>Now consider the code below.</p>
<pre><code><span class="hljs-built_in">console</span>.log(<span class="hljs-string">"This is 1st log"</span>);
setTimeOut(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"This is 2nd log"</span>),<span class="hljs-number">5000</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"This is 3rd log"</span>);
</code></pre><p>Here, there are more statements among which one takes 5 seconds time to execute. What do you think will happen? If your thought went the way that the 3rd log statement being executed after 5secs, I am afraid you are wrong. That's not how it works. Here's the big guns stealing the show, the <strong>Web API</strong>, the <strong>Call Back Queue</strong> and the <strong>Event Loop</strong>. Lets go one more level deeper.</p>
<h3 id="the-asynchronous-part">The Asynchronous Part</h3>
<pre><code>setTimeOut(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"This is 2nd log"</span>),<span class="hljs-number">5000</span>);
</code></pre><p>When JavaScript encounters a code like this, it is no more executed in the <strong>call stack</strong>, it is taken out and stored in the <strong>Web API environment</strong> where a timer of 5secs is attached to it. Since it is popped out of the call stack, the remaining code is executed. So the 3rd log statement is executed and the call stack is cleared and the execution stops. Is that it? What about the 2nd log statement? No JavaScript has not abandoned that, it waits for the timer to expire. So after 5secs it is again pushed to the call stack. No no, not directly into the call stack. What if there are other statements being executed in the call stack even after 5secs? Pushing the code in between will result to unindented problems. But JavaScript deals with this cleverly. When the timer expires, the code  first enters the <strong>Call Back Queue</strong> and it waits there till the call stack is empty. Another question arises is how would it know when the call stack is empty? Now its time for the final part, the <strong>Event Loop</strong>.</p>
<h3 id="event-loop">Event Loop</h3>
<p>Event Loop is the one who continuously monitors the call stack and the callback queue. When the call stack becomes empty, the event loop takes the first code in the callback queue and pushes it into the call stack. This process continues until both the call stack and callback queue are empty.</p>
<h3 id="what-kind-of-code-goes-through-this-process">What kind of code goes through this process?</h3>
<p>The <strong>Dom events</strong>, the <strong>network responses</strong>, other <strong>API calls</strong> and more. When it comes to <strong>network calls</strong>, they are to be dealt with more priority. So the network responses goes through the <strong>Micro Task Queue</strong> which is similar to the callback queue but has more priority. That is, if there are codes in both the Micro Task Queue and Callback Queue, the code inside the Micro Task queue is processed first. No matter what the number of code inside the micro task queue is, the code inside the callback queue is executed only after all of the micro task codes are executed. So be cautious when dealing with these stuffs as it would block the code if there are significantly large amount of statements waiting in the queue. It will result badly as the UI will be stuck which is a bad user experience.</p>
<h3 id="the-conclusion">The Conclusion</h3>
<p>That's it! Now you have a basic idea of how JavaScript deals with your code. There is more to it, this is just a basic introduction. I hope you realized how beautiful JavaScript is. So next time when you code, keep these things in mind. It will help you understand why the code is behaving as it is.</p>
]]></content:encoded></item></channel></rss>