<?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: Mohammad Bagher Abiyat</title>
    <description>The latest articles on Forem by Mohammad Bagher Abiyat (@aslemammad).</description>
    <link>https://forem.com/aslemammad</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%2F401660%2Fc7caa1e6-22ff-48fa-8773-bad89bb68712.jpeg</url>
      <title>Forem: Mohammad Bagher Abiyat</title>
      <link>https://forem.com/aslemammad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/aslemammad"/>
    <language>en</language>
    <item>
      <title>We Might Be Different Kind of Developers</title>
      <dc:creator>Mohammad Bagher Abiyat</dc:creator>
      <pubDate>Tue, 05 Mar 2024 20:56:48 +0000</pubDate>
      <link>https://forem.com/aslemammad/we-might-be-different-kind-of-developers-2df4</link>
      <guid>https://forem.com/aslemammad/we-might-be-different-kind-of-developers-2df4</guid>
      <description>&lt;p&gt;A while ago I tweeted this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/asleMammadam/status/1761709097127182731"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jFoCp9ep--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://minimalistweb.dev/posts/assets/tweet-we-might-be-different-kind-of-developers.png" alt="" width="800" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I was wondering where my FOMO (Fear Of Missing Out) comes from. There have been articles about React is this and that! A while after we hear about how's React Labs is going to change everything in React 19. &lt;/p&gt;

&lt;p&gt;Then I see Qwikjs with all of the new improvements for the second version. Or I follow news around Solidjs, and I wonder how it's becoming the React I ever wanted. But after all, I'm like, there are too many options right now, I cannot even choose.&lt;/p&gt;

&lt;p&gt;Next, I hear about the Local-first movement, and I suddenly decide to consider it for every application I'm going to write (spoiler alert: I don't write applications, mostly libraries)&lt;/p&gt;

&lt;p&gt;The htmx discussion gets me to consider reading &lt;a href="https://hypermedia.systems/"&gt;the htmx&lt;/a&gt; book. Honestly, I might not need to, but I'm just... curious.&lt;br&gt;
Unjs keeps innovating every day, and it just skyrockets my FOMO around how I'm supposed to keep up with everything those folks build.&lt;/p&gt;

&lt;p&gt;Wow, a new runtime just showed up, by amazon, which I haven't heard its name that much in the industry around me, except AWS. We also have pkl, by apple, which is a new way to do configuration. Next we have the serverless horror stories, now I'm thinking of self-hosting. &lt;/p&gt;

&lt;p&gt;All these technologies take part of my mind every day, thinking about how I'm going to adopt them. Or If I should adopt them or not, trying to find reasons based on different circumstances. &lt;/p&gt;

&lt;p&gt;Each of these technologies in itself is not confusing, instead they're all exciting, but all the mental burden building up makes the combination really confusing to me. How I'm going to combine each of these, that's the question I have every day, or as an open source developer, how I'm going to jump on the hype train, most importantly.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Cure
&lt;/h2&gt;

&lt;p&gt;I was trying to find a solution for this feeling, the FOMO. No solution yet to be honest. &lt;br&gt;
 Some may suggest removing twitter, but I enjoy it, it keeps me moving. All of that excitement, it comes with a cost, but at the end, each and every day I'm excited about all of that evolvement. &lt;/p&gt;

&lt;p&gt;But when deeply looking into it, there might not be. I'm trying to find the best solution, that fits every situation. And that's exactly the mistake I'm making. I do not have a company that's worth million dollars. I don't even work in one, I'm just an open source developer. Finding the best tooling solution might not even make sense.&lt;br&gt;
That's not the part of technology that's fun to me, maybe it's an age thing also, but I don't like finding the one-fits-all solution, I love exploring. Part of what made me enjoy this 5-4 years journey, is trying different things and finding out how they behave. &lt;/p&gt;

&lt;p&gt;Me and the circle I'm in, the open source devs, the devs who watch the twitter news eagerly, follow youtube videos around what tool came up the other day, are a different kind of developers I reckon. We're not trying to find the "right" solution for our own company or the company we work in. Or the product we build. It's just trying different solutions that gets us the excitement. At least that's how I feel it is for my fellow developers.&lt;/p&gt;

&lt;p&gt;So being confused about the different tools and getting the perfect combination, might not be bad after all! So even though I'm going to build a product soon, I won't choose the tool I used the last time I built a product. I would choose something more alternative that seems more interesting, like Nitro, because It's a backend-only solution that I feel it's going to be fun. &lt;/p&gt;

&lt;p&gt;The last time I built a product (which failed), I used Qwikjs, and I cannot imagine how much I enjoyed it, but this time if I'm going to build a UI, I might try the Waku / RSC implementation, not because I'm a team member, I still find Qwikjs smoother as a server first UI library compared to RSC. But I want to wrap my head around RSC right now, because, it's just more interesting at this time and I'm more curious around it.&lt;/p&gt;

&lt;p&gt;I think I might need to have a mindset shift, that this game for me at this point of my career should be fun after all, and not something strict that I have to bet it'd still behave the same in the next 10 years anyway. As I said, this might be absolutely wrong for other groups and niches of developers. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I'm trying to learn about writing and also experimenting with different ways of doing it! Let me know how do you feel about this one. Here's my &lt;a href="https://twitter.com/asleMammadam"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>fomo</category>
    </item>
    <item>
      <title>Streams and React Server Components</title>
      <dc:creator>Mohammad Bagher Abiyat</dc:creator>
      <pubDate>Sun, 14 Jan 2024 15:18:41 +0000</pubDate>
      <link>https://forem.com/aslemammad/streams-and-react-server-components-3fak</link>
      <guid>https://forem.com/aslemammad/streams-and-react-server-components-3fak</guid>
      <description>&lt;p&gt;Many developers have used streams when building technology, but how many have truly understood their intricacies and their connection to React Server Components? Personally, the concept never quite clicked for me. It wasn't until contributing to &lt;a href="https://waku.gg" rel="noopener noreferrer"&gt;Waku&lt;/a&gt; and being curious about how RSCs stream html—requiring me to take them seriously. Waku is a minimal layer over React Server Components using Vite.&lt;/p&gt;

&lt;p&gt;In this essay, I'm going to talk more about the concept, the abstraction the Streams API provides around the concept and how React Server Components leverage this API.&lt;/p&gt;

&lt;h3&gt;
  
  
  Concept
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;data that is created, processed, and consumed in an incremental fashion, without ever reading all of it into memory. &lt;a href="https://streams.spec.whatwg.org/#intro" rel="noopener noreferrer"&gt;whatwg&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Well, streams are a way of handling data sequentially! for instance, instead of reading the whole file into the memory which can exhaust the resources, you'd handle the file chunk by chunk (by dividing it).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;await &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;chunk&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Do something with each small "chunk"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This saves memory, as you handle one chunk at a time instead of the whole file. Reading that file becomes almost instantaneous, as soon as your computer process the first chunk, it'd pass it to you without waiting for all the data to be processed.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A chunk is a single piece of data that is written to or read from a stream. It can be of any type; streams can even contain chunks of different types. &lt;a href="https://streams.spec.whatwg.org/" rel="noopener noreferrer"&gt;whatwg&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The web streams API, just provides a standard API for this concept, that not only handles files, but any form of data!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// All these methods embed the whole response in memory and return all of the results as opposed to response.body&lt;/span&gt;
&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  RSC and &lt;code&gt;Transfer-Encoding: chunked&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;While curious about how the React team streams the html, most Google articles referred to streaming videos with HLS (HTTP Live Streaming), which wasn't the desired information. It wasn't until I stumbled upon &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding" rel="noopener noreferrer"&gt;this&lt;/a&gt; mdn document.&lt;/p&gt;

&lt;p&gt;This header means that the response I'm sending is divided into multiple chunks and we can add to those chunks in a non-blocking way after the initial response.&lt;br&gt;
The key is that React just returns a stream (&lt;a href="https://github.com/facebook/react/blob/c5b9375767e2c4102d7e5559d383523736f1c902/packages/react-dom/src/server/ReactDOMFizzServerEdge.js#L176" rel="noopener noreferrer"&gt;ReadableStream&lt;/a&gt; for edge, and a pipe method for Nodejs &lt;a href="https://github.com/facebook/react/blob/c5b9375767e2c4102d7e5559d383523736f1c902/packages/react-dom/src/server/ReactDOMFizzServerNode.js#L189" rel="noopener noreferrer"&gt;Streams&lt;/a&gt; ) and would let the server handle the rest!&lt;/p&gt;

&lt;p&gt;In Nodejs or Cloudflare workers, the &lt;code&gt;Transfer-Encoding: chunked&lt;/code&gt; header is set for the response since the response body is a stream.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fminimalistweb.dev%2Fposts%2Fassets%2Fwaku-rsc-streaming.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fminimalistweb.dev%2Fposts%2Fassets%2Fwaku-rsc-streaming.gif" alt="waku rsc render"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;RSC streaming an html document using a ReadableStream in Waku.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  ReadableStream
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Fetch the original image&lt;/span&gt;
&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./tortoise.png&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;// Retrieve its body as ReadableStream&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams#consuming_a_fetch_as_a_stream&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;It's a stream that you're supposed to read from, simple as that, and you cannot write (enqueue) to it unless you are the one who's providing the stream itself which you'll have access to an object called the controller at that point!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// you might want to open the console and try running this code&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;readable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ReadableStream&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;controller&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the &lt;code&gt;start&lt;/code&gt; method runs when you instantiate the &lt;code&gt;ReadableStream&lt;/code&gt; class and then you get the controller (&lt;code&gt;ReadableStreamDefaultController&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;chunk 1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;chunk 2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;chunk 3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// no more data&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When receiving a &lt;code&gt;ReadableStream&lt;/code&gt; from another source, like the &lt;code&gt;fetch&lt;/code&gt; API, you won't have the ability to write data into it because you do not have access to the controller. Additionally, it makes sense, that you cannot modify a response coming from the server :)&lt;/p&gt;

&lt;h4&gt;
  
  
  Pull
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://twitter.com/matteocollina" rel="noopener noreferrer"&gt;Matteo&lt;/a&gt; highlighted the pull method as a better way to add data to readable streams, something I wasn't familiar with. The &lt;code&gt;pull&lt;/code&gt; method is triggered when the consumer actively wants to receive data, unlike &lt;code&gt;start&lt;/code&gt;, which activates when the readable stream is created.&lt;/p&gt;

&lt;p&gt;In the earlier examples, data was added without considering if the consumer needed it. In such cases, the stream behaves like a push source. By using the &lt;code&gt;pull&lt;/code&gt; method, we transform our stream into a pull source. This allows the stream to provide the necessary data only when needed. &lt;a href="https://streams.spec.whatwg.org/#example-rs-pull" rel="noopener noreferrer"&gt;Here's&lt;/a&gt; an example.&lt;/p&gt;

&lt;h4&gt;
  
  
  Reader
&lt;/h4&gt;

&lt;p&gt;For reading from a readable stream, we need a reader.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;readable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getReader&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// {done: false, value: 'chunk 1'}&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// {done: false, value: 'chunk 2'}&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// {done: false, value: 'chunk 3'}&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// {done: true, value: undefined}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;what a reader does is lock the stream to itself so no other reader would read from the stream. I guess because it's jealous, just kidding 😄. The reason is that a stream handles one chunk at a time, and if we have two readers, those two readers might be at a different stage of reading (consuming) from the parent stream, or one of them might cancel the stream which would interfere with the other reader (consumer).&lt;/p&gt;

&lt;h4&gt;
  
  
  Teeing (&lt;code&gt;T&lt;/code&gt;)
&lt;/h4&gt;

&lt;p&gt;I won't go deep into this, but since we cannot have two readers from a single readable stream at the same time, we can tee that readable stream, creating two new readable streams that provide the same data. Teeing would force us to use either of those two new streams. And the original stream will be locked for them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;readable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tee&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// [ReadableStream, ReadableStream]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fminimalistweb.dev%2Fposts%2FExcalidraw%2FT-shape.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fminimalistweb.dev%2Fposts%2FExcalidraw%2FT-shape.svg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  WritableStream
&lt;/h3&gt;

&lt;p&gt;It's a stream you're supposed to write into (even if you don't have access to the controller), so it's the opposite of the &lt;code&gt;ReadableStream&lt;/code&gt;.&lt;br&gt;
But here's the catch, you can also read and track the values that are being written into this stream, but only if you are the one providing this stream. If you get to write data into this stream, you'll be called the &lt;em&gt;producer&lt;/em&gt;.&lt;br&gt;
It's usually a wrapper around the &lt;em&gt;underlying sink&lt;/em&gt; which is a destination for the data, like the filesystem.&lt;br&gt;
We have two ways of getting the data into a writable stream!&lt;/p&gt;
&lt;h4&gt;
  
  
  Readable Streams
&lt;/h4&gt;

&lt;p&gt;When data flows into a readable stream, we can then &lt;em&gt;pipe&lt;/em&gt; that stream into a writable stream. Like reading from the network (readable stream) and writing to a certain file on your disk (writable stream). The process that connects these two is called piping.&lt;/p&gt;

&lt;p&gt;In a project like Waku, this approach is more common!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// readable: our readable stream from the previous example&lt;/span&gt;
&lt;span class="nx"&gt;readable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipeTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WritableStream&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}))&lt;/span&gt;
&lt;span class="c1"&gt;// log: chunk 1&lt;/span&gt;
&lt;span class="c1"&gt;// log: chunk 2&lt;/span&gt;
&lt;span class="c1"&gt;// log: chunk 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Writer
&lt;/h4&gt;

&lt;p&gt;For readable streams, we've been able to lock the stream and read from it directly. The same goes for a writable stream, with writers we'll be able to write into a writable stream directly!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;writable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WritableStream&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;// imagine everytime we get a chunk, we write it directly to a file&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;writer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;writable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getWriter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nx"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;chunk 1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// log: chunk 1&lt;/span&gt;
&lt;span class="nx"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;chunk 2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// log: chunk 2&lt;/span&gt;
&lt;span class="nx"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;chunk 3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// log: chunk 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  TransformStream
&lt;/h3&gt;

&lt;p&gt;It's a stream that is mainly used for transforming the chunks we receive. It gives us two streams, one readable and one writable! We write into the writable, and the chunks will be processed (transformed) by the &lt;code&gt;transform&lt;/code&gt; function that we pass the the TransformStream, and then whatever result will be enqueued to the readable stream. Without the &lt;code&gt;transform&lt;/code&gt; function, it will just act as a bridge between writable streams and readable streams.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// https://streams.spec.whatwg.org/#example-transform-identity&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;writable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;readable&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TransformStream&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;readable&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="cm"&gt;/* ... */&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;writer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;writable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getWriter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mh"&gt;0x73&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x74&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x72&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x65&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x61&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x6D&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x73&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x21&lt;/span&gt;&lt;span class="p"&gt;]));&lt;/span&gt; &lt;span class="c1"&gt;// "streams!"&lt;/span&gt;
&lt;span class="nx"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's a great example, where we pass a readable as the request's body, and then we write body content using a writer from the writable stream.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;readable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;writable&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TransformStream&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nf"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;writer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;writable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getWriter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;readable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getReader&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;olleh&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// 'olleh' -&amp;gt; transform() -&amp;gt; 'hello'&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// {done: false, value: 'hello'}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's an example of how you can reverse a string using the TransformStream!&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;pipeThrough&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;This is a method on readable streams that is useful for when we want to pipe the readable stream to the writable side of the &lt;code&gt;TransformStream&lt;/code&gt;. And it'd return the readable side of the &lt;code&gt;TransformStream&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;transformer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TransformStream&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;// instead of this&lt;/span&gt;
&lt;span class="nx"&gt;readable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipeTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;transformer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;writable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// we can do this&lt;/span&gt;
&lt;span class="nx"&gt;readable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipeThrough&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;transformer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// This would return transformer.readable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;A typical example of constructing &lt;a href="https://streams.spec.whatwg.org/#pipe-chain" rel="noopener noreferrer"&gt;pipe chain&lt;/a&gt; using &lt;a href="https://streams.spec.whatwg.org/#rs-pipe-through" rel="noopener noreferrer"&gt;&lt;code&gt;pipeThrough(transform, options)&lt;/code&gt;&lt;/a&gt; would look like. &lt;a href="https://streams.spec.whatwg.org/" rel="noopener noreferrer"&gt;whatwg&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// https://streams.spec.whatwg.org/#example-pipe-chain&lt;/span&gt;
&lt;span class="nx"&gt;httpResponseBody&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipeThrough&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;decompressorTransform&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipeThrough&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ignoreNonImageFilesTransform&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipeTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mediaGallery&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// a pipe chain&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Definitions
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Internal queue
&lt;/h4&gt;

&lt;p&gt;For readable streams, it's data that has been enqueued to the stream but hasn't been read by the consumer (a reader or another writable/transform stream).&lt;br&gt;
For writable streams, it's the data that has been written to the stream itself but still hasn't been written into the underlying sink (file system for instance).&lt;/p&gt;

&lt;h4&gt;
  
  
  High water mark
&lt;/h4&gt;

&lt;p&gt;The boundary for the amount of data that the stream can handle in the internal queue is called high water mark. If we cross that boundary, then &lt;em&gt;backpressure&lt;/em&gt; would be applied.&lt;/p&gt;

&lt;h4&gt;
  
  
  Backpressure
&lt;/h4&gt;

&lt;p&gt;A signal from the consumer stream to the parent stream slows the amount of data we receive (read or write), so we first handle the current data that we have in the current internal queue. &lt;/p&gt;

&lt;p&gt;And that's it! The concept of Streams might mean different things in different contexts, but the core idea is handling small pieces of data sequentially, no matter if it's Video Streaming or React Server Components. The concept is not that complex but generating streaming-compatible data is the challenging part, and I guess that's why it took the React team years to reach what we call today React Server components.   &lt;/p&gt;

&lt;p&gt;I'm &lt;a href="https://twitter.com/asleMammadam" rel="noopener noreferrer"&gt;Mohammad&lt;/a&gt; and this is my first essay and post on my blog, would be happy to hear your thoughts. Don't hesitate to say hello in my DM! I'm also available for hire, so let's discuss that if you have an opportunity :)    &lt;/p&gt;

&lt;h2&gt;
  
  
  Reviewers
&lt;/h2&gt;

&lt;p&gt;Thanks to all of my friends who reviewed this document.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/noriSte" rel="noopener noreferrer"&gt;Stefano Magni&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/barelyreaper" rel="noopener noreferrer"&gt;Reaper&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/matteocollina/" rel="noopener noreferrer"&gt;Matteo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/mvllow" rel="noopener noreferrer"&gt;Mvllow&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/dai_shi" rel="noopener noreferrer"&gt;Daishi Kato&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/puruvjdev" rel="noopener noreferrer"&gt;Puru&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/AmirSa_12" rel="noopener noreferrer"&gt;Amir&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://streams.spec.whatwg.org/" rel="noopener noreferrer"&gt;https://streams.spec.whatwg.org/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nodesource.com/blog/understanding-streams-in-nodejs/" rel="noopener noreferrer"&gt;https://nodesource.com/blog/understanding-streams-in-nodejs/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.pubnub.com/guides/http-streaming/" rel="noopener noreferrer"&gt;https://www.pubnub.com/guides/http-streaming/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://cycle.js.org/streams.html" rel="noopener noreferrer"&gt;https://cycle.js.org/streams.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>streaming</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>The useEffect Cheatsheet</title>
      <dc:creator>Mohammad Bagher Abiyat</dc:creator>
      <pubDate>Fri, 09 Apr 2021 14:59:18 +0000</pubDate>
      <link>https://forem.com/aslemammad/the-useeffect-cheatsheet-4e9i</link>
      <guid>https://forem.com/aslemammad/the-useeffect-cheatsheet-4e9i</guid>
      <description>&lt;p&gt;When I started ReactJS, I really enjoyed my first steps, component, props and many fun things about react. One of that features was &lt;code&gt;useEffect&lt;/code&gt;; it was fun but complicated for me while I struggled to learn it. &lt;/p&gt;

&lt;p&gt;Now I want to share my mental model in this small cheat sheet to help you learn &lt;code&gt;useEffect&lt;/code&gt; better or own a better mental model. &lt;/p&gt;

&lt;h2&gt;
  
  
  Philosophy
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;useEffect&lt;/code&gt; is not a lifecycle hook. It's a mechanism for synchronizing side-effects (&lt;code&gt;fetch&lt;/code&gt;, &lt;code&gt;setTimeout&lt;/code&gt;, ...) with the state of your app. &lt;a href="https://epicreact.dev/myths-about-useeffect"&gt;EpicReact.dev&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The main goal is not using &lt;code&gt;useEffect&lt;/code&gt; for the component lifecycle but using it to do stuff when state-changes (re-renders).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// A: run whenever the deps changes&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// B: Optional, runs before 1, we call this the clean-up function&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;deps&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// deps is Optional too&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;useEffect&lt;/code&gt;'s running steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1: Run A&lt;/li&gt;
&lt;li&gt;2: Wait for new state changes (component re-renders)&lt;/li&gt;
&lt;li&gt;3: If the &lt;code&gt;deps&lt;/code&gt; changed

&lt;ul&gt;
&lt;li&gt;Run B to cleanup the previous render's side-effects&lt;/li&gt;
&lt;li&gt;Go to 2&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Dependencies
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No dependency&lt;/strong&gt;: the side-effect function (A) will run on every state-change (re-render)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// I depend on everything, I'll run on every re-render&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Empty Array&lt;/strong&gt;: There's nothing to listen to its changes, so it'll run the side-effect function just one time at the state initialization (first render)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// I depend on nothing, I'll run just one time&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Non-Empty Array&lt;/strong&gt;: The side-effect function runs on every dependency changes (at least one of the dependencies)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// I depend on state1, state2 and prop1&lt;/span&gt;
  &lt;span class="c1"&gt;// I'll run on every change of these dependencies&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;state2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;prop1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Each Render Has Its Own Effects
&lt;/h2&gt;

&lt;p&gt;I really love the "&lt;a href="https://overreacted.io/a-complete-guide-to-useeffect/#each-render-has-its-own-effects"&gt;Each Render has its own Effects&lt;/a&gt;" title; I think almost all hooks rely on that title. We should note that every render has its own function body and its own values. The same goes for the side-effect function; check this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;let's do some fake state changes and see what happens to the side-effect function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// in the first render, `count` is 0&lt;/span&gt;
&lt;span class="c1"&gt;// The side-effect function is going to be like this&lt;/span&gt;
&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// assume we change `count` to 1 (setCount(1)), next render is like that&lt;/span&gt;
&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// and so on...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's how &lt;code&gt;useEffect&lt;/code&gt; works around dependencies.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/Aslemammad/Cheatsheets/blob/master/useEffect.pdf"&gt;PDF&lt;/a&gt; on GitHub&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope you enjoyed this Cheatsheet. Don't forget to share and send reactions to my article. If you wanted to tell me something, tell me on &lt;a href="https://twitter.com/aslemammadam"&gt;Twitter&lt;/a&gt; or mention me anywhere else, You can even subscribe to my &lt;a href="https://bugged.dev/newsletter"&gt;newsletter&lt;/a&gt; and follow me on &lt;a href="https://github.com/Aslemammad"&gt;Github&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>career</category>
    </item>
    <item>
      <title>Scheduling in web</title>
      <dc:creator>Mohammad Bagher Abiyat</dc:creator>
      <pubDate>Fri, 26 Mar 2021 14:54:23 +0000</pubDate>
      <link>https://forem.com/aslemammad/scheduling-in-web-53jh</link>
      <guid>https://forem.com/aslemammad/scheduling-in-web-53jh</guid>
      <description>&lt;p&gt;Imagine doing 10 tasks continuously; maybe we don't get tired after those 10 tasks, but how about 50 tasks or 100 tasks? We cannot even do this amount of tasks continuously.&lt;/p&gt;

&lt;p&gt;But there's one solution for that, and that's &lt;strong&gt;scheduling&lt;/strong&gt;. With scheduling, we can do any amount of tasks easily, and we're going to increase our productivity. For example, After every 10 tasks, we take a rest for 5 minutes, or better than that, we can keep doing tasks until we get tired, then we take a rest and continue doing the rest of the tasks. &lt;/p&gt;

&lt;p&gt;The same goes for javascript, For example, this code will block our thread for 5 seconds, and everything will stop working.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`I'm blocking your thread for 5sec`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But that's not fair, because it's not heavy work. Libraries like Reactjs can handle 100x heavier work without blocking the browser. The reason is that such libraries use scheduling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scheduling 🤩
&lt;/h2&gt;

&lt;p&gt;Scheduling is about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;work queue&lt;/em&gt;: list of works that we want to schedule and get done&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide"&gt;micro &amp;amp; macro&lt;/a&gt; tasks&lt;/em&gt;: two different kinds of tasks which we can move our own tasks into one of these phases, so the browser's going to handle it later(micro task) or immediately(macro task or task)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;First, each time a task (macro task) exits, the event loop checks to see if the task is returning control to other JavaScript code. If not, it runs all of the microtasks in the microtask queue. The microtask queue is, then, processed multiple times per iteration of the event loop, including after handling events and other callbacks. &lt;a href="http://developer.mozilla.org/"&gt;&lt;em&gt;developer.mozilla.org&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now it's time to schedule some stuff, let's see.&lt;/p&gt;

&lt;p&gt;First of all, imagine we have many expensive work units in our work queue, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;expensive&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I wanna block the event loop&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;workQueue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000000&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;fill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;expensive&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [expensive, expensive, ...]&lt;/span&gt;
&lt;span class="c1"&gt;// 1000000 amount of work units 🤯🤯&lt;/span&gt;
&lt;span class="c1"&gt;// workQueue.map(job =&amp;gt; job()) will destroy the event loop and everything will stop working&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we want to schedule these units, we have to consider two things: a &lt;em&gt;deadline&lt;/em&gt; for running the units. If it's done, we yield to the browser to handle the user input events and then return to run the rest of the units; second, a user input event (clicking, typing, ...) happened when running the units, so we yield back immediately to the browser again, for checking if we have user input events, we use &lt;a href="https://bugged.dev/post/Next-generation-web-with-isInputPending"&gt;isInputPending&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's set the deadline time, which I prefer setting it as one frame.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;DEADLINE_TIME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="c1"&gt;// 1000ms / 60frames&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For now, we need to create the &lt;code&gt;schedule&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;schedule&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;DEADLINE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;performance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;DEADLINE_TIME&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;workQueue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;scheduling&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;isInputPending&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;performance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;DEADLINE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Yield to the browser if we have to handle an input event, or we're out of time.&lt;/span&gt;
      &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;schedule&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// re-running the schedule function later as a macro task&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// stop&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// execute the current work unit &lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;workQueue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nf"&gt;job&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So if we have a user input event waiting or we're out of time, we put our &lt;code&gt;schedule&lt;/code&gt; function on a different part of the event loop (&lt;code&gt;setTimeout&lt;/code&gt;), so the browser can handle the input event and other things that sit before it; after that, It's going to re-run it and let the &lt;code&gt;schedule&lt;/code&gt; function execute the rest of the units if possible.&lt;/p&gt;

&lt;p&gt;Now, if you try running the &lt;code&gt;schedule&lt;/code&gt; function, 1 million logs will not stop everything from working, and everything's going to work well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;schedule&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it, easy and fast like that.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed this article. Don't forget to share and send reactions to my article. If you wanted to tell me something, tell me on &lt;a href="https://twitter.com/aslemammadam"&gt;Twitter&lt;/a&gt; or mention me anywhere else, You can even subscribe to my &lt;a href="https://bugged.dev/newsletter"&gt;newsletter&lt;/a&gt; and follow me on &lt;a href="https://github.com/Aslemammad"&gt;Github&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>performance</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Next-generation web with isInputPending</title>
      <dc:creator>Mohammad Bagher Abiyat</dc:creator>
      <pubDate>Tue, 16 Mar 2021 03:43:41 +0000</pubDate>
      <link>https://forem.com/aslemammad/next-generation-web-with-isinputpending-4ep9</link>
      <guid>https://forem.com/aslemammad/next-generation-web-with-isinputpending-4ep9</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was originally posted on my personal &lt;a href="https://bugged.dev/post/Next-generation-web-with-isInputPending" rel="noopener noreferrer"&gt;blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As we know, almost everything in javascript gets done in a single thread, which gives us a unique model of controlling our work in the browser. With a single thread, I mean when we put an infinite loop in our code, it's going to block our page from doing other works, and everything's going to stop suddenly. &lt;/p&gt;

&lt;h2&gt;
  
  
  Does it matter? 🤔
&lt;/h2&gt;

&lt;p&gt;That's why we say javascript does one thing at a time, and it's not a parallel language. For example, we have two ways to optimize our code for the user in the browser; first, we can complete all of the works we have and get them done, then we're going to listen for the user's interactions (events), which prevents the user from interacting with the page for seconds (maybe less) sometimes, but it has a fast load time. &lt;br&gt;
The second way is responding fast, which we split our code into blocks and schedule/run them (I'll explain scheduling in the next article). After getting done every block, we yield to the browser to check if there's a chance of having user/browser events to respond to; thus, we can respond to the user faster, but we'll have a slower load time, and that's how many frameworks/libraries work. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6xdagamug4insn9h9sm4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6xdagamug4insn9h9sm4.png" alt="Load fast or respond fast, by web.dev"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;code&gt;isInputPending()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;But what if we check if there is a waiting event without yielding to the browser. Yes, it's now possible with the experimental &lt;code&gt;isInputPending&lt;/code&gt;. As you saw, without &lt;code&gt;isInputPending&lt;/code&gt;, after every block of work, we yielded to the browser compulsory without knowing even if we have a pending event or not. But with &lt;code&gt;isInputPending&lt;/code&gt;, there's no need to yield to the browser like a loop; instead, we check if there's a pending input, we yield; if not, we'll go to the next job/task. So we're going to have both fast loading and fast responding at the same time. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpm77g3lvkvzdinr97pt1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpm77g3lvkvzdinr97pt1.png" alt="isInputPending, by web.dev"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  How does it work?
&lt;/h2&gt;

&lt;p&gt;Let's create an example to see how it works and when we're going to use it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;isInputPending&lt;/code&gt; is an experimental tool, so I don't recommend using it now, and in the future, it's going to help frameworks/libraries schedulers. So it's for packages more than personal/business uses.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Assume we have a huge to calculate function called &lt;code&gt;expensive&lt;/code&gt;, And we're going to run it without breaking the web page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;expensive&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/* Some expensive calculations */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;calculate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scheduling&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isInputPending&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* 
      if there's a pending input, yield to the browser, 
      cancel the operation, and put the function in another part of the event loop,
      to check and run it again if it's possible (if there's no pending input)
    */&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;calculate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// if there's no pending input, run the expensive function&lt;/span&gt;
  &lt;span class="nf"&gt;expensive&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;calculate&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it, and that's how we're going to be performant and fast in the future.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To learn more about event loop and zero delays setTimeout, Check out &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#zero_delays" rel="noopener noreferrer"&gt;this&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I hope you enjoyed this article and learned something new. Don't forget to share and send reactions to my article. If you wanted to tell me something, tell me on &lt;a href="https://twitter.com/aslemammadam" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; or mention me anywhere else, You can even subscribe to my &lt;a href="https://bugged.dev/newsletter" rel="noopener noreferrer"&gt;newsletter&lt;/a&gt; and follow me on &lt;a href="https://github.com/Aslemammad" rel="noopener noreferrer"&gt;Github&lt;/a&gt;. 👋🏻&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;isInputPending by &lt;a href="https://web.dev/isinputpending" rel="noopener noreferrer"&gt;web.dev&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>performance</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How JSX works?</title>
      <dc:creator>Mohammad Bagher Abiyat</dc:creator>
      <pubDate>Thu, 11 Mar 2021 19:46:45 +0000</pubDate>
      <link>https://forem.com/aslemammad/how-jsx-works-1ece</link>
      <guid>https://forem.com/aslemammad/how-jsx-works-1ece</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;I put a &lt;a href="https://twitter.com/asleMammadam/status/1369633033737928712"&gt;post&lt;/a&gt; on my Twitter account and had a brief talk on how JSX works, So I want to explain more about it here.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Every ReactJS developer knows about the jsx magic, it let us write some HTML in javascript, and that's how the magic happens. We can do this kind of stuff in tools like babel.&lt;/p&gt;

&lt;p&gt;For example, we have a babel plugin named &lt;code&gt;@babel/plugin-transform-react-jsx&lt;/code&gt; that gives us the ability to work with JSX syntax.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;We just need &lt;code&gt;@babel/plugin-transform-react-jsx&lt;/code&gt; to start, you can install it with npm or yarn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; @babel/plugin-transform-react-jsx
&lt;span class="c"&gt;# or&lt;/span&gt;
yarn add &lt;span class="nt"&gt;-D&lt;/span&gt; @babel/plugin-transform-react-jsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Magic
&lt;/h2&gt;

&lt;p&gt;It's the time to dig into the magic and see what happens inside. In React 17, React core team replaced the classic &lt;code&gt;React.createElement&lt;/code&gt; with the &lt;code&gt;jsx&lt;/code&gt; and &lt;code&gt;jsx&lt;/code&gt; functions, so you don't need to import React anymore to write jsx. Now, the &lt;code&gt;@babel/plugin-transform-react-jsx&lt;/code&gt; uses the &lt;code&gt;jsx&lt;/code&gt; function by default now, check out this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;profile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"avatar.png"&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"profile"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, babel is going to compile that jsx to this plain javascript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;jsx&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;_jsx&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react/jsx-runtime&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;jsxs&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;_jsxs&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react/jsx-runtime&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;profile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;_jsxs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nf"&gt;_jsx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;img&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;avatar.png&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;profile&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}),&lt;/span&gt;
    &lt;span class="nf"&gt;_jsx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;h3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's how the magic works, easy as that!&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's be a magician
&lt;/h2&gt;

&lt;p&gt;We all love to do magical things, why we don't do it now, so let's do it! We need that babel plugin to do some magic. &lt;/p&gt;

&lt;p&gt;Now we can tell the plugin to use our own function instead of react's functions. For that, we need to write a small comment to tell it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** @jsx logJsx */&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we tell it, we want to use &lt;code&gt;logJsx&lt;/code&gt; for jsx syntax. For the &lt;code&gt;logJsx&lt;/code&gt; function, we turn the jsx into a logged statement like "It's a div, Hello Mohammad". Now let's define the &lt;code&gt;logJsx&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// the babel plugin is going to pass this function the type and props/attributes &lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;logJsx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`It's a &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We did it, check this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;title&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Mohammad"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;title&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// type: "title" props: { name: "Mohammad" }&lt;/span&gt;
&lt;span class="c1"&gt;// console: It's a title, Hello Mohammad&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's how jsx works, but in React, instead of logging, it gives us an object like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;title&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mohammad&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;_owner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;_store&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we saw what's going on underneath in React. If you know any other magic, comment it or mention me on Twitter and tell me about it. &lt;/p&gt;

&lt;p&gt;I hope you enjoyed this article and learned something new. Don't forget to share and send reactions to my article. If you wanted to tell me something, tell me on &lt;a href="https://twitter.com/aslemammadam"&gt;Twitter&lt;/a&gt; or mention me anywhere else, You can even subscribe to my &lt;a href="https://bugged.dev/newsletter"&gt;newsletter&lt;/a&gt; and follow me on &lt;a href="https://github.com/Aslemammad"&gt;Github&lt;/a&gt;. 👋🏻&lt;/p&gt;

</description>
      <category>react</category>
      <category>beginners</category>
      <category>javascript</category>
      <category>babel</category>
    </item>
    <item>
      <title>Big useEffect mistake</title>
      <dc:creator>Mohammad Bagher Abiyat</dc:creator>
      <pubDate>Sun, 07 Mar 2021 15:54:47 +0000</pubDate>
      <link>https://forem.com/aslemammad/big-useeffect-mistake-7jm</link>
      <guid>https://forem.com/aslemammad/big-useeffect-mistake-7jm</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was originally posted on my personal &lt;a href="https://bugged.dev/post/Big-useEffect-mistake"&gt;blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In Reactjs, we encountered infinite loops or useless re-rendering a lot. It's something unavoidable, but we can be more attentive about it sometimes. One of the reasons that cause us this kind of problems is useEffect; I'm going to talk about one of the mistakes we can make with it and learn how to be aware of it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The mistake 😮
&lt;/h2&gt;

&lt;p&gt;Check this out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;re-rendering&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;someData&lt;/span&gt; &lt;span class="p"&gt;}])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if we do &lt;code&gt;setState&lt;/code&gt;multiple times, we're going to see this in console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;re&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;rendering&lt;/span&gt;
&lt;span class="nx"&gt;re&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;rendering&lt;/span&gt;
&lt;span class="nx"&gt;re&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;rendering&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wait; what? We just passed &lt;code&gt;someData&lt;/code&gt; as a dependency list; why it logs that? That's not even related to &lt;code&gt;state&lt;/code&gt;. Yes, it's not related, but pay more attention to the dependency list; We passed an inline object, which means it's always different from its previous version; we create it every time we cause a re-render to the component. Check this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;someData&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;someData&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="c1"&gt;// all are inline and have different references&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;useEffect&lt;/code&gt; somehow cache the dependency list and check if it's equal to the next value. That's why inline non-primitive values (&lt;code&gt;[]&lt;/code&gt;, &lt;code&gt;{}&lt;/code&gt;, &lt;code&gt;{...data}&lt;/code&gt;, ...) are always different in this tool's eyes.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;Symbol()&lt;/code&gt; is an exception here, it's different every time we create it, but it's a primitive value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For example, check this, I saw many developers that they check part of an array like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;re-rendering&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)]);&lt;/span&gt; 
&lt;span class="cm"&gt;/* 
    prevVal = state.slice(0, 2) // first render
        ***
    nextVal = state.slice(0, 2) // second render
        ***
    prevVal === nextVal // false
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The expected behaviour is checking 1 to 3 values, but it doesn't work like that because the &lt;code&gt;slice&lt;/code&gt; method will always return a new array (reference).&lt;/p&gt;

&lt;p&gt;I hope you enjoyed this article. Don't forget to share and send reactions to my article. If you wanted to tell me something, tell me on &lt;a href="https://twitter.com/aslemammadam"&gt;Twitter&lt;/a&gt; or mention me anywhere else, You can even subscribe to my &lt;a href="https://bugged.dev/newsletter"&gt;newsletter&lt;/a&gt; and follow me on &lt;a href="https://github.com/Aslemammad"&gt;Github&lt;/a&gt;. 👋🏻&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>performance</category>
    </item>
    <item>
      <title>React Context without Provider + useMutableSource</title>
      <dc:creator>Mohammad Bagher Abiyat</dc:creator>
      <pubDate>Wed, 24 Feb 2021 17:26:59 +0000</pubDate>
      <link>https://forem.com/aslemammad/react-context-without-provider-usemutablesource-4aph</link>
      <guid>https://forem.com/aslemammad/react-context-without-provider-usemutablesource-4aph</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was originally posted on my personal &lt;a href="https://bugged.dev/post/React-Context-without-Provider-+-useMutableSource"&gt;blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A while ago, I was reading an RFC from react's RFCs called &lt;a href="https://github.com/reactjs/rfcs/blob/master/text/0147-use-mutable-source.md"&gt;&lt;code&gt;useMutableSource&lt;/code&gt;&lt;/a&gt;; it was an experimental feature that, in a nutshell, lets you safely read, write and even edit an external source (outside of the react components tree). It's a banger feature, which I'm really chuffed for it, but it's experimental at the same time. You may know that I'm working on an Open-source state-management library called jotai. This library announced a new feature, the Provider-less mode. To know what it is, think of React Context, but no need for a &lt;code&gt;Provider&lt;/code&gt; component, it's not exactly that, but it gives you the idea.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why a new one?
&lt;/h2&gt;

&lt;p&gt;Yea, we have patterns and libraries that allow us to read and write from an external source, but as I said, this one lets you do things safely; no tearing anymore.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tearing
&lt;/h2&gt;

&lt;p&gt;Think of tearing as something like if we have a value(state) that A and B read from it, but somehow in the rendering, the value changes. The B component is later than A, So in Rendering, the value in the A component is 0, and in the newer component (B), the value is 1. We call this tearing; it means you see two different values in the viewport from one exact source. It's a new and hard to understand implementation in React concurrent mode; for more information, see &lt;a href="https://stackoverflow.com/questions/54891675/what-is-tearing-in-the-context-of-the-react-redux"&gt;this&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Experimental, Why should I use it?
&lt;/h2&gt;

&lt;p&gt;So I thought about this, we have two options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Experimental version of react: &lt;code&gt;yarn add react@experimental&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Consistent version of &lt;code&gt;useMutableSource&lt;/code&gt;, you can copy paste it from &lt;a href="https://gist.github.com/Aslemammad/0e0a4eff774f991df47fa7c6cd219174"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I recommend the second option because it's not going to change, and good for now as long as we don't have &lt;code&gt;useMutableSource&lt;/code&gt; in a major react version.&lt;/p&gt;

&lt;h2&gt;
  
  
  Context with no Provider
&lt;/h2&gt;

&lt;p&gt;I think we have reached what brought you here, but wait before all of this, don't forget to look at my &lt;a href="https://github.com/Aslemammad"&gt;Github&lt;/a&gt; and &lt;a href="https://twitter.com/aslemammadam"&gt;Twitter&lt;/a&gt;; you're going to see cool stuff there and help me with my learning journey too. So let's start.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Some of the code below is written with typescript, so it will be more understandable, even for people who don't know typescript.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Start
&lt;/h3&gt;

&lt;p&gt;First we need to create a simple global object, which contains three properties:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;globalStore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;listeners&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;state&lt;/code&gt;: simple value like react Context value&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;version&lt;/code&gt;: important part that has to change whenever any part of the state changes&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;listeners&lt;/code&gt;: a set of functions that we call them every time we change part of the &lt;code&gt;state&lt;/code&gt;, so we notify them about the changes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we need to create a mutable source from &lt;code&gt;globalStore&lt;/code&gt; and give it the version, so it'll help it with triggering new changes, so we're going to access it in &lt;code&gt;getSnapshot&lt;/code&gt; and &lt;code&gt;subscribe&lt;/code&gt;; we'll talk about these soon.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;globalStoreSource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createMutableSource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;globalStore&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;globalStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;version&lt;/span&gt; &lt;span class="c1"&gt;// (store) =&amp;gt; store.version (Optional) if you use the consistent and non-experimental version of useMutableSource&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it's the time to talk about &lt;code&gt;getSnapshot&lt;/code&gt;; in a nutshell, it's a function that &lt;code&gt;useMutableSource&lt;/code&gt; returns its returned value whenever the state changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getSnapshot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;globalStore&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;setState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="na"&gt;cb&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;cb&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;version&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listeners&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// remove all the old references&lt;/span&gt;
    &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; 
    &lt;span class="c1"&gt;// we cache the result to prevent the useless re-renders&lt;/span&gt;
    &lt;span class="c1"&gt;// the key (store.state) is more consistent than the { ...store.state },&lt;/span&gt;
    &lt;span class="c1"&gt;// because this changes everytime as a new object, and it always going to create a new cache&lt;/span&gt;
    &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// check the above if statement, if the store changed completely (reference change), we'll make a new result and new state&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [state, setState]&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="c1"&gt;// later: const [state, setState] = useMutableSource(...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Take a look at the &lt;code&gt;setState&lt;/code&gt; function, first we use &lt;code&gt;cb&lt;/code&gt; and pass it the previous state, then assign its returned value to our state, then we update the store version and notify all the listeners of the new change.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;we used the spread operator &lt;code&gt;({ ...store.state })&lt;/code&gt; because we have to clone the value, so we make a new reference for the new state object and disable direct mutations.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We don't have any &lt;code&gt;listener&lt;/code&gt; yet, so how we can add one? with the &lt;code&gt;subscribe&lt;/code&gt; function, take a look at this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;subscribe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;globalStore&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listeners&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listeners&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function's going to get called by &lt;code&gt;useMutableSource&lt;/code&gt;, So it passes &lt;code&gt;subscribe&lt;/code&gt; two parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;store&lt;/code&gt;: which is our original store&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;callback&lt;/code&gt;: this is going to cause our component a re-render (by &lt;code&gt;useMutableSource&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So when &lt;code&gt;useMutableSource&lt;/code&gt; calls the subscribe, we're going to add the &lt;code&gt;callback&lt;/code&gt; to our listeners. Whenever something changes in the state (&lt;code&gt;setState&lt;/code&gt;), we call all of our listeners so that the component will get re-rendered. That's how we have the updated value every time with &lt;code&gt;useMutableSource&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So you may wonder we delete the callback in return, the answer is that when the component unmounts, &lt;code&gt;useMutableSource&lt;/code&gt; will call &lt;code&gt;subscribe()&lt;/code&gt;, or in another term, we call it &lt;code&gt;unsubscribe&lt;/code&gt;. When it gets deleted, we'll no longer call a useless callback that will cause a re-render to an unmounted (or sometimes an old) component.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;useContext&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Now we reached the end line, don't think too much about the name, we just wanted to mimic the Provider-less version of React context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;useMutableSource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;globalStoreSource&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;getSnapshot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// returns [state, setState]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can use this function everywhere we want. Take a look at this example, or if you want, you could go straight for the &lt;a href="https://codesandbox.io/s/usemutablesource-react-context-spyhu?file=/src/App.tsx"&gt;codesandbox&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Display1&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Display1 component count: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Display2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Display2 component count: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Changer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[,&lt;/span&gt; &lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;
      &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="nx"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt;
      &lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      +1
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"App"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Display1&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Display2&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Changer&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now whenever you click the +1 button, you can see the beautiful changes without any &lt;code&gt;Provider&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed this article, and don't forget to share and reaction to my article. If you wanted to tell me something, tell me on &lt;a href="https://twitter.com/aslemammadam"&gt;Twitter&lt;/a&gt; or mention me anywhere else, You can even subscribe to my &lt;a href="https://bugged.dev/newsletter"&gt;newsletter&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cover image: Experiment, Nicolas Thomas, unsplash&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>performance</category>
      <category>javascript</category>
    </item>
    <item>
      <title>A wild ride in my 16th year</title>
      <dc:creator>Mohammad Bagher Abiyat</dc:creator>
      <pubDate>Mon, 15 Feb 2021 15:04:02 +0000</pubDate>
      <link>https://forem.com/aslemammad/a-wild-ride-in-my-16th-year-444n</link>
      <guid>https://forem.com/aslemammad/a-wild-ride-in-my-16th-year-444n</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was originally posted on my personal &lt;a href="https://bugged.dev/post/A-wild-ride-in-my-16th-year"&gt;blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I'm not 16 anymore from now on (Feb 15, HBD to myself🎉), But my 16th (2020-21) year on earth was the first year of my professional career where I started learning and making things seriously, so be with me and see the highlights. Hope you like this wild journey.&lt;/p&gt;

&lt;h2&gt;
  
  
  2020🧛🏻
&lt;/h2&gt;

&lt;p&gt;The most miserable year in the 21st century, everyone knows about it, we lost many friends, many jobs and many things I'm so sorry about. Still, at the same time, it was a productive year for me, no hanging out, less school and it empowered my programmers-life lifestyle. I think every programmer feels the same, doesn't?&lt;br&gt;
So let me start the main thing and tell you about my career.&lt;/p&gt;

&lt;h2&gt;
  
  
  The beginning
&lt;/h2&gt;

&lt;p&gt;Actually I didn't expect such a year specifically for me. I had many ups and downs, but they made my year so exciting. I didn't know that I would be an active Github user or be in a job position (Thank you, &lt;a href="https://github.com/HosseinShabani"&gt;Hossein&lt;/a&gt;, for helping me in that); it was all unexpected. I expected a typical year like any standard years I've passed, but it was really different. &lt;/p&gt;

&lt;h2&gt;
  
  
  Svelte vs React🤔
&lt;/h2&gt;

&lt;p&gt;I was learning HTML/CSS and javascript and practising them more and more, but everyone kept telling me that I have to choose a framework/library. Most beginners had the Vue vs React or React vs Angular dilemma, but I had a different one you saw in the heading. I was exploring &lt;a href="http://dev.to"&gt;dev.to&lt;/a&gt;; everyone's talking about svelte future, Learning it, and why we should. However, we had many react developers, and react itself grows day by day, I didn't know which to choose. I randomly chose svelte and learned many great things about it. Actually, even I don't know what to suggest to beginners; I may tell svelte to newbies first because, don't want to exaggerate but if you deep dive into react hole, you won't get out of it quickly. &lt;/p&gt;

&lt;h2&gt;
  
  
  So Svelte
&lt;/h2&gt;

&lt;p&gt;Nice looking technology, no challenging concepts and no state-management options anymore, It's literally all-in-one. I really loved its docs, and you can take a look &lt;a href="https://svelte.dev/tutorial/basics"&gt;here&lt;/a&gt;, learn things so fast with the live examples in front of you. Absolutely I had some difficulties in learning svelte, but when I take a look at them now, they weren't that hard.&lt;/p&gt;

&lt;p&gt;I really loved the way svelte treats global stores, syntax and reactivity, so genius. I think svelte is so potent that it needs a more significant community to take over most modern technologies. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/blackestwhite"&gt;My friend&lt;/a&gt;'s team and I planned to make an idea into a real-world project, so the front-end was with me, I built it with svelte, the experience was incredible, but unfortunately, we cancelled the project. Here's the project &lt;a href="https://github.com/ArimaTeam/LyricBox"&gt;repository&lt;/a&gt; (Don't bother me for my dumb commit messages).&lt;/p&gt;

&lt;h2&gt;
  
  
  React😮
&lt;/h2&gt;

&lt;p&gt;I joined many communities, and everyone was a reactjs developer, I couldn't even talk about React; So I had to join reactjs. When I joined react community, I found out why they don't talk about things except react. I just said that's it, that is what I want. &lt;/p&gt;

&lt;p&gt;I started practising and doing stuff, shared some of them in Github and some not. Started reading articles about reactjs from different authors. Then I made my first ever npm package called contextism, and I was inspired by one of &lt;a href="https://github.com/kentcdodds"&gt;kent&lt;/a&gt;'s posts about reactjs context API, Seeing others use your stuff is one of the best feelings ever. That's how I fell in love with creating and helping others. &lt;/p&gt;

&lt;h2&gt;
  
  
  Open-source😌
&lt;/h2&gt;

&lt;p&gt;But wait, Contextism wasn't my first open-source activity. It was &lt;a href="https://github.com/disizali/espresso"&gt;Espresso&lt;/a&gt;, So what is Espresso? It's something like expressjs but not for nodejs; it's for denojs, we made it when all developers talked about a new trend called deno. Thanks for &lt;a href="https://github.com/disizali"&gt;Ali Hasani&lt;/a&gt; for bringing me into contributing javascript libraries.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XHft_hFq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/u6fnig4ciuw8lziq5yrn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XHft_hFq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/u6fnig4ciuw8lziq5yrn.png" alt="Hactoberfest T-shirt" width="800" height="1067"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://hacktoberfest.digitalocean.com/"&gt;Hacktoberfest&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;If you don't know about this monthlong celebration, let me tell you that it's one of the best things for open source (as long as spammers don't attend). I contributed to these repositories in this celebration:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://github.com/carbon-app/carbon"&gt;Carbon&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;It has an ugly source code but a challenging one, I refactored some code and sent a pull request to the repository, it was a special feeling for me.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://github.com/pmndrs/jotai"&gt;Jotai&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;How should I start with this? It started with just 3 pull requests for Immerjs combination, and now I'm a collaborator there. I really love this package. If you know Recoil, forget about it, we have a better one, it's jotai. I learned about promise batching, acceptable pull requests and even how to manage a repository. Thanks, &lt;a href="https://github.com/dai-shi"&gt;Daishi&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing✏️
&lt;/h2&gt;

&lt;p&gt;The best way of learning is teaching, that's weird, but that's how it is. I started writing stuff because &lt;a href="https://github.com/sw-yx"&gt;swyx&lt;/a&gt;'s Learn-In-Public inspired me, then I decided to make my own blog with my own domain, That's why I started &lt;a href="https://bugged.dev"&gt;Bugged.dev&lt;/a&gt;. I'm in love with writing, suggest you start too, it's not that hard, but it takes time to dive into the writing routine. It's so fun to share your drafts with your friends and let them review your drafts; I always share them with my friends like &lt;a href="https://github.com/aliPMPAINT"&gt;Ali&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://github.com/pmndrs"&gt;Poimandres&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Contributing to jotai, valtio and eslint-plugin-valtio made and some other libs made me into such a lovely, friendly and active group. We maintain many wondrous things; you can take a look at our Github. Thanks, &lt;a href="https://github.com/drcmda"&gt;Paul&lt;/a&gt;, for letting me in.&lt;/p&gt;

&lt;p&gt;In the end, I'm fortunate for having my family♥️, A supportive and helpful one, and I really wanted to thank them for everything. I have many plans for my 17th. I'll write about them slowly. Lucky too for having such readers hope you liked this post, don't forget to show me your reactions also, really love you all. Alhamdulillah.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cover image: Fuji-Q Highland, Priscilla Du Preez, unsplash &lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>career</category>
    </item>
    <item>
      <title>Javascript dynamic imports + Next.js</title>
      <dc:creator>Mohammad Bagher Abiyat</dc:creator>
      <pubDate>Wed, 10 Feb 2021 10:42:10 +0000</pubDate>
      <link>https://forem.com/aslemammad/javascript-dynamic-imports-next-js-14c0</link>
      <guid>https://forem.com/aslemammad/javascript-dynamic-imports-next-js-14c0</guid>
      <description>&lt;p&gt;This article was originally posted on my personal &lt;a href="https://bugged.dev/post/Next-Dynamic-imports"&gt;blog&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Dynamic import()&lt;/strong&gt; introduces a new function-like form of import that unlocks new capabilities compared to static import. This article compares the two and gives an overview of what’s new. V8 implementation&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I assume you know the regular &lt;strong&gt;import&lt;/strong&gt; syntax, It's cool, but you know, it's too static and strict, if you don't know about it, read &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import"&gt;this&lt;/a&gt; to take the idea. We're gonna take a look at something like it in Next.js.&lt;/p&gt;

&lt;h2&gt;
  
  
  Intro 👋
&lt;/h2&gt;

&lt;p&gt;In this little article, I'm not teaching you about dynamic import, but I'll give some ideas about dynamic imports in &lt;strong&gt;Next.js&lt;/strong&gt; specifically and You should know that I'm a learner like you, Maybe I'll say wrong things, So I expect you to correct them if I did.&lt;/p&gt;

&lt;p&gt;For a review or introduction, We can implement dynamic import like this examples but It's a bad idea, I'll do just for your eyes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 📁 say.js&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;hi&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;bye&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Bye`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// example from https://javascript.info/modules-dynamic-imports&lt;/span&gt;
&lt;span class="c1"&gt;// another file&lt;/span&gt;

&lt;span class="c1"&gt;// regular import &lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;bye&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;hi&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./say.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// dynamic import  &lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;bye&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;hi&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./say.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Next
&lt;/h2&gt;

&lt;p&gt;In Next.js, Where We use it? I don't know specifically, but I know some situations where I use it, For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A huge component annoys us and slows us down in the loading of the page, So We can kick it with*&lt;em&gt;dynamic import&lt;/em&gt;*, That means We load it after the initial loading page (lazily loading it), because We don't want the browser to waste its time on it. That means Next.js split our code into other manageable chunks. With this, We give the ability to load just the needed components.&lt;/li&gt;
&lt;li&gt;handling other huge files like perfect pictures with the same method.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next.js implemented it in another and similar way, because &lt;a href="https://reactjs.org/docs/code-splitting.html"&gt;React.Lazy &amp;amp; Suspense&lt;/a&gt; aren't ready for SSR or ... now. I think there are other reasons, And are.&lt;/p&gt;

&lt;p&gt;The reason why I'm writing this article is that I had problems with &lt;a href="https://web.dev/time-to-first-byte/"&gt;TTFB&lt;/a&gt; in the Blog that you are reading this article in, So I wanted to share here.&lt;/p&gt;

&lt;h3&gt;
  
  
  Huge components
&lt;/h3&gt;

&lt;p&gt;This Blog is a markdown blog, So We need a markdown engine, and inside the markdown, we use code samples and code samples need their syntax highlighter that needs to be passed to the markdown engine or component. Here is the point, The &lt;strong&gt;markdown component&lt;/strong&gt; and &lt;strong&gt;syntax highlighter&lt;/strong&gt; are too huge components, And when I used them regularly, They affect the TTFB (page loading), I used them like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ReactMarkdown&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-markdown/with-html&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 157.2 KB&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Prism&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;SyntaxHighlighter&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-syntax-highlighter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 495.1 KB&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just see the size, It's crazy.&lt;br&gt;
So, I learned about dynamic imports in Next.js, And I implemented it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;dynamic&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;next/dynamic&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Next.js dynamic&lt;/span&gt;
&lt;span class="c1"&gt;// const Component = dynamic(() =&amp;gt; import('Your component'),{...options})&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ReactMarkdown&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dynamic&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-markdown/with-html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🐞...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;SyntaxHighlighter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dynamic&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-syntax-highlighter/dist/cjs/prism&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In React we used React.Lazy syntax but in Next.js, it's a little bit different and We use dynamic instead and wrap the &lt;strong&gt;import()&lt;/strong&gt; in it. There are some options for this method that We can use, like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ssr&lt;/strong&gt;: Default is true, And if you make it false, the component will be handled in the client-side.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;loading&lt;/strong&gt;: Show something when the component is in loading situation.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Others
&lt;/h3&gt;

&lt;p&gt;If you have huge image or something else too, you can handle it using just the &lt;strong&gt;import()&lt;/strong&gt; or &lt;strong&gt;require()&lt;/strong&gt;, We don't need &lt;em&gt;dynamic&lt;/em&gt;, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Image&lt;/span&gt;
        &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`../path/to/image.png`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'w-full'&lt;/span&gt;
    &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Note
&lt;/h2&gt;

&lt;p&gt;Don't be Obsessive like me, Sometimes you don't have a TTFB problem or huge components, So you don't need &lt;strong&gt;dynamic import&lt;/strong&gt;, Imagine you are the browser, and someone thinks that you are too weak for handling 15 lines components, What you're going to do?&lt;br&gt;
It's like using pure components or memo when you don't have any problem with React re-rendering.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed this small article and don't forget to share, and reaction to my article. If you wanted to tell me something, tell me on &lt;a href="https://twitter.com/asleMammadam"&gt;Twitter&lt;/a&gt; or mention me anywhere else. You can subscribe to my newsletter too on my &lt;a href="https://bugged.dev/post/Next-Dynamic-imports"&gt;blog&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;span&gt;Cover picture by &lt;a href="https://unsplash.com/@tannnpro?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Tanya Pro&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/dynamic?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>nextjs</category>
      <category>react</category>
    </item>
    <item>
      <title>Tree shaking goodness</title>
      <dc:creator>Mohammad Bagher Abiyat</dc:creator>
      <pubDate>Tue, 09 Feb 2021 04:35:06 +0000</pubDate>
      <link>https://forem.com/aslemammad/tree-shaking-goodness-2hmp</link>
      <guid>https://forem.com/aslemammad/tree-shaking-goodness-2hmp</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Tree shaking is a term commonly used in the JavaScript context for dead-code elimination. It relies on the static structure of ES2015 module syntax, i.e. import and export. The name and concept have been popularized by the ES2015 module bundler rollup. &lt;a href="https://webpack.js.org/guides/tree-shaking/"&gt;Webpack&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We're not going to deep dive in &lt;strong&gt;tree shaking&lt;/strong&gt;, just talk about its benefits and examples. If you're an npm package developer, You should check out &lt;a href="https://webpack.js.org/guides/tree-shaking/"&gt;Webpack tree shaking&lt;/a&gt; or &lt;a href="https://rollupjs.org/guide/en/#tree-shaking"&gt;Rollup tree shaking&lt;/a&gt; to give the users and developers a better experience.&lt;/p&gt;

&lt;p&gt;Tree shaking is a simple concept to reduce the size of production build to have faster TTFB (Time To First Byte or page load time).&lt;/p&gt;

&lt;h2&gt;
  
  
  Why? 🤔
&lt;/h2&gt;

&lt;p&gt;It allows the bundler to modify some parts of the production code to remove the unused parts. &lt;/p&gt;

&lt;p&gt;Imagine we have a file &lt;strong&gt;meet.js&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// meet.js&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;hi&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;bye&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;bye&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;hi&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now import it in another file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;hi&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./meet.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="nf"&gt;hi&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the point, maybe you thought we just imported the &lt;strong&gt;hi&lt;/strong&gt;, but not, We imported all of the &lt;strong&gt;meet.js&lt;/strong&gt;, that means we have &lt;strong&gt;bye&lt;/strong&gt; in the production, too. So the production build has unused code, so we don't want that.&lt;/p&gt;

&lt;p&gt;In some packages, we do this too, we import the needed parts, but in production, we have unused code when using object destructuring, but there is a way to get rid of the unused code! &lt;/p&gt;

&lt;h2&gt;
  
  
  How? 😬
&lt;/h2&gt;

&lt;p&gt;Here we can use tree shaking, but if the package that we're using implemented the tree shaking feature.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;chunk&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lodash-es/array/chunk&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Almost every package has its way to implement its tree shaking, and it's not wildly different!&lt;/p&gt;

&lt;p&gt;For bigger packages, see What happens when we don't use tree shaking.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;chunk&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lodash-es&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// No tree shaking&lt;/span&gt;
&lt;span class="c1"&gt;// 30Kb just for one function, what! 👎&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;chunk&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lodash-es/array/chunk&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// With tree shaking&lt;/span&gt;
&lt;span class="c1"&gt;// 1Kb for one function, That's awesome 👍&lt;/span&gt;

&lt;span class="c1"&gt;// https://github.com/webpack/webpack/issues/1750&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Note
&lt;/h2&gt;

&lt;p&gt;I think you should use or implement this feature because reducing the build size is so important and the users love the speed. In the previous &lt;a href="https://dev.to/post/Next-Dynamic-imports"&gt;article&lt;/a&gt;, I said to use the dynamic import feature when you have TTFB issues, and if you use it always, It will affect the TTFB and slows it down sometimes, But tree shaking is a great feature, So use it always when you can.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed this small article and don't forget to share, and reaction to my article. If you wanted to tell me something, tell me on Twitter or mention me anywhere else. You can subscribe to my newsletter too on my &lt;a href="https://bugged.dev/post/Tree-shaking"&gt;blog&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>performance</category>
      <category>webdev</category>
      <category>nextjs</category>
    </item>
    <item>
      <title>How to read open source js libraries</title>
      <dc:creator>Mohammad Bagher Abiyat</dc:creator>
      <pubDate>Sat, 06 Feb 2021 14:33:42 +0000</pubDate>
      <link>https://forem.com/aslemammad/how-to-read-open-source-js-libraries-5cfp</link>
      <guid>https://forem.com/aslemammad/how-to-read-open-source-js-libraries-5cfp</guid>
      <description>&lt;p&gt;All of us, for one time at least, have plotted to contribute to one open-source library, it goes well but when we take a look at the source code, we become incapable and dumb, we can't even get our head around it.&lt;/p&gt;

&lt;p&gt;Today I want to show you my approach to read libraries and understand them thoroughly, I don't think it's the best approach, but let's give it a shot, it worked so well for me.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you don't know what library you must start with, I suggest you check &lt;a href="https://kentcdodds.com/blog/what-open-source-project-should-i-contribute-to" rel="noopener noreferrer"&gt;this&lt;/a&gt; article by Kent C Dodds.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Needs
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;VS code( or any code editor that you can attach a debugger to it)&lt;/li&gt;
&lt;li&gt;Debugger&lt;/li&gt;
&lt;li&gt;VS code jest extension( we can work without) or node debugger🤷🏻‍♂️&lt;/li&gt;
&lt;li&gt;Your favourite project( I use &lt;a href="https://github.com/pmndrs/jotai" rel="noopener noreferrer"&gt;jotai&lt;/a&gt; this time)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Sometimes we need to set our own node version on 14 because I saw the debugger has some struggles with the 15 version.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Make sure you have some background of the library docs, then open the library's source code, so let's start our journey and drink the coffee( I don't do actually) because I'm really passionate to get it out of my system. &lt;/p&gt;

&lt;h2&gt;
  
  
  Start
&lt;/h2&gt;

&lt;p&gt;Most of the times I won't open the &lt;code&gt;index.js&lt;/code&gt; file, because many branches of the library tree are there, and we don't have clue about any of that yet. I suggest that you check the tests first, so you can debug the tests and see how the library works. &lt;/p&gt;

&lt;h2&gt;
  
  
  Settings
&lt;/h2&gt;

&lt;p&gt;Jotai has jest tests, so I can debug its tests with &lt;a href="https://marketplace.visualstudio.com/items?itemName=Orta.vscode-jest" rel="noopener noreferrer"&gt;jest&lt;/a&gt; extension. I just need to create a config file there.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;.vscode/launch.json&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"0.2.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"configurations"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"vscode-jest-tests"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"request"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"launch"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"program"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"${workspaceFolder}/node_modules/jest/bin/jest"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"args"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"--runInBand"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"${file}"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"cwd"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"${workspaceFolder}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"console"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"integratedTerminal"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"internalConsoleOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"neverOpen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"disableOptimisticBPs"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I use this config most of the times, but it depends on your project sometimes, for example in the Reactjs source code, you have to link the &lt;code&gt;jest-cli.js&lt;/code&gt; as a program instead of the jest itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Breakpoints
&lt;/h2&gt;

&lt;p&gt;For example, I'd love to see how &lt;code&gt;useAtom&lt;/code&gt; works, so I just need to go to the related test and set a breakpoint on the line that I want to know more about. Then, we can run the &lt;code&gt;vscode-jest-tests&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;config, so it's gonna run the file we were in, and it would be stopped on the line we set a breakpoint⏹️ on.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fk6uv9g2n5xp52iqvz1z2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fk6uv9g2n5xp52iqvz1z2.png" alt="vscode-jest-debugger"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we have to play with the debugger bar( right corner) and watch the code execution flow, we can just set many breakpoints wherever we want and understand what's happening there.&lt;/p&gt;

&lt;p&gt;I hope this article encourages you to do more open source and kills your fears( not all of them actually). It's time to party again, so don't forget to share this little article, It's my first 2021 article after 3 bloody months, I'm trying to post more, so help me to do so with your reactions. Don't forget to take a look at &lt;a href="https://bugged.dev/post/How-to-read-open-source-js-libraries" rel="noopener noreferrer"&gt;my blog&lt;/a&gt; for exclusive articles there.&lt;/p&gt;

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