<?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[DEVPEDIA]]></title><description><![CDATA[Software Engineer | Poet | Writer | Learner]]></description><link>https://blog.akashchaudhary.online</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1627194542981/VZ-KvLKTU.png</url><title>DEVPEDIA</title><link>https://blog.akashchaudhary.online</link></image><generator>RSS for Node</generator><lastBuildDate>Sun, 17 May 2026 22:57:09 GMT</lastBuildDate><atom:link href="https://blog.akashchaudhary.online/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Web Workers - How are they different & why should we use them?]]></title><description><![CDATA[Since the birth of the mighty warrior who conquered the web as if it was nothing, now known as Javascript, there's a saying that has been going on ever since...

"Javascript is a synchronous and single-threaded programming language."

The majority of...]]></description><link>https://blog.akashchaudhary.online/web-workers-how-are-they-different-why-should-we-use-them</link><guid isPermaLink="true">https://blog.akashchaudhary.online/web-workers-how-are-they-different-why-should-we-use-them</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[webworker]]></category><category><![CDATA[multithreading]]></category><category><![CDATA[asynchronous JavaScript]]></category><dc:creator><![CDATA[Akash Chaudhary]]></dc:creator><pubDate>Sun, 01 Jan 2023 16:00:58 GMT</pubDate><content:encoded><![CDATA[<p>Since the birth of the mighty warrior who conquered the web as if it was nothing, now known as Javascript, there's a saying that has been going on ever since...</p>
<blockquote>
<p>"Javascript is a synchronous and single-threaded programming language."</p>
</blockquote>
<p>The majority of developers across the globe 🌍 would deny this and they should 💯. The concept of callbacks took out the synchronous nature of the language a long ago and now asynchronous programming in Javascript has evolved with the help of <code>promises</code> &amp; <code>async/await</code>. Now we can, and let me quote it for you, <strong>perform non-blocking execution of code</strong> very easily in javascript.</p>
<p>But what about the other giant accusation of being a single-threaded language? 🤔</p>
<p>That's where the <strong>Web Workers</strong> come into play. For detailed information about web workers, you can always refer to the MDN <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers">doc</a>.</p>
<p>So what are Web Workers, you ask... huh?</p>
<p><em>Web workers provide a way to run scripts in the background on a separate thread (known as a worker thread).</em></p>
<p>Let's dig a little deeper... 🤩</p>
<p>So you can create a worker object by using the <code>Worker()</code> constructor which takes a file path as an argument. This file contains all the logic that we want to execute on the worker thread.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> myAwesomeWroker = <span class="hljs-keyword">new</span> Worker(<span class="hljs-string">'worker.js'</span>)
</code></pre>
<p>This will spawn a new worker, that will execute the code written in <code>worker.js</code> on a separate thread.</p>
<p>Before you start scratching your heads 🤯, here's <strong>why we need a separate thread</strong>...</p>
<p>If we just want to execute some piece of code in the background without blocking the main thread, we could've easily achieved that with <code>promises</code>. But there's a caveat, we know that asynchronous execution in JS is facilitated by <code>Callback Queue</code> &amp; <code>Event Loop</code>. All the callbacks associated with an async function are pushed into a queue and the event loop keeps monitoring the <code>call stack</code>, a callback from the queue is picked by the event loop only when the call stack is empty 🫙, and put into the call stack.</p>
<p>Whoa! 😲</p>
<p>Perhaps this might help...</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1672586163978/0b377bda-ea15-4bea-b04b-144c3f56d7f1.png" alt class="image--center mx-auto" /></p>
<p>So what if that task is a large computational task (that we returned as a <code>promise</code> ) and is resource intensive 🖥️, in that case, the async method won't be efficient. Promise will take a substantial amount of time to resolve, and the associated callback will be picked up late causing higher latency 🙇‍♂️.</p>
<p>So the right choice would be to execute that process separately, parallel with the other tasks being executed on the main thread.</p>
<p>This is often termed <strong>Multithreading</strong>.</p>
<p>Now that the cat's 🐱 out of the bag 👜, here's a layman's explanation of how <code>asynchronous</code> execution is different from <code>multithreading</code> .</p>
<ul>
<li><p>Clark and Lois decide to make coffee ☕️ but they don't have any milk 🥛. So Lois says "<em>You go whoosh ⚡️ in some milk from the store, and when you're back we'll make coffee together.</em>" - <strong>Async</strong></p>
</li>
<li><p>Lois says "You go whoosh in some milk, I'll boil the water. While the water is boiling please grab some honey &amp; sugar from the shelf, you can watch TV 📺 while the coffee gets finished. Then we'll enjoy the coffee together." - <strong>Multithreading</strong></p>
</li>
</ul>
<p>Hope, that clears up things a bit.</p>
<p>Now what happens when the task on the worker thread is finished, how does the worker convey the response to the main thread? The communication b/w the main thread &amp; worker thread happens via two event handlers that come with the package 🙌.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// sending messages to the worker</span>
myAwesomeWorker.postMessage(<span class="hljs-string">'Hey there, worker!'</span>)

<span class="hljs-comment">// listening to the messages being emitted</span>
myAwesomeWorker.onmessage = <span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Here's what my worker has to say: "</span>,e.data)
}

<span class="hljs-comment">//<span class="hljs-doctag">NOTE:</span> these two event handlers can be similarly used in the worker.js as well to send and listen to messages from main thread</span>
</code></pre>
<p>But but but, is everything rainbows 🌈 and sunshine ⛅️ about Web Workers?</p>
<p>No, there are certain limitations.</p>
<ul>
<li><p>Workers have a separate global scope instead of the default global scope (<code>window</code>), so you can't access <code>window</code> or its methods inside of a worker.</p>
</li>
<li><p>We can't manipulate the DOM from a web worker, so no interference with the UI.</p>
</li>
</ul>
<p>Here i've only touched the surface of Web Workers, there's more to them than this article. But that's what you amigos will discover, so wake up the Columbus inside you and start discovering (Well, not exactly countries 😅 ).</p>
<p>Don't forget to drop your discoveries in the comment box below &amp; if you really liked this article subscribe to my newsletter for more content like this.</p>
<p>Until then, Keep Cyberforming!!</p>
]]></content:encoded></item><item><title><![CDATA[Two ways to write CSS more elegantly in React]]></title><description><![CDATA[Ever since React came into existence, and we got to know about JSX, writing Javascript logic within HTML made our life all roses and sunshine. But with this modern art of writing code, still writing a style.css, for styling our React Components is so...]]></description><link>https://blog.akashchaudhary.online/two-ways-to-write-css-more-elegantly-in-react</link><guid isPermaLink="true">https://blog.akashchaudhary.online/two-ways-to-write-css-more-elegantly-in-react</guid><category><![CDATA[React]]></category><category><![CDATA[CSS]]></category><category><![CDATA[styled-components]]></category><dc:creator><![CDATA[Akash Chaudhary]]></dc:creator><pubDate>Sun, 25 Jul 2021 05:54:50 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1627232853518/qisfBniaJ.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Ever since React came into existence, and we got to know about JSX, writing Javascript logic within HTML made our life all roses and sunshine. But with this modern art of writing code, still writing a <strong>style.css</strong>, for styling our React Components is somewhat off putting.</p>
<p>So here i'm presenting before you 2 elegant ways to style your react components.</p>
<p>This article explains all these methods through a very basic example having two buttons - <strong>primary</strong> &amp; <strong>secondary</strong>.</p>
<h1 id="writing-css-as-objects">Writing CSS as objects</h1>
<p>This method is nothing but inline styling with slightly refactored code.
Our project is a basic React Application,our project structure looks like-</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1627189614749/9y5Ud6RUK.png" alt="article.png" /></p>
<p>It just contains an App.js file which is our root component,and our Buttom component is imported here.</p>
<p>Here's how App.js looks-</p>
<pre><code><span class="hljs-keyword">import</span> Button <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/Button'</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <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-keyword">const</span> container = {
    <span class="hljs-attr">width</span>: <span class="hljs-string">'100%'</span>,
    <span class="hljs-attr">height</span>: <span class="hljs-string">'100%'</span>,
    <span class="hljs-attr">display</span>: <span class="hljs-string">'flex'</span>,
    <span class="hljs-attr">justifyContent</span>: <span class="hljs-string">'center'</span>,
    <span class="hljs-attr">alignItems</span>: <span class="hljs-string">'center'</span>,
  }


  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{container}</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Button</span> <span class="hljs-attr">primary</span>/&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Button</span> <span class="hljs-attr">secondary</span>/&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre><p>Important thing to notice here is how the styling is being done, and as it seems obvious it isn't some rocket science just inline styling written in different way. But it makes our code look cleaner.</p>
<p>So you might be wondering what are those <strong>primary</strong> &amp; <strong>secondary</strong> props doing ? </p>
<p>Well, you'll get it in just a moment. Now let's have a look at our Button.jsx file-</p>
<pre><code>
<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>


<span class="hljs-keyword">const</span> primaryStyles = {
  <span class="hljs-attr">height</span>: <span class="hljs-string">'40px'</span>,
  <span class="hljs-attr">width</span>: <span class="hljs-string">'120px'</span>,
  <span class="hljs-attr">borderRadius</span>: <span class="hljs-string">'20px'</span>,
  <span class="hljs-attr">background</span>: <span class="hljs-string">'#01bf71'</span>,
  <span class="hljs-attr">color</span>: <span class="hljs-string">'#fff'</span>,
  <span class="hljs-attr">display</span>: <span class="hljs-string">'flex'</span>,
  <span class="hljs-attr">justifyContent</span>: <span class="hljs-string">'center'</span>,
  <span class="hljs-attr">alignItems</span>: <span class="hljs-string">'center'</span>,
  <span class="hljs-attr">margin</span>: <span class="hljs-string">'10px'</span>
}

<span class="hljs-keyword">const</span> secondaryStyles = {
  <span class="hljs-attr">height</span>: <span class="hljs-string">'40px'</span>,
  <span class="hljs-attr">width</span>: <span class="hljs-string">'120px'</span>,
  <span class="hljs-attr">borderRadius</span>: <span class="hljs-string">'20px'</span>,
  <span class="hljs-attr">border</span>: <span class="hljs-string">'2px solid #01bf71'</span>,
  <span class="hljs-attr">background</span>: <span class="hljs-string">'#fff'</span>,
  <span class="hljs-attr">color</span>: <span class="hljs-string">'#01bf71'</span>,
  <span class="hljs-attr">display</span>: <span class="hljs-string">'flex'</span>,
  <span class="hljs-attr">justifyContent</span>: <span class="hljs-string">'center'</span>,
  <span class="hljs-attr">alignItems</span>: <span class="hljs-string">'center'</span> 
}


<span class="hljs-keyword">const</span> Button = <span class="hljs-function">(<span class="hljs-params">props</span>) =&gt;</span> {

  <span class="hljs-keyword">const</span> {primary,secondary} = props

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{primary</span> ? <span class="hljs-attr">primaryStyles</span> <span class="hljs-attr">:</span> <span class="hljs-attr">secondaryStyles</span>}&gt;</span>
        CLICK ME
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  )
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Button
</code></pre><p>Here we have declared two objects - <strong>primaryStyles</strong> and <strong>secondaryStyles</strong>,which are nothing but css for styling our buttons.</p>
<p>First we destructure our props,it isn't mandatory but it's best practice to do so.
Then while rendering our button div we finally check which prop has been passed to the Button component by using a ternary operator.</p>
<p>So if primary is passed then our ternary expression will evaluate to <strong>primaryStyles</strong> else <strong>secondaryStyles</strong>.</p>
<blockquote>
<p>If you want to learn what ternary operator,  <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator">refer this.</a> </p>
</blockquote>
<p>And the corresponding style will get applied to our Button.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1627192379293/igJa57lw8.png" alt="csb.png" /></p>
<h1 id="using-styled-components">Using Styled Components</h1>
<p>Styled Components is a library that lets you write actual CSS in your Javascript, isn't it 
the climax of our partial story, Now you can write Javascript within HTML and CSS within Javascript.</p>
<p>This is the cleanest and the most modern way of writing CSS.</p>
<blockquote>
<p>If you want to know more about it,please  <a target="_blank" href="https://styled-components.com/">refer this.</a> </p>
<p>You can install it by running:  <em>npm install --save styled-components</em></p>
</blockquote>
<p>Once you've installed it, just create a new component <em>StyledButton.jsx</em>, which looks like -</p>
<pre><code>
<span class="hljs-keyword">import</span> styled <span class="hljs-keyword">from</span> <span class="hljs-string">'styled-components'</span>


<span class="hljs-keyword">export</span> const CustomButton = styled.div`<span class="javascript">
  <span class="hljs-attr">height</span>: <span class="hljs-number">40</span>px;
  width: <span class="hljs-number">120</span>px;
  border-radius: <span class="hljs-number">20</span>px;
  border: ${<span class="hljs-function"><span class="hljs-params">props</span> =&gt;</span> props.secondary ? <span class="hljs-string">'1px solid #01bf71'</span> : <span class="hljs-literal">null</span>};
  background: ${<span class="hljs-function"><span class="hljs-params">props</span> =&gt;</span> props.primary ? <span class="hljs-string">'#01bf71'</span> : <span class="hljs-string">'#fff'</span>};
  color: ${<span class="hljs-function"><span class="hljs-params">props</span> =&gt;</span> props.primary ? <span class="hljs-string">'#fff'</span> : <span class="hljs-string">'#01bf71'</span>};
  display: flex;
  justify-content: center;
  align-items: center;
  margin: <span class="hljs-number">10</span>px;
</span>`
</code></pre><p>If you carefully observe styled components gives us the leverage of accessing props directly, so that we can use them to dynamically change our CSS as we want.</p>
<p>And if you can see, this also shortens our code than the previous method.</p>
<p>Now we just need to import this CustomButton component in our App.js and pass our props.</p>
<pre><code>
<span class="hljs-keyword">import</span> Button <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/Button'</span>
<span class="hljs-keyword">import</span> {CustomButton} <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/StyledButtons'</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <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-keyword">const</span> container = {
    <span class="hljs-attr">width</span>: <span class="hljs-string">'100%'</span>,
    <span class="hljs-attr">height</span>: <span class="hljs-string">'100%'</span>,
    <span class="hljs-attr">display</span>: <span class="hljs-string">'flex'</span>,
    <span class="hljs-attr">flexDirection</span>: <span class="hljs-string">'column'</span>,
    <span class="hljs-attr">justifyContent</span>: <span class="hljs-string">'center'</span>,
    <span class="hljs-attr">alignItems</span>: <span class="hljs-string">'center'</span>,
  }


  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{container}</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Button</span> <span class="hljs-attr">primary</span>/&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Button</span> <span class="hljs-attr">secondary</span>/&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Styled Buttons<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">CustomButton</span> <span class="hljs-attr">primary</span>&gt;</span>CLICK ME<span class="hljs-tag">&lt;/<span class="hljs-name">CustomButton</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">CustomButton</span> <span class="hljs-attr">secondary</span>&gt;</span>CLICK ME<span class="hljs-tag">&lt;/<span class="hljs-name">CustomButton</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre><p>And our final output looks like-</p>
<iframe src="https://codesandbox.io/embed/elegantcss-b9488?fontsize=14&amp;hidenavigation=1&amp;theme=dark" style="width:100%;height:500px;border:0;border-radius:4px;overflow:hidden" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"></iframe>



<p>So now you see how easier and elegant it is to use styled components in your React App. Hope you enjoyed the article and are willing to use in your future projects.</p>
<p><strong>Keep Cyberforming!</strong></p>
<p>Sigining Off...</p>
]]></content:encoded></item></channel></rss>