<?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>DEV Community: Aibol Kussain</title>
    <description>The latest articles on DEV Community by Aibol Kussain (@aibolik).</description>
    <link>https://dev.to/aibolik</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%2F84881%2F5d21cf9c-d5bc-4cbd-a113-0e72cced4aa7.jpeg</url>
      <title>DEV Community: Aibol Kussain</title>
      <link>https://dev.to/aibolik</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aibolik"/>
    <language>en</language>
    <item>
      <title>7 React Challenges</title>
      <dc:creator>Aibol Kussain</dc:creator>
      <pubDate>Thu, 03 Nov 2022 22:49:43 +0000</pubDate>
      <link>https://dev.to/aibolik/7-react-challenges-4elc</link>
      <guid>https://dev.to/aibolik/7-react-challenges-4elc</guid>
      <description>&lt;p&gt;Hello there!&lt;/p&gt;

&lt;p&gt;I've been collecting different &lt;em&gt;interesting challenges&lt;/em&gt; that could be implemented for practice in ReactJS. The list is big and is growing whenever I encounter an interesting piece of UI or functionality. &lt;/p&gt;

&lt;p&gt;Initially, I planned to come up with a course with video materials, where I would show an end result and explain how to do it in ReactJS. However, I figured out it is &lt;em&gt;not that simple&lt;/em&gt; to create a video course. So, I decided to come up with a &lt;strong&gt;list of curated challenges&lt;/strong&gt;, which will cover important topics in ReactJS and guide with relevant hints(but not provide implementation itself). That turned out into 7 challenges and 18 pages PDF.&lt;/p&gt;

&lt;p&gt;So, I am presenting you &lt;a href="https://aibolik.com/7-react-challenges"&gt;"7 React Challenges you can build Today"&lt;/a&gt; PDF. Feel free to grab it on my website and leave me feedback.&lt;/p&gt;

&lt;p&gt;I might still come up with a full-blown course, which will present 20+ ReactJS challenges with the guidance on implementation, so stay tuned for further updates.&lt;/p&gt;

&lt;p&gt;That's it from me. Thanks for your attention.&lt;/p&gt;

</description>
      <category>react</category>
      <category>challenge</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to add dynamic canonical links in Remix application</title>
      <dc:creator>Aibol Kussain</dc:creator>
      <pubDate>Thu, 07 Jul 2022 21:04:54 +0000</pubDate>
      <link>https://dev.to/aibolik/how-to-add-dynamic-canonical-links-in-remix-application-2763</link>
      <guid>https://dev.to/aibolik/how-to-add-dynamic-canonical-links-in-remix-application-2763</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This article was originally written on my personal blog &lt;a href="https://aibolik.com/blog/how-to-add-dynamic-canonical-links-in-remix-application" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I rewrote my blog using Remix.run several months ago, but I faced an issue with &lt;strong&gt;canonical links&lt;/strong&gt; when I started improving the SEO of my blog. After some research about this, I came to a perfect solution that lets me have &lt;strong&gt;dynamic link&lt;/strong&gt; elements that absorb route data. Let me show you how I got to it.&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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2Awke4B4SPRlIzvrWQt1T4QQ.jpeg" 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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2Awke4B4SPRlIzvrWQt1T4QQ.jpeg" alt="Two similar white pages in a orange background"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Make sure you add canonical URLs to all of your pages in Remix app. Credits: Table &lt;a href="https://www.freepik.com/photos/table-flatlay" rel="noopener noreferrer"&gt;flatlay photo&lt;/a&gt; created by freepik&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  What is a canonical URL
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;If you already know what it is and why you need it, jump to the next section.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Simply put, a canonical URL is used by search engines to determine the origin of the content. Suppose your blog post is available through http://, https:// and/or &lt;a href="http://www" rel="noopener noreferrer"&gt;www&lt;/a&gt;. URLs. In that case, it is highly recommended to put a canonical URL that follows some structure, so that search engines will mark other pages as duplicates and &lt;em&gt;give all the credits&lt;/em&gt; to the original content.&lt;/p&gt;

&lt;p&gt;Another popular reason to use canonical URLs is to &lt;strong&gt;syndicate your content&lt;/strong&gt;. If you publish your post on your blog and third-party platforms(like medium.com, dev.to, etc.) Google will consider them as &lt;a href="https://moz.com/learn/seo/duplicate-content" rel="noopener noreferrer"&gt;duplicate content&lt;/a&gt;. It might hurt the ranking of your website but using the canonical URLs you can turn this on its head and get back again &lt;em&gt;all the SEO credits&lt;/em&gt; to your website by setting up canonical URLs in third-party platforms. This blog post is not meant to explain everything about canonical URLs, so if you still have questions I highly recommend reading about it &lt;a href="https://developers.google.com/search/docs/advanced/crawling/consolidate-duplicate-urls" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Even if I duplicated this post to Dev.to, I still get all the “SEO credits” to my website because of the canonical URL. Check out the canonical URL link the source of this page.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  The problem with the links in Remix.run
&lt;/h3&gt;

&lt;p&gt;Most likely, you have a template route like &lt;code&gt;blog.$slug.tsx&lt;/code&gt; in your Remix application. I thought I could add canonical URLs using a &lt;a href="https://remix.run/docs/en/v1/api/conventions#links" rel="noopener noreferrer"&gt;links&lt;/a&gt; function, just like this(assuming I return canonicalUrl in the route loader):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const links: LinksFunction = ({ data }) =&amp;gt; {

  return [{
    rel: 'canonical', href: data.canonicalUrl,
  }];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But TypeScript immediately turned me down and didn't accept having an object with data in parameters. It turns out LinksFunction does not have access to data returned by loader and we can't put there any &lt;em&gt;dynamic&lt;/em&gt; content.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution: use DynamicLinks
&lt;/h3&gt;

&lt;p&gt;So after some research on the internet about adding dynamic link elements to a page in Remix, I came to a solution using DynamicLinks. So, DynamicLinks is not something that comes from the &lt;a href="https://remix.run/docs" rel="noopener noreferrer"&gt;Remix.run documentation&lt;/a&gt; it is rather a utility that can be built using Remix.run capabilities.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Big thanks to &lt;a href="https://twitter.com/sergiodxa" rel="noopener noreferrer"&gt;Sergio&lt;/a&gt; who implemented it in &lt;a href="https://github.com/sergiodxa/remix-utils" rel="noopener noreferrer"&gt;remix-utils&lt;/a&gt; Open-Source library.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  How does DynamicLinks utility works
&lt;/h4&gt;

&lt;p&gt;Let me show you the code and walk you through what is happening:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export function DynamicLinks() {
  let links: LinkDescriptor[] = useMatches().flatMap((match) =&amp;gt; {
    let fn = match.handle?.dynamicLinks;
    if (typeof fn !== 'function') return [];
    return fn({ data: match.data });
  });

  return (
    &amp;lt;React.Fragment&amp;gt;
      {links.map((link) =&amp;gt; (
        &amp;lt;link {...link} key={link.integrity || JSON.stringify(link)} /&amp;gt;
      ))}
    &amp;lt;/React.Fragment&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;First of all, we get the all matched routes using the &lt;a href="https://remix.run/docs/en/v1/api/remix#usematches" rel="noopener noreferrer"&gt;useMatches&lt;/a&gt; utility hook&lt;/li&gt;
&lt;li&gt;We look for our pre-defined dynamicLinks function in exported &lt;a href="https://remix.run/docs/en/v1/api/conventions#handle" rel="noopener noreferrer"&gt;handle&lt;/a&gt; constant of each matched route and call it giving the route data as a parameter(if the function is defined by the route)&lt;/li&gt;
&lt;li&gt;After we collect links from all the matched routes, we simply render them&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can put this component at the root of the Remix app and it will work for all the routes that define dynamicLinks function in exported handle variable.&lt;/p&gt;

&lt;h4&gt;
  
  
  How to use DynamicLinks in Remix app
&lt;/h4&gt;

&lt;p&gt;The usage of the utility is very simple. I define dynamicLinks function in my blog.$slug.tsx module and export it within handle Remix-defined constant:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const dynamicLinks: DynamicLinksFunction&amp;lt;LoaderData&amp;gt; = ({ data }) =&amp;gt; {
  return [
    {
      rel: 'canonical', href: data.canonicalUrl,
    },
  ];
}

export const handle = {
  dynamicLinks,
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note that this code assumes that I return canonicalUrl from my loader function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And then I just need to render DynamicLinks the component in the root of the Remix application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { DynamicLinks } from "remix-utils";

export default function App() {

 return (
    &amp;lt;html lang="en"&amp;gt;
      &amp;lt;head&amp;gt;
        {/** your head tags */}
        &amp;lt;DynamicLinks /&amp;gt;
      &amp;lt;/head&amp;gt;
      &amp;lt;body&amp;gt;
        {/** body tags */}
        &amp;lt;Outlet /&amp;gt;
        &amp;lt;Scripts /&amp;gt;
      &amp;lt;/body&amp;gt;
    &amp;lt;/html&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This results in having a dynamic canonical URL in all of my blog posts(both client and server-side). E.g.:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;link rel="canonical" href="https://aibolik.com/blog/how-to-add-dynamic-canonical-links-in-remix-application"/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s it! I was upset at the beginning that LinksFunction does not support loader data(for sure, for some good reason), but I liked how Remix gives the flexibility to implement this kind of utility. There are more examples in the &lt;a href="https://remix.run/docs/en/v1/api/remix#usematches" rel="noopener noreferrer"&gt;Remix.run documentation&lt;/a&gt; about the useMatches utility hook, like implementing breadcrumbs for all the nested components.&lt;/p&gt;

&lt;p&gt;Thank you for reading this post. If you have any questions please leave comments or tweet tagging me(at &lt;a href="https://twitter.com/aibolik_" rel="noopener noreferrer"&gt;aibolik&lt;/a&gt;_).&lt;/p&gt;

</description>
      <category>react</category>
      <category>canonicallink</category>
      <category>seo</category>
      <category>remix</category>
    </item>
    <item>
      <title>My thoughts on learning Web in 2021</title>
      <dc:creator>Aibol Kussain</dc:creator>
      <pubDate>Mon, 27 Dec 2021 23:04:24 +0000</pubDate>
      <link>https://dev.to/aibolik/my-thoughts-on-learning-web-in-2021-4260</link>
      <guid>https://dev.to/aibolik/my-thoughts-on-learning-web-in-2021-4260</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Note: this post is originally written in &lt;a href="https://aibolik.github.io/blog/my-thoughts-on-learning-web-in-2021"&gt;my personal blog&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hello, people! You probably already saw or read one of those articles like “Web developer roadmap in 2021”, “How to become a web developer”, etc. So, I also wanted to share my perspective on this topic that might be useful to some people who are at the start of their exciting journey or open some discussion with others.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8e8ShWOC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ANfqoLP-mVAHW6QBIBHp7BQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8e8ShWOC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ANfqoLP-mVAHW6QBIBHp7BQ.jpeg" alt="" width="880" height="880"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Learning Web in 2021 is not a straight line (Photo by Mark König on Unsplash)&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  HTML
&lt;/h3&gt;

&lt;p&gt;I want to start with the most basic and the important, as well as, sometimes, overlooked part of the Web — HTML. Why overlooked? Thanks to some great JavaScript frameworks, like React, web developers frequently deal with “components” rather than HTML elements. However, it is important to understand &lt;strong&gt;semantic HTML&lt;/strong&gt; and &lt;strong&gt;accessibility&lt;/strong&gt; and many more topics. I have to admit that I kickstarted my Web Developer work with React and had to catch up with many important concepts of Web later on. Let’s go over some main of the topics here(in my opinion).&lt;/p&gt;

&lt;h4&gt;
  
  
  Semantic HTML and Accessibility
&lt;/h4&gt;

&lt;p&gt;The &lt;a href="https://web.dev/accessible/"&gt;simple idea&lt;/a&gt; is to build a site that works for all of your users. By saying “all of your users” I mean: people with disabilities, people with different internet bandwidth, color blindness issues, device issues, or someone who just doesn’t want to(or can’t) use a mouse/keyboard. Some pretty obvious things to start with are using semantic HTML elements, such as button, input, select, nav, and more. In addition to them, you should be familiar with aria-* and role attributes, which can add additional context for users with screen readers. And even before starting the development, you can talk with your designer to use contrasting colors and also introduce dark/alternative color schema, if it is worth it. (it is a bit outside of the HTML topic, but still worth mentioning).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When I was around 10, my mouse was broken on Pentium II Windows 98 PC. Guess what I had to use to be able to navigate through my computer?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Know your “head” tags
&lt;/h4&gt;

&lt;p&gt;As the subtitle suggests, you should be familiar with what you put on your head tag. This and using the right semantic HTML elements can also affect the SEO of your website. Some meta tags can provide additional information about your website(like title, description, data for a Twitter card) and some tags can optimize your website. With the link tag, you can hint your browser to do some work before accessing some resources(prefetch, preconnect). Take some time to get familiar with them and feel free to take these low-hanging fruits for your website optimization.&lt;/p&gt;

&lt;h4&gt;
  
  
  Know how your elements look and interact
&lt;/h4&gt;

&lt;p&gt;Other than knowing your “head” tags you should be quite familiar with your main “body” tags. Especially, what do they imply and how they interact. For example, consider these:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the default styling of elements(margins on paragraph, font size, and margins on h1-h6 tags, styling of anchors)&lt;/li&gt;
&lt;li&gt;interactive elements(those are accessible with TAB, like an anchor, button, etc.) and how to make one yourself&lt;/li&gt;
&lt;li&gt;other accessibility features, like outlined button&lt;/li&gt;
&lt;li&gt;transition when clicking on the anchor&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are kinda basic, but you might also want to familiarize yourself with more HTML5 elements, and maybe some new elements like dialog those let you create modals out of the box.&lt;/p&gt;

&lt;p&gt;I believe there are many topics that I left not mentioning here about HTML, but I wouldn’t be able to put everything in this article. So, if I missed something important, please feel free to leave a comment or mention it tagging me on Twitter(&lt;a href="https://twitter.com/aibolik_"&gt;@aibolik_&lt;/a&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  CSS
&lt;/h3&gt;

&lt;p&gt;When it comes to CSS, today, we already have different tools to pre- and post-process our “style code” to make our lives easier. But the essential knowledge of CSS remains the key. These are the basic concepts related to laying out elements and styling of them: box-model, positioning, responsive styles, different laying out strategies(flex, grid), transform and transition of properties, and more. But if we go a bit further, it is worth exploring these topics too.&lt;/p&gt;

&lt;h4&gt;
  
  
  Logical properties
&lt;/h4&gt;

&lt;p&gt;These might not be relevant to the project you are working on, but if your website supports multiple locales, which include right-to-left and/or vertical top-down layouts, you should be familiar with these properties. Shortly, these properties &lt;a href="https://web.dev/logical-property-shorthands/"&gt;adapt to the layout&lt;/a&gt; of the page. You don’t explicitly set right/left/top/bottom spacing, you set start/end spacing and the browser will take care of setting the exact side for you.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Alternatively, if you are working on an open-source (popular) components library, chances are that your design would also be used in websites, where they need &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/direction"&gt;“rtl”&lt;/a&gt; support.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  CSS variables
&lt;/h4&gt;

&lt;p&gt;When I first heard about CSS variables, the support for them was not good. However, these days all modern browsers support this feature. At first, some might think it is similar to variables in other CSS post-processors(like LESS or SASS), however, they possess more power than you thought. You can easily rewrite them for some selector, media query, or even with JavaScript(because they are still CSS properties). They are quite useful when building (several) &lt;a href="https://web.dev/building-a-color-scheme/"&gt;color schemas&lt;/a&gt;. But they are also useful for many different things, I saw people using them in very creative ways. It is worth checking out its capabilities.&lt;/p&gt;

&lt;p&gt;Again here, CSS is another big topic in the Web world. There are also topics like typography and fonts, selector naming conventions, many different CSS frameworks, CSS-in-JS, and more. I can not cover all of them in this article, but case by case, some might be more relevant than others, and for some topics, it is enough to just have a basic understanding of the principles.&lt;/p&gt;

&lt;h3&gt;
  
  
  JavaScript
&lt;/h3&gt;

&lt;p&gt;Yet another big topic and I believe the one that is more over-hyped than the other 2 pillars of the Web(thanks again to JS frameworks). Since it is a full-blown programming language it is essential to understand how it works. In addition to how JavaScript works, for Web Developer, it is essential to understand how it works in a browser context. Some points to mention:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;interaction with DOM API&lt;/li&gt;
&lt;li&gt;event handling&lt;/li&gt;
&lt;li&gt;browser APIs(history, fetch, IntersectionObserver, storage APIs, and more)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://javascript.info/"&gt;and many many more topics&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even if popular JavaScript frameworks give a real boost in Web Development, the core JavaScript knowledge is still very important. Even if you start very well with some JavaScript framework, at some point you might end up not understanding how some things function. I was in that shoe, and when I was interviewed for one project where essential JavaScript knowledge was important, I failed that interview. Immediately after that interview, I decided to level up my core JavaScript knowledge and discovered the &lt;a href="https://github.com/getify/You-Dont-Know-JS"&gt;“You don’t know JS”&lt;/a&gt; book series. It was a really good time investment and I highly recommend it for people who feel they have gaps in their JavaScript knowledge.&lt;/p&gt;

&lt;p&gt;This is a small portion of my thoughts regarding the Web topic. I hope to share more of my thoughts and learnings, so if you liked the content, feel free to &lt;a href="https://twitter.com/intent/follow?screen_name=aibolik_"&gt;follow me on Twitter&lt;/a&gt;, where I will share everything I write.&lt;/p&gt;

&lt;p&gt;Stay tuned and thanks for reading!&lt;/p&gt;

</description>
      <category>html</category>
      <category>web</category>
      <category>roadmaps</category>
      <category>learningwebdevelopment</category>
    </item>
    <item>
      <title>Creating Toast API with React Hooks</title>
      <dc:creator>Aibol Kussain</dc:creator>
      <pubDate>Sun, 12 Jan 2020 21:36:18 +0000</pubDate>
      <link>https://dev.to/aibolik/creating-toast-api-with-react-hooks-1bm8</link>
      <guid>https://dev.to/aibolik/creating-toast-api-with-react-hooks-1bm8</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;You can also read this post on my blog by &lt;a href="https://aibolik.github.io/blog/creating-toast-api-with-react-hooks"&gt;this link&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this blog post we will be gradually creating fully working Toast API and we will use advantages of React Hooks to create nicer hooks-supported interface. Full working example is available &lt;a href="https://codesandbox.io/s/awesome-engelbart-cwyqh?fontsize=14&amp;amp;hidenavigation=1&amp;amp;theme=dark"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--24eoFmRh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/hmmj7sjdgwk5cema07uw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--24eoFmRh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/hmmj7sjdgwk5cema07uw.png" alt="Example of shown toast notification" width="880" height="369"&gt;&lt;/a&gt;&lt;em&gt;Example of shown toast notification&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Toast component
&lt;/h2&gt;

&lt;p&gt;Let’s start by creating simple Toast component. It should be simple nice looking box that renders some content. For simplicity of this application, let that content be just a text.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;I will use &lt;a href="https://www.styled-components.com/"&gt;styled-components&lt;/a&gt; in this example for styling.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&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;Wrapper&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="s2"&gt;`
  margin-right: 16px;
  margin-top: 16px;
  width: 200px;

  position: relative;
  padding: 16px;
  border: 1px solid #d7d7d7;
  border-radius: 3px;
  background: white;
  box-shadow: 0px 4px 10px 0px #d7d7d7;
  color: #494e5c;
`&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;Toast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Wrapper&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;Now we have basic Toast, you can test it out by rendering &lt;code&gt;&amp;lt;Toast&amp;gt; Example&amp;lt;/Toast&amp;gt;&lt;/code&gt; in your root component(&lt;code&gt;App.js&lt;/code&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  ToastContainer component
&lt;/h2&gt;

&lt;p&gt;Usually, there can be several toasts at the same time and they are positioned at some corner of the page. Therefore, it makes sense to create &lt;code&gt;ToastContainer&lt;/code&gt; component, that will be responsible for toasts positioning and rendering them in a sequence.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;For simplicity, let’s assume that toast notifications will always be rendered at top right corner. If you want it to be more customisable, &lt;code&gt;ToastContainer&lt;/code&gt; is a right place for this.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Additionally, in order to not mess with z-index, it is better to render components like toasts somewhere up in a DOM tree. In our example we will render them directly inside &lt;code&gt;body&lt;/code&gt; of the page. We can easily accomplish this using &lt;a href="https://reactjs.org/docs/portals.html"&gt;React DOM’s portal API&lt;/a&gt;.&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;Wrapper&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="s2"&gt;`
  position: absolute;
  /* Top right corner */
  right: 0;
  top: 0;
`&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;ToastContainer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;toasts&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;createPortal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;toasts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Toast&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;toast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Toast&lt;/span&gt;&lt;span class="p"&gt;&amp;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="nc"&gt;Wrapper&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;,&lt;/span&gt;
    &lt;span class="nb"&gt;document&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside of wrapper we render array of toasts. We assume that &lt;code&gt;toasts&lt;/code&gt; is an array of objects with &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;content&lt;/code&gt; keys. &lt;code&gt;id&lt;/code&gt; is a unique ID of each toast notification that we will use later to dismiss it, and &lt;code&gt;content&lt;/code&gt; is just a text.&lt;/p&gt;

&lt;h2&gt;
  
  
  ToastProvider
&lt;/h2&gt;

&lt;p&gt;We built &lt;code&gt;Toast&lt;/code&gt; and &lt;code&gt;ToastContainer&lt;/code&gt; components, but we will not expose them directly. Instead, we will expose them through &lt;code&gt;ToastProvider&lt;/code&gt; component, that will be responsible for rendering and managing all toasts. If we were building some library or package, &lt;code&gt;ToastProvider&lt;/code&gt; would be the one exported and exposed to its consumers(of course along with hooks).&lt;/p&gt;

&lt;p&gt;Since it should hold all toasts, let’s use React’s &lt;a href="https://reactjs.org/docs/hooks-reference.html#usestate"&gt;&lt;code&gt;useState&lt;/code&gt; hook&lt;/a&gt; to save and manage toasts array.&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;ToastProvider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;toasts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setToasts&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([]);&lt;/span&gt;

  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ToastProvider&lt;/code&gt; will also use &lt;a href="https://reactjs.org/docs/context.html"&gt;React's context API&lt;/a&gt; to pass helper functions down the tree: &lt;code&gt;addToast&lt;/code&gt; and &lt;code&gt;removeToast&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;addToast&lt;/code&gt; function
&lt;/h3&gt;

&lt;p&gt;This function should add &lt;code&gt;toast&lt;/code&gt; object into &lt;code&gt;toasts&lt;/code&gt; array in &lt;code&gt;ToastProvider&lt;/code&gt;. So it's usage will look like this: &lt;code&gt;addToast('You friend John liked your photo')&lt;/code&gt;. As you can see, it should take a string as an argument, that will end up being &lt;code&gt;content&lt;/code&gt;. Assigning of ID will be responsibility of the function, therefore we need some way of tracking unique IDs. For simplicity, we can have global variable &lt;code&gt;id&lt;/code&gt; that will be incremented on each function call. Let's see how the function would look:&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;let&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;ToastProvider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&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;// ...&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addToast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&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;setToasts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;toasts&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;toasts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;content&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;setToasts&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Note the usage of &lt;a href="https://reactjs.org/docs/hooks-reference.html#functional-updates"&gt;functional update&lt;/a&gt; of &lt;code&gt;setToasts&lt;/code&gt;. We need to use that, since new toasts array is computed using previous state.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I used &lt;code&gt;useCallback&lt;/code&gt;, as a small optimisation. We don't need to recreate this function on every render, therefore we use &lt;code&gt;useCallback&lt;/code&gt; hook. Read more about it in &lt;a href="https://reactjs.org/docs/hooks-reference.html#usecallback"&gt;React's hooks documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;removeToast&lt;/code&gt; function
&lt;/h3&gt;

&lt;p&gt;Contrary to &lt;code&gt;addToast&lt;/code&gt;, this function should remove toast object from &lt;code&gt;toasts&lt;/code&gt; array in &lt;code&gt;ToastProvider&lt;/code&gt; component given the ID of a toast. Guess where this function should be called from... from anywhere where ID is known! Remember we added &lt;code&gt;id&lt;/code&gt; prop to &lt;code&gt;Toast&lt;/code&gt; component? We will use that &lt;code&gt;id&lt;/code&gt; to call &lt;code&gt;removeToast&lt;/code&gt;. Let's see this function's code:&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;ToastProvider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&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;// ...&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addToast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&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;setToasts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;toasts&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;toasts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;content&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;setToasts&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;removeToast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&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;setToasts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;toasts&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;toasts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;id&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;setToasts&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Very simple function — we just filter out the dismissed toast by its ID.&lt;/p&gt;

&lt;p&gt;We are almost done with &lt;code&gt;ToastProvider&lt;/code&gt; component. Let's put everything together and see how it would look:&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;ToastContext&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="nx"&gt;createContext&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&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;ToastProvider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;toasts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setToasts&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&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;addToast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&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;setToasts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;toasts&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;toasts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;content&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;setToasts&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;removeToast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&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;setToasts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;toasts&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;toasts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;id&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;setToasts&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="nc"&gt;ToastContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt; &lt;span class="na"&gt;value&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="nx"&gt;addToast&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;removeToast&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;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ToastContainer&lt;/span&gt; &lt;span class="na"&gt;toasts&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;toasts&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;ToastContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&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;Nothing new in this code: we just added &lt;code&gt;ToastContext&lt;/code&gt;, so that &lt;code&gt;addToast&lt;/code&gt; and &lt;code&gt;removeToast&lt;/code&gt; can be used anywhere down the React tree. Then we render &lt;code&gt;ToastContainer&lt;/code&gt;, that will be rendered always inside body of page, thanks to &lt;a href="https://reactjs.org/docs/portals.html"&gt;Portals&lt;/a&gt;. And &lt;code&gt;children&lt;/code&gt;, since &lt;code&gt;ToastProvider&lt;/code&gt; is rendered at the top level of React tree(along with other providers, e.g. Redux's Provider, &lt;code&gt;ThemeProvider&lt;/code&gt;, etc.).&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;useToast&lt;/code&gt; hook
&lt;/h3&gt;

&lt;p&gt;Finally we reached to creating our own hook, that will be exported along with &lt;code&gt;ToastProvider&lt;/code&gt;. This hook is actually very simple and consists of only 2 lines of code. It's purpose is to make &lt;code&gt;addToast&lt;/code&gt; and &lt;code&gt;removeToast&lt;/code&gt; available with just a function/hook call. Without this hook, you'd use &lt;code&gt;addToast&lt;/code&gt; and &lt;code&gt;removeToast&lt;/code&gt; by importing &lt;code&gt;ToastContext&lt;/code&gt; and usage of &lt;code&gt;React.useContext&lt;/code&gt;:&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;ToastContext&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;./path/to/ToastProvider&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;Example&lt;/span&gt; &lt;span class="o"&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="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;addToast&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="nx"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ToastContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s implement this simple hook:&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;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;useToast&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;toastHelpers&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="nx"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ToastContext&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;toastHelpers&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 don’t need to import &lt;code&gt;ToastContext&lt;/code&gt; because this hook resides along with it in &lt;code&gt;ToastProvider&lt;/code&gt; component. And now we can simply call 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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;addToast&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useToast&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Dismissing toasts with timeout
&lt;/h2&gt;

&lt;p&gt;We can add toasts with &lt;code&gt;addToast&lt;/code&gt; and now they need to be automatically dismissed. I think the right place for this is a &lt;code&gt;Toast&lt;/code&gt; component, since it is aware of its own lifecycle and aware of ID sent to it as props.&lt;/p&gt;

&lt;p&gt;We need to fire a &lt;code&gt;setTimeout&lt;/code&gt; with a call to &lt;code&gt;removeToast&lt;/code&gt; after delay. The best way we can do this is using &lt;a href="https://reactjs.org/docs/hooks-reference.html#useeffect"&gt;&lt;code&gt;useEffect&lt;/code&gt; hook&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Side note about &lt;code&gt;useEffect&lt;/code&gt;: it will run passed callback function whenever one of dependencies changes.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, we will use &lt;code&gt;removeToast&lt;/code&gt; and &lt;code&gt;id&lt;/code&gt; in dependencies list for this &lt;em&gt;effect&lt;/em&gt;, since everything used inside the function should be passed as a dependency. We assume(and know) that &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;removeToast&lt;/code&gt; function won't change, that means the &lt;em&gt;effect&lt;/em&gt; will only be called upon first render. Let's see how it looks in code:&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;Toast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;removeToast&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useToast&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="nx"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setTimeout&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;removeToast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// delay&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="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timer&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;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;removeToast&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="c1"&gt;// ...render toast content as before...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Note the &lt;a href="https://reactjs.org/docs/hooks-reference.html#cleaning-up-an-effect"&gt;clean up function&lt;/a&gt; in &lt;code&gt;useEffect&lt;/code&gt;: we need to clean up timer, so that it won't cause errors in case of unexpected removal of component.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That’s it! Now it works as expected. Feel free to play with the demo in &lt;a href="https://codesandbox.io/s/awesome-engelbart-cwyqh?fontsize=14&amp;amp;hidenavigation=1&amp;amp;theme=dark"&gt;CodeSandbox&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/toast-api-with-react-hooks-cwyqh"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;If you want to go further and practice more you can try enhancing it by adding some more customisation. For example by configuring delay, render position, styling and more. Most likely &lt;code&gt;ToastProvider&lt;/code&gt; is the best place for that, since it is exposed to consumer and renders all other components.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;If you liked the post check out my upcoming course &lt;a href="https://aibolik.github.io/30-day-react"&gt;&lt;em&gt;30-day-React&lt;/em&gt;&lt;/a&gt;. There you can practice more alike examples in a video format. It is still in progress, therefore I will give away the course for free when it is released to first 100 people who &lt;a href="https://aibolik.github.io/30-day-react"&gt;signs up&lt;/a&gt; now.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>react</category>
      <category>reacthook</category>
      <category>javascript</category>
      <category>toast</category>
    </item>
    <item>
      <title>Git Commit Message</title>
      <dc:creator>Aibol Kussain</dc:creator>
      <pubDate>Thu, 20 Dec 2018 07:31:34 +0000</pubDate>
      <link>https://dev.to/aibolik/git-commit-message-41c1</link>
      <guid>https://dev.to/aibolik/git-commit-message-41c1</guid>
      <description>&lt;p&gt;I managed to write this post long time ago, but just now I found some time to write it. I want to cover the thing that every active developer faces everyday probably multiple times from 1 to 10, 20, 30 times a day. So I will talk about &lt;strong&gt;Git commit messages&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;There are probably many articles covering this topic, anyway I don’t want to force my vision how this should be handled, but instead share some practices I acquired from &lt;em&gt;seeing problem in this area&lt;/em&gt;, &lt;em&gt;looking for some solutions&lt;/em&gt; and &lt;em&gt;applying them in practice&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Main source of knowledge was &lt;a href="https://anvilabs.co/blog/writing-practical-commit-messages/" rel="noopener noreferrer"&gt;article of one company&lt;/a&gt; practicing writing commit messages as in AngularJS library. Here is their &lt;a href="https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit" rel="noopener noreferrer"&gt;Git commit message conventions&lt;/a&gt;. I will add some more points to it as well as summarizing their message.&lt;/p&gt;

&lt;h3&gt;
  
  
  Identifying problem
&lt;/h3&gt;

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

&lt;p&gt;If you search for &lt;em&gt;worst commit messages&lt;/em&gt; you can find many examples. I hope you don’t, but just take a look &lt;a href="https://www.reddit.com/r/ProgrammerHumor/comments/375fjr/whats_the_most_humorous_commit_message_youve_ever/" rel="noopener noreferrer"&gt;here&lt;/a&gt;. These changes don’t make sense what has been done.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe4w0lhzfq4lvv2rom26e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe4w0lhzfq4lvv2rom26e.png" width="800" height="506"&gt;&lt;/a&gt;&lt;em&gt;Example of commit messages from Angular project&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;But there are some better messages, those make sense what has been done. However, sometimes there is no &lt;em&gt;context&lt;/em&gt; in them — where the change was applied. Of course you can take a look at what has changed, but isn’t it faster just to read one line, rather than inspecting bunch of changed lines?&lt;/p&gt;

&lt;p&gt;So, there is a definitely a problem. I will share some my suggestions how it should be handled, and then some resources where from I adopted some practices and modified some.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adopted practices
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Use editor, not CLI
&lt;/h4&gt;

&lt;p&gt;This one is probably obvious at the same time a little bit time consuming, but it is really easier to write in some editor, rather than writing everything in one line in CLI after &lt;code&gt;git commit -m "..."&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Set it to your favorite editor, but keep in mind that some &lt;em&gt;visually rich&lt;/em&gt; editors(like VSCode) might have useful plugins those will hint on some best practices.&lt;/p&gt;

&lt;p&gt;To change default editor configuration for your git, write this command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git config --global core.editor "code --wait"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will set VSCode as a default editor for commit messages.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: you should have VSCode configured to run from CLI.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As a bonus, in VSCode you will also see staged files as a comment in &lt;code&gt;COMMIT_MSG&lt;/code&gt;, those will be ignored in original commit. Additionally there is a built-in plugin, that will show when your commit message length is getting too long.&lt;/p&gt;

&lt;h4&gt;
  
  
  Structure your first line
&lt;/h4&gt;

&lt;p&gt;Probably you have already seen some structures for commit messages. I propose you to take a look at &lt;a href="https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit" rel="noopener noreferrer"&gt;AngularJS Git Commit Message Conventions&lt;/a&gt;. Here are key takeaways about subject line:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;your commit message should consist of 3 parts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;type&amp;gt;(&amp;lt;scope&amp;gt;): &amp;lt;subject&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;&amp;lt;type&amp;gt;&lt;/code&gt; - the type of the made changes. Should be one of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;feat&lt;/code&gt; - some feature development&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fix&lt;/code&gt; - bug fix&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docs&lt;/code&gt; - changes in documentation&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;style&lt;/code&gt; - formatting changes&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;refactor&lt;/code&gt; - changes those do not fix a bug or implement a feature. Simple refactoring&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;test&lt;/code&gt; - changes to tests or addition of new ones&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;chore&lt;/code&gt; - any other changes, not affecting code&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;lt;scope&amp;gt;&lt;/code&gt; - here you define the scope of changes. For example: featureA, buildConfig, or any other scope you can think of.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;subject&amp;gt;&lt;/code&gt; - there are different methodologies to write the subject, but the main rules are as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;use imperative, present tense. “fix” not “fixed”, “add” not “added”. By the way, Git also uses this convention for its own pre-formateed messages(“Merge pull request…”).&lt;/li&gt;
&lt;li&gt;don’t capitalize first letter&lt;/li&gt;
&lt;li&gt;do not add dot(.) at the end of subject&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;At first it might be harder to maintain this style, but you will get used to. If you are having troubles remembering all of these you can use library that does it for you. Take a look at &lt;a href="http://commitizen.github.io/cz-cli/" rel="noopener noreferrer"&gt;commitizen&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Sometimes, you might want more type of changes. In this case feel free to add up more, just be sure to coordinate it with the team and document it somewhere.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Add message body and footer when necessary
&lt;/h4&gt;

&lt;p&gt;In some cases you probably want to leave more clues in your commit message. For this purpose use message body. It typically goes after subject line and is divided from it by one empty line. It should also follow the rule of using imperative tense in verbs.&lt;/p&gt;

&lt;p&gt;Footer is mostly ignored, but in some cases it can be useful to add up some meta information. You can use it to mention &lt;em&gt;breaking changes_and list the issues it closes in your issue tracker. For examples: _Closes #132, #123&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Here are examples from AngularJS project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;feat($browser): onUrlChange event (popstate/hashchange/polling)

Added new event to $browser:
- forward popstate event if available
- forward hashchange event if popstate not available
- do polling when neither popstate nor hashchange available

Breaks $browser.onHashChange, which was removed (use onUrlChange instead)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fix($compile): couple of unit tests for IE9

Older IEs serialize html uppercased, but IE9 does not...
Would be better to expect case insensitive, unfortunately jasmine does
not allow to user regexps for throw expectations.

Closes #392
Breaks foo.bar api, foo.baz should be used instea
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;These are mostly recommendations — not a strict policy. You would probably want to adjust these practices to your own needs. Finally, it is your project, your repository, your code — setup your own guidelines and stick to them. If this article was helpful I reached my goal. Thanks for reading!&lt;/p&gt;

&lt;h4&gt;
  
  
  Resources
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://anvilabs.co/blog/writing-practical-commit-messages/" rel="noopener noreferrer"&gt;AnviLabs: Writing practical commit messages&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit" rel="noopener noreferrer"&gt;AngularJS Git Commit Message Conventions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.reddit.com/r/ProgrammerHumor/comments/375fjr/whats_the_most_humorous_commit_message_youve_ever/" rel="noopener noreferrer"&gt;Reddit Topic about worst commit messages&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://commitizen.github.io/cz-cli/" rel="noopener noreferrer"&gt;Commitizen&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html" rel="noopener noreferrer"&gt;A Note About Git Commit Messages&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://365git.tumblr.com/post/3308646748/writing-git-commit-messages" rel="noopener noreferrer"&gt;Writing Git commit messages&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>git</category>
      <category>commit</category>
      <category>versioncontrolsystem</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Whom to follow to stay up to date in JS and FrontEnd world</title>
      <dc:creator>Aibol Kussain</dc:creator>
      <pubDate>Wed, 17 Oct 2018 20:53:22 +0000</pubDate>
      <link>https://dev.to/aibolik/whom-to-follow-to-stay-up-to-date-in-js-and-frontend-world-3k19</link>
      <guid>https://dev.to/aibolik/whom-to-follow-to-stay-up-to-date-in-js-and-frontend-world-3k19</guid>
      <description>&lt;p&gt;Hi, DEV folks. In this post I want to share whom I follow and admire in tech world to stay up to date with all tendencies and other cool stuffs they share. Since I am mostly JavaScript/FrontEnd developer, this list will contain people mostly from this part of IT.&lt;/p&gt;

&lt;p&gt;So here is a list:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Dan Abramov(&lt;a href="https://twitter.com/dan_abramov" rel="noopener noreferrer"&gt;@dan_abramov&lt;/a&gt;) - I don't think that I really need to introduce Dan Abramov, but anyway. He is a main creator of &lt;a href="https://redux.js.org" rel="noopener noreferrer"&gt;Redux&lt;/a&gt; and co-author of &lt;a href="https://github.com/facebook/create-react-app" rel="noopener noreferrer"&gt;create-react-app&lt;/a&gt;, part of team working on ReactJS and just a nice guy ;). He also has stunning courses on &lt;a href="https://egghead.io/instructors/dan-abramov" rel="noopener noreferrer"&gt;egghead.io&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Feross(&lt;a href="https://twitter.com/feross" rel="noopener noreferrer"&gt;@feross&lt;/a&gt;) - great person whom I started following right after watching his talk about linting, where he introduced &lt;a href="https://standardjs.com" rel="noopener noreferrer"&gt;standardJS&lt;/a&gt;, and yeah, he is a creator of that tool. He is also creator of many other cool projects, he is more like tech entrepreneur, but he also has huge experience working on giant companies. I like people who create. By the way, he also has &lt;a href="https://feross.org/" rel="noopener noreferrer"&gt;great blog&lt;/a&gt;(I've read much stuff after his talk).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ali Spittel(&lt;a href="https://twitter.com/ASpittel" rel="noopener noreferrer"&gt;@ASpittel&lt;/a&gt;) - Ali is a developer, teacher and blogger. I just like what she writes and shares. She also an active blogger and has newsletters, where she shares some great articles across the web. She is also creator - take a look at &lt;a href="https://learncodefrom.us/" rel="noopener noreferrer"&gt;https://learncodefrom.us/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Michael Jackson(&lt;a href="https://twitter.com/mjackson" rel="noopener noreferrer"&gt;@mjackson&lt;/a&gt;) - not that one :). Co-Author of popular &lt;code&gt;react-router&lt;/code&gt; and owner of &lt;a href="https://reacttraining.com" rel="noopener noreferrer"&gt;ReactTraining&lt;/a&gt;. Great guy in tech world, especially he focuses(as I understood) on React. Being co-creator of react-router his guides are really well written. He also provides great trainings and workshops at ReactTraining. Also, he really cares about future of web. Just read this thread.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1050594839127973896-780" src="https://platform.twitter.com/embed/Tweet.html?id=1050594839127973896"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1050594839127973896-780');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1050594839127973896&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Kyle Simpson(&lt;a href="https://twitter.com/getify" rel="noopener noreferrer"&gt;@getify&lt;/a&gt;) - when we talk about JavaScript it is unfair to not include Kyle Simpson, also knows as GETIFY. My foundation of JavaScript strengthened after reading his &lt;a href="https://github.com/getify/You-Dont-Know-JS" rel="noopener noreferrer"&gt;You Don't Know JS&lt;/a&gt; book series. I highly recommend to all new(not only beginners) JS devs. You better go to his twitter account, learn about him and read 2 books mentioned in his profile. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Todd Motto(&lt;a href="https://twitter.com/toddmotto" rel="noopener noreferrer"&gt;@toddmotto&lt;/a&gt;) - if I liked Angular and worked with it much, probably Todd Motto would be a number one person whom I should know, and &lt;a href="https://toddmotto.com/" rel="noopener noreferrer"&gt;his blog&lt;/a&gt; would be "go-to" resource for(almost) any Angular related question. He writes great stuff on in, also he is Google Developer Expert and creator &lt;a href="https://ultimateangular.com/" rel="noopener noreferrer"&gt;Ultimate Angular&lt;/a&gt; course series. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Chris Coyier(&lt;a href="https://twitter.com/chriscoyier" rel="noopener noreferrer"&gt;@chriscoyier&lt;/a&gt;) - co-founder of CodePen.io and CSS-tricks. He knows more CSS tricks than anyone else. He is also very active on his blog/website. Definitely should follow.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Wes Bos(&lt;a href="https://twitter.com/wesbos" rel="noopener noreferrer"&gt;@wesbos&lt;/a&gt;) - Wes Bos, I call this man a "serial instructor", he has dozens of JavaScript related courses and ton of other useful materials. Just head to his website at &lt;a href="https://wesbos.com" rel="noopener noreferrer"&gt;wesbos.com&lt;/a&gt;. By the way, recently he launched &lt;a href="https://advancedreact.com" rel="noopener noreferrer"&gt;AdvancedReact and GraphQL course&lt;/a&gt; - that I mentioned in my newsletters. This course already has many positive feedbacks. Maybe you should also take a look at it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;RisingStack(&lt;a href="https://twitter.com/RisingStack" rel="noopener noreferrer"&gt;@RisingStack&lt;/a&gt;) - I know I was going to talk about people. But this company is really great. I did not investigate much what they are doing, but they are super useful for dev community, since they share really useful articles from real experience. You definitely have to take a look at &lt;a href="https://blog.risingstack.com" rel="noopener noreferrer"&gt;their tech blog&lt;/a&gt; and follow them to stay up to date. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This list is just a small portion of whole thing. Actually there are more people whom I follow and admire what they do. Feel free to share your "hereos" in comments.&lt;/p&gt;

&lt;p&gt;Thanks for having me, Bye!&lt;/p&gt;

&lt;p&gt;P.S.: if you want to stay up date with what I share, &lt;a href="https://twitter.com/aibolik_" rel="noopener noreferrer"&gt;follow me on Twitter(@aibolik)&lt;/a&gt; and subscribe to &lt;a href="https://aibolik.github.io" rel="noopener noreferrer"&gt;my newsletters&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Launching my own newsletter</title>
      <dc:creator>Aibol Kussain</dc:creator>
      <pubDate>Wed, 03 Oct 2018 20:27:55 +0000</pubDate>
      <link>https://dev.to/aibolik/launching-my-own-newsletter-ked</link>
      <guid>https://dev.to/aibolik/launching-my-own-newsletter-ked</guid>
      <description>&lt;p&gt;Hi, there, Dev.to community!&lt;/p&gt;

&lt;p&gt;It's been a while since I did not post anything here. I want to announce that I am starting my own newsletters "JSinDEV", that will serve selected content around the web about JavaScript, FrontEnd and related stuff that will be useful to read.&lt;/p&gt;

&lt;p&gt;If you haven't subscribed yet, feel free to subscribe at my &lt;a href="https://aibolik.github.io" rel="noopener noreferrer"&gt;website&lt;/a&gt; - there is a form at the bottom. &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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwxxpw3o91gaz90w25q8w.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwxxpw3o91gaz90w25q8w.png" alt="JSinDEV weekly newsletters"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;P.S. Hey, I recently launched my website/blog, so if you want to follow along my path and see what I do, follow &lt;a href="https://twitter.com/aibolik_" rel="noopener noreferrer"&gt;my twitter&lt;/a&gt; to be on track of what I am doing.&lt;/p&gt;

&lt;p&gt;Thanks,&lt;/p&gt;

&lt;p&gt;Have a nice day&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>frontend</category>
      <category>node</category>
      <category>newsletters</category>
    </item>
    <item>
      <title>Create your own virtual DOM to understand it (Part 2)</title>
      <dc:creator>Aibol Kussain</dc:creator>
      <pubDate>Sun, 29 Jul 2018 15:53:32 +0000</pubDate>
      <link>https://dev.to/aibolik/create-your-own-virtual-dom-to-understand-it-part-2-nlc</link>
      <guid>https://dev.to/aibolik/create-your-own-virtual-dom-to-understand-it-part-2-nlc</guid>
      <description>&lt;p&gt;&lt;em&gt;This article is the continuation of&lt;/em&gt; &lt;a href="https://dev.to/aibolik/create-your-own-virtual-dom-to-understand-it-part-1-3g4e"&gt;&lt;em&gt;first part&lt;/em&gt;&lt;/a&gt;&lt;em&gt;(so you better read first part too :), where we learnt how to create virtual nodes with JSX-like function&lt;/em&gt; &lt;strong&gt;&lt;em&gt;hyperscript&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;and render them. In this part we will implement “&lt;/em&gt; &lt;strong&gt;&lt;em&gt;diffing” algorithm&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;of virtual DOM, that is the main advantage of it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally, I was inspired by the talk of&lt;/em&gt; &lt;a href="https://medium.com/u/c3a0319fab53" rel="noopener noreferrer"&gt;&lt;em&gt;stefan judis&lt;/em&gt;&lt;/a&gt; _at Web Rebels 2018, so feel free to take a look at it &lt;a href="https://youtu.be/kghSjWmNJyQ" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Okay, we have a simple function component that renders dynamic component. Let’s make it a bit complex by introducing its own state and converting it into class with render method. Also we can separate out new &lt;strong&gt;&lt;em&gt;People component,&lt;/em&gt;&lt;/strong&gt; that will responsible for rendering only list of emojis. Here how it will look like after:&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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AgP5nb4zbPqgKgwtTNWdneA.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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AgP5nb4zbPqgKgwtTNWdneA.png"&gt;&lt;/a&gt;&lt;strong&gt;People component&lt;/strong&gt; that renders list of emojis given in its properties&lt;/p&gt;

&lt;p&gt;Nothing special with this component: it just renders list of items that is given in its props. &lt;strong&gt;Note&lt;/strong&gt; : we introduced render method here — it actually creates virtual DOM that we use to render real DOM. And here is how our &lt;strong&gt;&lt;em&gt;App component&lt;/em&gt;&lt;/strong&gt; will change:&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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2A23EwA0oCCuzbjJvBpM2RTQ.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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2A23EwA0oCCuzbjJvBpM2RTQ.png"&gt;&lt;/a&gt;&lt;strong&gt;App component &lt;/strong&gt;turned into class component&lt;/p&gt;

&lt;p&gt;Two things to mention here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We set its state in its constructor and create &lt;em&gt;timer,&lt;/em&gt; that updates its state every 1 second(1000ms)&lt;/li&gt;
&lt;li&gt;We call setState method to update its state.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can find what does setState method in &lt;a href="https://reactjs.org/docs/faq-state.html#what-does-setstate-do" rel="noopener noreferrer"&gt;React’s documentation&lt;/a&gt;. We will implement almost the same functionality here — update its &lt;strong&gt;state&lt;/strong&gt; object and &lt;strong&gt;make our component to re-render.&lt;/strong&gt; Of course React’s implementation is not that trivial, it makes bunch of optimizations on top of it, but come on, we are just building it :)&lt;/p&gt;

&lt;p&gt;Since the setState is the method that every function will call, it makes sense to create an &lt;em&gt;abstract class&lt;/em&gt; that implements it. When you write class MyApp extends React.Component in React you are &lt;em&gt;inheriting&lt;/em&gt; the &lt;a href="https://reactjs.org/docs/glossary.html#components" rel="noopener noreferrer"&gt;&lt;strong&gt;Component&lt;/strong&gt;&lt;/a&gt; class and can use its methods. Okay, let’s scratch our simple Base Component class:&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%2Fcdn-images-1.medium.com%2Fmax%2F858%2F1%2AD5-dNjonvaqEbQC98iUt9A.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%2Fcdn-images-1.medium.com%2Fmax%2F858%2F1%2AD5-dNjonvaqEbQC98iUt9A.png"&gt;&lt;/a&gt;&lt;strong&gt;Base Component &lt;/strong&gt;implementation&lt;/p&gt;

&lt;p&gt;Basically, we just initiate our component with props and state fields and implement setState method that basically rewrites our state and calls &lt;strong&gt;renderComponent&lt;/strong&gt; with itself in its parameters. I’ll explain it now, but first of all let’s make changes to our components:&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%2Fcdn-images-1.medium.com%2Fmax%2F716%2F1%2AeKQkT7QW7w98peWRoGDFLw.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%2Fcdn-images-1.medium.com%2Fmax%2F716%2F1%2AeKQkT7QW7w98peWRoGDFLw.png"&gt;&lt;/a&gt;Changes to our components&lt;/p&gt;

&lt;p&gt;Okay, now we have our &lt;strong&gt;App&lt;/strong&gt; and &lt;strong&gt;People&lt;/strong&gt; components updated I’ll try to explain what &lt;strong&gt;renderComponent&lt;/strong&gt; should do.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It grabs the old base(current DOM before change that is saved in component.base)&lt;/li&gt;
&lt;li&gt;Renders virtual DOM — that we can get from &lt;em&gt;component.render&lt;/em&gt; method and saves it into &lt;em&gt;component.base.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Replaces the old child with the new one&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here it is:&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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AECgKa89HTXvXDMrIghlilg.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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AECgKa89HTXvXDMrIghlilg.png"&gt;&lt;/a&gt;&lt;strong&gt;renderComponent &lt;/strong&gt;function implementation&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I’ve added parent parameter, to that we could use parent.appendChild when we render the root component first time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One little change to make it perfect is to change a little bit renderNode function. Remember how it looked before:&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2ARxplN3cdbxAPc8Qr5P-vUQ.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2ARxplN3cdbxAPc8Qr5P-vUQ.png"&gt;&lt;/a&gt;&lt;strong&gt;renderNode &lt;/strong&gt;before&lt;/p&gt;

&lt;p&gt;It only handles DOM elements, but actually we have something like this in our &lt;strong&gt;&lt;em&gt;App component’s&lt;/em&gt;&lt;/strong&gt; render_ &lt;em&gt;method **&lt;/em&gt;:_**&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return h('div', { class: 'app' },
 h('h1', null, 'Simple vDOM'),
 h(People)
 )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see &lt;strong&gt;nodeName&lt;/strong&gt; can take not only string values, but also another component/function to render, in our case this is &lt;strong&gt;&lt;em&gt;People component&lt;/em&gt;&lt;/strong&gt;. We need to make sure we handle them properly — basically we need to initiate the component, render it and do this with all of its children. Here is our updated renderNode implementation:&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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AudGX7_pc2dUa3vVwRvyVlw.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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AudGX7_pc2dUa3vVwRvyVlw.png"&gt;&lt;/a&gt;updated &lt;strong&gt;renderNode &lt;/strong&gt;function&lt;/p&gt;

&lt;p&gt;That’s it about rendering! Now our app works perfectly: any state change will cause app to re-render — as a result every child element/component will be updated. To leverage from this approach let’s move update operation in our application from &lt;strong&gt;&lt;em&gt;App component&lt;/em&gt;&lt;/strong&gt; to &lt;strong&gt;&lt;em&gt;People component&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;(remove constructor from App and write it inside People)&lt;/em&gt; &lt;strong&gt;&lt;em&gt;:&lt;/em&gt;&lt;/strong&gt;&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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AsuVzr44RaWNPduSMVh1t9Q.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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AsuVzr44RaWNPduSMVh1t9Q.png"&gt;&lt;/a&gt;&lt;strong&gt;constructor&lt;/strong&gt; of People component&lt;/p&gt;

&lt;p&gt;And update render method of People from this: return h('ul', null, ...props.list.map(item =&amp;gt; h('li', null, item))) to this return h('ul', null, ...state.list.map(item =&amp;gt; h('li', null, item)))&lt;/p&gt;

&lt;p&gt;Now only &lt;strong&gt;&lt;em&gt;People component&lt;/em&gt;&lt;/strong&gt; will be re-rendered when its state updates. Compare these GIFs:&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%2Fcdn-images-1.medium.com%2Fmax%2F480%2F1%2AOD2VWhF04kXUvHBEdRvVCA.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%2Fcdn-images-1.medium.com%2Fmax%2F480%2F1%2AOD2VWhF04kXUvHBEdRvVCA.gif"&gt;&lt;/a&gt;Application re-render &lt;strong&gt;before&lt;/strong&gt;&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%2Fcdn-images-1.medium.com%2Fmax%2F480%2F1%2A1x8MWMqh2HCv4yg8oEjYLg.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%2Fcdn-images-1.medium.com%2Fmax%2F480%2F1%2A1x8MWMqh2HCv4yg8oEjYLg.gif"&gt;&lt;/a&gt;Application re-render &lt;strong&gt;after&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As you can see only the list updates, while the headline(Simple vDOM) does not re-render. Now we made our components &lt;strong&gt;Autonomous.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note 1&lt;/strong&gt; : I’ve changes a bit styles: displayed items in &lt;strong&gt;flex&lt;/strong&gt; row, so that adding new items will not cause reflow.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note 2:&lt;/strong&gt; To see which parts of the website re-render you can enable &lt;strong&gt;Paint flashing&lt;/strong&gt; in Chrome Dev Tools -&amp;gt; More(… icon)-&amp;gt; More tools -&amp;gt; Rendering)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Diffing algorithm
&lt;/h3&gt;

&lt;p&gt;Well, it looks more like &lt;em&gt;React-ish&lt;/em&gt; but re-rendering whole app when we just add a new item to the list is awful. Now it’s time to introduce “ &lt;strong&gt;Diffing” algorithm&lt;/strong&gt; that will make our application more powerful.&lt;/p&gt;

&lt;p&gt;In our case, we can just naively check if there are any new items in new virtual Node, and if there are any, we will just append it. So let’s start implementing it step by step. First of all, if there is no DOM provided, we just want render our virtual node and append it to the parent:&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%2Fcdn-images-1.medium.com%2Fmax%2F812%2F1%2A8lZ2fUQEgnjgfFZvNL4YFg.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%2Fcdn-images-1.medium.com%2Fmax%2F812%2F1%2A8lZ2fUQEgnjgfFZvNL4YFg.png"&gt;&lt;/a&gt;Render our app and append it to the parent, if there is no DOM to compare&lt;/p&gt;

&lt;p&gt;This is applicable for the &lt;em&gt;first run — _where there is no DOM rendered yet, and the only place where we give _parent&lt;/em&gt; in its parameters. So we can use it for initial rendering, here how we will do it:&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%2Fcdn-images-1.medium.com%2Fmax%2F952%2F1%2A4gZn82zKuRjhrG1k2V3XFQ.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%2Fcdn-images-1.medium.com%2Fmax%2F952%2F1%2A4gZn82zKuRjhrG1k2V3XFQ.png"&gt;&lt;/a&gt;Rendering app first time&lt;/p&gt;

&lt;p&gt;Okay, now we rendered our app, the &lt;strong&gt;&lt;em&gt;People component&lt;/em&gt;&lt;/strong&gt; will update its state after 1 second(remember the &lt;em&gt;timer&lt;/em&gt; we set?). That will cause our renderComponent to be called, so it will also be changed:&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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AWDNSeZ6pk67WgR256eTB6A.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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AWDNSeZ6pk67WgR256eTB6A.png"&gt;&lt;/a&gt;updated &lt;strong&gt;renderComponent&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Firstly, we create the virtual DOM with component.render method, then we just compare it with its current DOM state(that we saved in component.base field), and save the result of &lt;strong&gt;diff&lt;/strong&gt; in it again. So our &lt;strong&gt;diff&lt;/strong&gt; function should check if there are any new child, and in case it is true, we just &lt;em&gt;append&lt;/em&gt; it. Here how it could look like:&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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AwEA0oTjVUZPluaaaqYzSHg.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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AwEA0oTjVUZPluaaaqYzSHg.png"&gt;&lt;/a&gt;Simple diffing that checks for new children&lt;/p&gt;

&lt;p&gt;That’s it, we made it! Now take a look how it looks in our 3rd iteration:&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%2Fcdn-images-1.medium.com%2Fmax%2F480%2F1%2AIdRJUrYBPB7rl61Ga8ZK0A.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%2Fcdn-images-1.medium.com%2Fmax%2F480%2F1%2AIdRJUrYBPB7rl61Ga8ZK0A.gif"&gt;&lt;/a&gt;Re-rendering with diffing algorithm&lt;/p&gt;

&lt;p&gt;As you can see the whole application and list do not re-render. Only the new child is added to list.&lt;/p&gt;

&lt;p&gt;This is just a simple example with simple “&lt;em&gt;diffing” algorithm,&lt;/em&gt; but &lt;em&gt;hopefully&lt;/em&gt; it will &lt;strong&gt;make sense for those who are new to these concept&lt;/strong&gt; &lt;em&gt;.&lt;/em&gt; It can not be compared to what React does. Here is just a few things what is there in &lt;a href="https://reactjs.org/docs/reconciliation.html" rel="noopener noreferrer"&gt;React’s diffing&lt;/a&gt;algorithm:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Comparing the types&lt;/li&gt;
&lt;li&gt;Comparing the attributes(props)&lt;/li&gt;
&lt;li&gt;Comparing the children&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And of course there are tons of optimization under the hood, that makes React so powerful and beautiful UI library.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you liked this and &lt;a href="https://dev.to/aibolik/create-your-own-virtual-dom-to-understand-it-part-1-3g4e"&gt;previous&lt;/a&gt; posts feel free to like(❤) and "unicorn" it, that will motivate me to do stuff like this more. Thanks for reading!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;UPDATE 24.11.2019: If you want to learn React by doing many exercises, feel free to sign up to my upcoming course &lt;a href="https://aibolik.github.io/30-day-react/" rel="noopener noreferrer"&gt;30-day-React&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://youtu.be/kghSjWmNJyQ" rel="noopener noreferrer"&gt;Stefan Judis: What the v…DOM? | Web Rebels 2018&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://codepen.io/stefanjudis/pen/vjYWNO" rel="noopener noreferrer"&gt;Implementation of this experiment by Stefan Judis in CodePen&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/aibolik/simple-vdom" rel="noopener noreferrer"&gt;GitHub repo of this experiment&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/docs/reconciliation.html" rel="noopener noreferrer"&gt;React’s diffing algorithm&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>virtualdom</category>
      <category>reconciliation</category>
      <category>javascript</category>
      <category>learnreactjs</category>
    </item>
    <item>
      <title>Create your own virtual DOM to understand it (Part 1)</title>
      <dc:creator>Aibol Kussain</dc:creator>
      <pubDate>Thu, 26 Jul 2018 17:40:51 +0000</pubDate>
      <link>https://dev.to/aibolik/create-your-own-virtual-dom-to-understand-it-part-1-3g4e</link>
      <guid>https://dev.to/aibolik/create-your-own-virtual-dom-to-understand-it-part-1-3g4e</guid>
      <description>&lt;p&gt;In this article I want to recap my experience creating own “virtual DOM”. Sounds too ambitiously? Probably, but it is not that complicated as you might think. As the title states, it will make sense when you create your own, rather than reading thousands of articles about it.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally, I was inspired by the talk of &lt;a href="https://medium.com/u/c3a0319fab53" rel="noopener noreferrer"&gt;Stefan Judis&lt;/a&gt; at Web Rebels 2018, so feel free to take a look at it &lt;a href="https://youtu.be/kghSjWmNJyQ" rel="noopener noreferrer"&gt;&lt;em&gt;here&lt;/em&gt;&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This concept of &lt;strong&gt;Virtual DOM&lt;/strong&gt; got popular back in 2013 when &lt;a href="https://reactjs.org/blog/2013/06/05/why-react.html" rel="noopener noreferrer"&gt;React was released&lt;/a&gt;. Because of this concept ReactJS is one of super fast libraries for building UI. I’ll try to explain it in few sentences, then we will get to writing own one.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Virtual DOM is the representation of DOM as an object. When changes to state of application are made, new Virtual DOM is compared(applying &lt;a href="https://reactjs.org/docs/reconciliation.html" rel="noopener noreferrer"&gt;&lt;strong&gt;diffing algorithms&lt;/strong&gt;&lt;/a&gt;) with DOM and only changes are reflected, not causing full re-rendering of DOM.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ok, here is a plan how we will create our virtual DOM.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create &lt;em&gt;hyperscript&lt;/em&gt; function to render DOM — it is kinda &lt;a href="https://reactjs.org/docs/jsx-in-depth.html" rel="noopener noreferrer"&gt;JSX&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Create simple app with hyperscript&lt;/li&gt;
&lt;li&gt;Make our App dynamic and learn to render virtual DOM&lt;/li&gt;
&lt;li&gt;Implement diffing algorithm to see the power of virtual DOM&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  HyperScript implementation
&lt;/h3&gt;

&lt;p&gt;If you ever worked with ReactJS you probably know what is JSX. It can be another topic for discussion, but shortly it transforms “HTML-like” syntax into JavaScript function calls, in React it is transferred as React.createElement. So, in this step our aim is to &lt;em&gt;recreate&lt;/em&gt; this awesome function.&lt;/p&gt;

&lt;p&gt;Generally, it is the building block that creates virtual DOM. However, in this step I want to emphasize how we can build simple DOM with it and in further steps we will develop it to make virtual DOM.&lt;/p&gt;

&lt;p&gt;The inputs for this function are: &lt;em&gt;type of node, properties(a.k.a. attributes), children.&lt;/em&gt; So here is a simple implementation of this function:&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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2Av7t7-08nG5TGJFSD0aaqVw.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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2Av7t7-08nG5TGJFSD0aaqVw.png"&gt;&lt;/a&gt;Implementation of &lt;strong&gt;hyperscript &lt;/strong&gt;function, that constructs DOM given nodeName, attrs and children&lt;/p&gt;

&lt;p&gt;Firstly, It simply creates DOM element with &lt;em&gt;nodeName.&lt;/em&gt; Secondly, sets its attributes, and last step to append child nodes with the check for text nodes.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; actually, we could go further and render children recursively, but I left this for later for simplicity reason.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here is how it can be used(from now on we will use &lt;strong&gt;&lt;em&gt;h&lt;/em&gt;&lt;/strong&gt; instead of &lt;strong&gt;&lt;em&gt;hyperscript&lt;/em&gt;&lt;/strong&gt; ):&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%2Fcdn-images-1.medium.com%2Fmax%2F500%2F1%2AdhtTJ-mXbjzWj6dsxvgfyQ.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%2Fcdn-images-1.medium.com%2Fmax%2F500%2F1%2AdhtTJ-mXbjzWj6dsxvgfyQ.png"&gt;&lt;/a&gt;Simple function that renders h1 element&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%2Fcdn-images-1.medium.com%2Fmax%2F544%2F1%2Ayh4ESw-DoyojIG6KpY2BJw.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%2Fcdn-images-1.medium.com%2Fmax%2F544%2F1%2Ayh4ESw-DoyojIG6KpY2BJw.png"&gt;&lt;/a&gt;Result of calling App() function&lt;/p&gt;

&lt;h4&gt;
  
  
  Creating application with Hyperscript
&lt;/h4&gt;

&lt;p&gt;Okay, we now can create simple application with the help of Hyperscript. Let’s create a bit more complex application than it was in previous step. Here is our newer &lt;strong&gt;&lt;em&gt;App&lt;/em&gt;&lt;/strong&gt; function.&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%2Fcdn-images-1.medium.com%2Fmax%2F984%2F1%2AAf-dWCO9YL2_2I37m-AEgA.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%2Fcdn-images-1.medium.com%2Fmax%2F984%2F1%2AAf-dWCO9YL2_2I37m-AEgA.png"&gt;&lt;/a&gt;“Complex Application” with hyperscript&lt;/p&gt;

&lt;p&gt;When the &lt;strong&gt;App&lt;/strong&gt; is executed it creates a &lt;em&gt;div,&lt;/em&gt; with two children: one rendering H1 heading, and the second one unordered list. Note that we pass &lt;strong&gt;&lt;em&gt;props&lt;/em&gt;&lt;/strong&gt; to our function and render &lt;strong&gt;props.list&lt;/strong&gt; into unordered list. Let’s add some more rendering magic:&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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AoarZn_WhFoBFQh1LghDUcA.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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AoarZn_WhFoBFQh1LghDUcA.png"&gt;&lt;/a&gt;Render the app with the state&lt;/p&gt;

&lt;p&gt;Generally, we just want to render the output of &lt;strong&gt;App&lt;/strong&gt; function(that is valid DOM) into the body of document giving the state that contains emojis list.&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%2Fcdn-images-1.medium.com%2Fmax%2F577%2F1%2Ag0mDXTJMoiVsJ4dPtjXK0A.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%2Fcdn-images-1.medium.com%2Fmax%2F577%2F1%2Ag0mDXTJMoiVsJ4dPtjXK0A.png"&gt;&lt;/a&gt;Result of our work&lt;/p&gt;

&lt;p&gt;&lt;em&gt;It wasn’t that hard. Is it?&lt;/em&gt; Let’s add some dynamic content, and add random emoji every 1 second this way we can see how our app renders.&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%2Fcdn-images-1.medium.com%2Fmax%2F856%2F1%2AgpnljAXNujX8Lkl1JRTEUw.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%2Fcdn-images-1.medium.com%2Fmax%2F856%2F1%2AgpnljAXNujX8Lkl1JRTEUw.png"&gt;&lt;/a&gt;&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%2Fcdn-images-1.medium.com%2Fmax%2F680%2F1%2AltA2Y6YhMq8y2s5Kz39C0g.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%2Fcdn-images-1.medium.com%2Fmax%2F680%2F1%2AltA2Y6YhMq8y2s5Kz39C0g.gif"&gt;&lt;/a&gt;DOM is fully re-rendering every 1 second&lt;/p&gt;

&lt;h4&gt;
  
  
  Implement vDOM rendering
&lt;/h4&gt;

&lt;p&gt;Okay, now we have dynamic app done with &lt;em&gt;hyperscript&lt;/em&gt; let’s move on to actual virtual DOM and its implementation. First of all we need to change our &lt;em&gt;hyperscript&lt;/em&gt; function. Now it should not create real DOM, but instead it rather should create virtual DOM. So, given &lt;em&gt;nodeName,&lt;/em&gt; &lt;em&gt;attributes&lt;/em&gt; and &lt;em&gt;children&lt;/em&gt; we just a create an object with corresponding keys. Thanks to ES6 we can do this in one line:&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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AkZpNI6nl6KVQjfFj2ewY6Q.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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AkZpNI6nl6KVQjfFj2ewY6Q.png"&gt;&lt;/a&gt;hyperscript&lt;em&gt; function that returns virtual DOM&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We have a virtual DOM and if we execute the &lt;strong&gt;App&lt;/strong&gt; function with same emojis list we get something like this(logged in console):&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%2Fcdn-images-1.medium.com%2Fmax%2F663%2F1%2AHYsUARTHUNuhAe5uvPeEig.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%2Fcdn-images-1.medium.com%2Fmax%2F663%2F1%2AHYsUARTHUNuhAe5uvPeEig.png"&gt;&lt;/a&gt;Virtual DOM of our application&lt;/p&gt;

&lt;p&gt;Pretty similar to DOM. Now let’s create a function that renders virtual DOM into real DOM. As you might have guessed it should take &lt;em&gt;virtual Node&lt;/em&gt; as a parameter. Here it is:&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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AQkHR2QSgoAvuwkv406bf3Q.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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AQkHR2QSgoAvuwkv406bf3Q.png"&gt;&lt;/a&gt;This function renders real DOM(DOM Node) given virtual DOM(Node)&lt;/p&gt;

&lt;p&gt;Let me explain what it does step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using destructuring we retrieve nodeName, attributes and children of virtual Node&lt;/li&gt;
&lt;li&gt;If vnode is text(we can check it by &lt;em&gt;vnode.split&lt;/em&gt;) then we return text Node&lt;/li&gt;
&lt;li&gt;Otherwise we create an element with nodeName and set its attributes from &lt;em&gt;attributes&lt;/em&gt; object&lt;/li&gt;
&lt;li&gt;Do the same thing for children if any&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, remember our &lt;em&gt;render&lt;/em&gt; function that rendered our &lt;strong&gt;App&lt;/strong&gt;? We just need to change a little bit to make it 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%2Fcdn-images-1.medium.com%2Fmax%2F812%2F1%2APlV4Puh-8DuuL5ZI1lD6hg.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%2Fcdn-images-1.medium.com%2Fmax%2F812%2F1%2APlV4Puh-8DuuL5ZI1lD6hg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This article became a bit longer than I thought, so I decided to break into two parts.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, let’s recap this. We created a &lt;strong&gt;hyperscript — &lt;/strong&gt; &lt;em&gt;virtual DOM factory&lt;/em&gt; &lt;strong&gt;, renderNode — &lt;/strong&gt; &lt;em&gt;that turns virtual DOM into DOM element&lt;/em&gt; and a &lt;em&gt;function component&lt;/em&gt; &lt;strong&gt;&lt;em&gt;App&lt;/em&gt;&lt;/strong&gt; that creates whole page. The result is now the same as we did it before without virtual DOM, but now we have more control. In the next article we will explore what makes React(and virtual DOM) so fast.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;You can look up all steps in my&lt;/em&gt; &lt;a href="http://bit.ly/2NKXH0V" rel="noopener noreferrer"&gt;&lt;em&gt;GitHub repository&lt;/em&gt;&lt;/a&gt;&lt;em&gt;. You can find these steps in branches.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the next article we will implement simple &lt;strong&gt;diffing algorithm&lt;/strong&gt; , that will make our app faster and you’ll be able to see it action.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;UPDATE: Hey there, if you liked it go and take a look at the &lt;a href="https://dev.to/aibolik/create-your-own-virtual-dom-to-understand-it-part-2-nlc"&gt;2nd part of this article&lt;/a&gt;!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;UPDATE 24.11.2019: If you want to learn React by doing many exercises, feel free to sign up to my upcoming course &lt;a href="https://aibolik.github.io/30-day-react/" rel="noopener noreferrer"&gt;30-day-react&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>hyperscript</category>
      <category>javascript</category>
      <category>virtualdom</category>
      <category>react</category>
    </item>
  </channel>
</rss>
