<?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: Rishi Kumar Chawda</title>
    <description>The latest articles on DEV Community by Rishi Kumar Chawda (@rishikc).</description>
    <link>https://dev.to/rishikc</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%2F174374%2F1dd1371e-6d93-482a-8031-68f5e9f2fd8c.jpg</url>
      <title>DEV Community: Rishi Kumar Chawda</title>
      <link>https://dev.to/rishikc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rishikc"/>
    <language>en</language>
    <item>
      <title>React Native just got better! The New Javascript Engine is here!</title>
      <dc:creator>Rishi Kumar Chawda</dc:creator>
      <pubDate>Thu, 11 Jul 2019 18:14:10 +0000</pubDate>
      <link>https://dev.to/rishikc/react-native-just-got-better-the-new-javascript-engine-is-here-7nl</link>
      <guid>https://dev.to/rishikc/react-native-just-got-better-the-new-javascript-engine-is-here-7nl</guid>
      <description>&lt;p&gt;&lt;a href="http://hermesengine.dev" rel="noopener noreferrer"&gt;Hermes Engine&lt;/a&gt; for React Native is here!&lt;/p&gt;

&lt;p&gt;So it started from here : &lt;/p&gt;

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

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



&lt;/p&gt;

&lt;p&gt;Facebook officially released React Native v0.60.2 about &lt;a href="(https://github.com/facebook/react-native/releases/tag/0.60.2)"&gt;two hours ago&lt;/a&gt; which enables the engine as an opt-in feature. This brings a really big advancement on mobile development with React Native as it will give the Android Applications built with react native a significant boost in performance, smaller build size and less memory usage! 💜❤️&lt;/p&gt;

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

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



&lt;/p&gt;

&lt;p&gt;And the integration is so easy! Just enable it in your &lt;code&gt;build.gradle&lt;/code&gt; file and that's it. Done 🙆‍♂️ Literally, JUST ONE LINE OF CODE AND THATS IT! ✨&lt;/p&gt;

&lt;p&gt;You can see the difference on app startup here!👇 :&lt;/p&gt;

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

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



&lt;/p&gt;

&lt;p&gt;The only thing is that you need to upgrade React Native to version 0.60.2 which has a lot of significant changes (RN v0.60 was released a week ago - &lt;a href="https://github.com/react-native-community/releases/blob/master/CHANGELOG.md" rel="noopener noreferrer"&gt;see changelog here&lt;/a&gt;) but well, the improvement for Android apps with this engine is AWESOME! Definitely trying it over the weekend! 💪 &lt;/p&gt;

&lt;p&gt;Oh, and don't forget to follow &lt;a href="https://twitter.com/HermesEngine/status/1149370023539961856" rel="noopener noreferrer"&gt;this discussion&lt;/a&gt; on &lt;a href="https://twitter.com/HermesEngine" rel="noopener noreferrer"&gt;Hermes' official twitter account&lt;/a&gt;!&lt;/p&gt;

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

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



&lt;/p&gt;

&lt;p&gt;Who all are excited for this? I definitely am!&lt;/p&gt;

&lt;p&gt;You can also follow me on &lt;a href="https://twitter.com/rishiikc" rel="noopener noreferrer"&gt;twitter&lt;/a&gt; for my shitposts and retweets! 🤓&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>javascript</category>
      <category>mobile</category>
      <category>react</category>
    </item>
    <item>
      <title>What's your plan for the weekend?</title>
      <dc:creator>Rishi Kumar Chawda</dc:creator>
      <pubDate>Thu, 11 Jul 2019 12:22:42 +0000</pubDate>
      <link>https://dev.to/rishikc/what-s-your-plan-for-the-weekend-4lfp</link>
      <guid>https://dev.to/rishikc/what-s-your-plan-for-the-weekend-4lfp</guid>
      <description>&lt;p&gt;Weekend is near and again it's time to focus completely on some personal endeavours! 📸🏃‍♂️&lt;/p&gt;

&lt;p&gt;Although a lot of people feel / think that we should relax or do something else, including my family and friends, I rarely find myself interested in anything other than sitting with my laptop. 👨‍💻&lt;/p&gt;

&lt;p&gt;Whatever helps you relax, you should do it. And I utilise that time to learn new things or strengthen my expertise.&lt;/p&gt;

&lt;p&gt;This weekend, I am going to work on improving my skills on Progressive Web Apps and service workers. 💪 And planning to try out one or two from the tools I found on React Native development (I have a few on my browser's history that would serve as a checklist). Might also finish writing another article too! 🤓&lt;/p&gt;

&lt;p&gt;What are your plans?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Architecting React Applications - What I learned from my experience as a Web developer</title>
      <dc:creator>Rishi Kumar Chawda</dc:creator>
      <pubDate>Tue, 02 Jul 2019 16:40:06 +0000</pubDate>
      <link>https://dev.to/rishikc/architecting-react-applications-what-i-learned-from-my-experience-as-a-web-developer-4ma5</link>
      <guid>https://dev.to/rishikc/architecting-react-applications-what-i-learned-from-my-experience-as-a-web-developer-4ma5</guid>
      <description>&lt;p&gt;React has always been popular for its short learning curve and easy to use APIs. But if you have been working with this &lt;a href="https://reactjs.org/" rel="noopener noreferrer"&gt;popular javascript library&lt;/a&gt; for a while now, you might agree with me on this one as well -- If you don't pay attention to it's structure it will soon become an unmaintainable mess. &lt;/p&gt;

&lt;p&gt;Before joining my first job, I had some experience as a freelancer but didn't know much about best practices and architecture apart from what I had come across either online or while development. And even as a freelancer I didn't have experience with such large scale applications that we had to work with later in the job. This is when I started paying more attention to architecture. In the start, things used to get a little confusing sometimes - mostly because I was a bit more conscious about my code than before - which was obvious as I was part of a team now and not a solo freelancer.&lt;/p&gt;

&lt;p&gt;So I started looking through GitHub, online articles, papers and books. And as I kept working with React more and more on a large scale I had realised that &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The key to a robust, scalable and easy to maintain React application is architecture.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This applies to any application or software but with React, abstraction was a bit more difficult than other libraries / frameworks. This was until &lt;a href="https://reactjs.org/docs/hooks-intro.html" rel="noopener noreferrer"&gt;Hooks were introduced&lt;/a&gt; - but we will keep it out of the context for now as it is still fairly new and most of the applications are still built with older versions of React. Also, there are a lot of improvements to be made; lot of do's and dont's to be discovered with its usage. &lt;/p&gt;

&lt;p&gt;As of now, I think the principle that I follow for structuring React applications will work fairly well with Hooks too! Since the focus was on a scalable web application architecture - not just React.&lt;/p&gt;



&lt;p&gt;Let's quickly take a look at the setup and then I'll walk you through it and try to explain why it is structured that way. So the root of the project looks something like this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxyyk1ze63yzgr6ix5r2y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxyyk1ze63yzgr6ix5r2y.png" alt="base-structure-1" width="665" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And the &lt;code&gt;src&lt;/code&gt; directory ( which will contain all the source code for our application, ofcourse ) is structured like this : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx9708xht5gl9k9bt93ak.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx9708xht5gl9k9bt93ak.png" alt="base-structure-2" width="665" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first thing you might notice, and wonder maybe - and if you don't, I'd recommend taking a look again - that we have two directories named &lt;code&gt;config&lt;/code&gt; in our project. No, this isn't by mistake! It has a ( extremely ) simple reason. &lt;/p&gt;



&lt;h2&gt;
  
  
  Two config directories for a single web application?! Why though??
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;config&lt;/code&gt; directory at the root contains all the configuration files related to build - like our application's webpack config or any other bundler that we might use, environment files and other configs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1xrp30prq4b4wes9eg4g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1xrp30prq4b4wes9eg4g.png" alt="config directory snapshot" width="665" height="371"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You might also notice that it is nested and that the webpack configuration lives in its own directory. This makes the configurations more organised and easier to manage. This might seem trivial but when the application starts growing , and with that the build process might also get complex - which then demands a well organised place of its own. Also, this brings a peace of mind while working with it -- a large mess of configuration files is the last thing you might want while deploying your application in production! 👀&lt;/p&gt;



&lt;p&gt;The other &lt;code&gt;config&lt;/code&gt; directory inside our &lt;code&gt;src&lt;/code&gt; folder is for configurations related to our application, .i.e, the ones related to runtime. This may contain our json files ( or any other files ) that might shape the behaviour or capabilities of our app. Although, this may or may not be required as per your needs but for me, I have had this folder in most of the projects.&lt;/p&gt;



&lt;h2&gt;
  
  
  But wait, what about the &lt;code&gt;resources&lt;/code&gt; and &lt;code&gt;assets&lt;/code&gt; directories? Aren't assets also a part of the 'resources' for our react application?
&lt;/h2&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi4i6qyu61pdkc72dspzu.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi4i6qyu61pdkc72dspzu.jpg" alt="good question" width="675" height="474"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, the &lt;code&gt;assets&lt;/code&gt; directory here is meant &lt;strong&gt;&lt;em&gt;only&lt;/em&gt;&lt;/strong&gt; for images and other media &lt;em&gt;, duhh,&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;whereas, &lt;code&gt;resources&lt;/code&gt; is for data that might be required by our web application, for example, constants and other static data which basically doesn't have any or much logic associated to it. You can also add small methods to return the data, perhaps formatted to specific needs, and / or perform &lt;strong&gt;&lt;em&gt;minor&lt;/em&gt;&lt;/strong&gt; operations on them there which can be used by parts of our application, which by the way -- &lt;em&gt;trust me&lt;/em&gt; -- will make your code a lot cleaner and more organised.&lt;/p&gt;

&lt;p&gt;This directory may also contain data and other 'resources' which can be occasionally fetched, stored and updated; and maybe processed a little before they are used in certain parts of our web application. Well, I guess - you get the idea.&lt;/p&gt;



&lt;h2&gt;
  
  
  And what about our pages and all the react components??
&lt;/h2&gt;

&lt;p&gt;So, here comes the interesting part. Atleast I think so. This is something that has been derived from a few other solutions on architecting react applications as well as other web applications along with some of my own practical experience. And by far, I'm pretty satisfied with it! 🤓&lt;/p&gt;

&lt;p&gt;To start with, let's assume our web application contains a home page, a profile page for the users and just for the sake of not having just two pages in the example, a third page that we will call -- the other page. So the directory structure would look something like this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;
&lt;span class="o"&gt;-----&lt;/span&gt; &lt;span class="nx"&gt;components&lt;/span&gt;
&lt;span class="o"&gt;-----&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;
&lt;span class="o"&gt;-----&lt;/span&gt; &lt;span class="nx"&gt;pages&lt;/span&gt;
&lt;span class="o"&gt;---------&lt;/span&gt; &lt;span class="nx"&gt;home&lt;/span&gt;
&lt;span class="o"&gt;-----------&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;
&lt;span class="o"&gt;-----------&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scss&lt;/span&gt;    &lt;span class="c1"&gt;// Mandatory sass file (wanted to make this look realistic!!)&lt;/span&gt;
&lt;span class="o"&gt;---------&lt;/span&gt; &lt;span class="nx"&gt;profile&lt;/span&gt;
&lt;span class="o"&gt;-----------&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;
&lt;span class="o"&gt;---------&lt;/span&gt; &lt;span class="nx"&gt;other&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;page&lt;/span&gt;
&lt;span class="o"&gt;-----------&lt;/span&gt; &lt;span class="nx"&gt;components&lt;/span&gt;
&lt;span class="o"&gt;-----------&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;
&lt;span class="o"&gt;-----&lt;/span&gt; &lt;span class="nx"&gt;resources&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how all the pages have their own separate directory with an entry point? And how that 'other' page has a component folder? Why do we need another component folder? Don't we already have a component folder in the root of &lt;code&gt;src&lt;/code&gt; directory?&lt;/p&gt;

&lt;p&gt;Wait, just hold on for a second! I'll explain it real quick! ☝️&lt;/p&gt;

&lt;p&gt;This is what I call the "branching" structure. Each page has their own directory, their own set of components which are not used anywhere else except in that specific page, their own styles rules and other stuff which are associated with only that page. If any component is shared by two pages, guess where they'd go? Yes, you guessed it right -- the &lt;code&gt;components&lt;/code&gt; directory in the root of our &lt;code&gt;src&lt;/code&gt; directory!&lt;/p&gt;

&lt;p&gt;But.. you might wonder.. what is the point of doing that?&lt;/p&gt;

&lt;p&gt;Let's say, one day you and your teammates decide to get rid of the 'other' page -- &lt;em&gt;maybe the name wasn't good enough?&lt;/em&gt; -- so what do you do? Spend an entire afternoon or a day on removing code, breaking and fixing the application? &lt;strong&gt;NO&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You just go ahead and delete the directory and remove its reference from where it was attached to / used in the web application. &lt;em&gt;And voila, it's done!&lt;/em&gt; 💁🏻‍♂️&lt;/p&gt;

&lt;p&gt;Nothing breaks in your app just because a bunch of code was deleted! Everything is independent of each other's existence even if they were binded together at some point! A lot less to work with and worry about, isn't it? And yeah, this principle can be applied to almost any application / software and not just some react application.&lt;/p&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgdyexosy77ta75ohlx80.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgdyexosy77ta75ohlx80.jpg" alt="welcome" width="498" height="307"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Some of you might think -- Well no, our application / software is quite complex and stuff is just too interconnected with each other. They &lt;strong&gt;&lt;em&gt;shared&lt;/em&gt;&lt;/strong&gt; code, were &lt;strong&gt;&lt;em&gt;bridged&lt;/em&gt;&lt;/strong&gt; together, etc. But I guess you might understand now what to do with the "shared code" and "bridges" if you try to apply this principle to it! This is just a simple example to demonstrate and give you an idea of how parts of the product can be organised for convenience and maintainability.&lt;/p&gt;



&lt;h2&gt;
  
  
  A little tip - something that I learnt while developing progressive web applications with GatsbyJS
&lt;/h2&gt;

&lt;p&gt;You can also go ahead and add another directory to the &lt;code&gt;src&lt;/code&gt; -- called &lt;code&gt;layouts&lt;/code&gt; ( or maybe add it to the &lt;code&gt;components&lt;/code&gt; directory, whichever feels more appropriate to you ) which contains a layout file which is global to the application, or even have multiple layouts ; each associated with certain parts of the application. For example, let's assume our application also has a fancy navbar and a decent footer which goes into all of our pages. Instead of having them shoved inside our &lt;code&gt;components&lt;/code&gt; directory and then repeatedly used inside each page - we can have a layout file that contains the navbar and the footer and renders the &lt;code&gt;children&lt;/code&gt; that are passed to it, like so :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Layout&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    Yayy! This is my fancy home page!!
  &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Layout&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;// And in the profile page :&lt;/span&gt;

&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Layout&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    This is the page of the user whose data we're secretly trying to steal! 
    Please read our privacy policies (not so) carefully!!
  &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Layout&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in our Layout file, we can have something similar to this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Layout&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;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Navbar&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;Footer&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Layout&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;p&gt;This makes your code more organised and logically abstracted. Also, it'll help in keeping your pages consistent.&lt;/p&gt;

&lt;h2&gt;
  
  
  But wait.. There's more to architecting react applications!!
&lt;/h2&gt;

&lt;p&gt;Yes, I haven't forgotten about reducers, the lengthy sagas, services, a ton of action creators and what not! But that's for the &lt;a href="https://dev.to/rishikc/architecting-react-applications-part-ii-prettifying-the-complex-codebase-2l43"&gt;second part&lt;/a&gt; of this article since I don't want it to become too long and exhausting to read. Also, this first part might serve as a good starting point for beginners or other developers who are new to React.&lt;/p&gt;



&lt;p&gt;&lt;em&gt;Thank you for reading! Let me know in the discussion thread below what you think about this post.&lt;/em&gt; 😄&lt;/p&gt;

&lt;p&gt;You can also connect with me through &lt;a href="https://twitter.com/rishiikc" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;



&lt;p&gt;Happy hacking! Cheers! 🎉&lt;/p&gt;

</description>
      <category>react</category>
      <category>architecture</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Automate lighthouse audits for your Progressive Web Application</title>
      <dc:creator>Rishi Kumar Chawda</dc:creator>
      <pubDate>Tue, 02 Jul 2019 10:12:02 +0000</pubDate>
      <link>https://dev.to/rishikc/automate-lighthouse-audits-for-your-progressive-web-application-3lfc</link>
      <guid>https://dev.to/rishikc/automate-lighthouse-audits-for-your-progressive-web-application-3lfc</guid>
      <description>&lt;p&gt;We all know how important and helpful the insights are from lighthouse audits when we're developing our web applications. But the way most of us check is manually through Chrome devtools or the lighthouse extension, which in my opinion, is not very productive.&lt;/p&gt;

&lt;p&gt;For those of us who don't know, there are mainly four ways of auditing our web application with lighthouse :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Chrome devtools&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Command line&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;NPM module ( which we are going to use in a while )&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developers.google.com/speed/pagespeed/insights/" rel="noopener noreferrer"&gt;PageSpeed Insights&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the purpose of programmatically performing lighthouse audits, we will use the &lt;a href="https://www.npmjs.com/package/lighthouse" rel="noopener noreferrer"&gt;lighthouse npm package&lt;/a&gt;, &lt;a href="https://mochajs.org/" rel="noopener noreferrer"&gt;mocha&lt;/a&gt; and &lt;a href="https://www.chaijs.com" rel="noopener noreferrer"&gt;chai&lt;/a&gt; for writing our tests and &lt;a href="https://www.npmjs.com/package/chrome-launcher" rel="noopener noreferrer"&gt;chrome-launcher&lt;/a&gt; for running our lighthouse tests.&lt;/p&gt;

&lt;p&gt;First, let's install the above packages as dev dependencies in our project :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;lighthouse chrome-launcher chai mocha &lt;span class="nt"&gt;--save-dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's create a file named &lt;code&gt;lighthouse.tests.js&lt;/code&gt; in our &lt;code&gt;tests&lt;/code&gt; directory. We'll run our lighthouse audits through this file.&lt;br&gt;
Here, we'll import the lighthouse module and chrome launcher - which will enable us to open our webpage from local development server and run the audits to test against a minimum threshold that we want our lighthouse scores to be.&lt;/p&gt;

&lt;p&gt;While this might sound a lot to do, it isn't much. Here's what it looks like on actual code :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;lighthouse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lighthouse&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;chromeLauncher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chrome-launcher&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;launchChromeAndRunLighthouse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;opts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;conf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;chromeLauncher&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;chromeFlags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;opts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;chromeFlags&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;chrome&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;opts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;chrome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;lighthouse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;opts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;conf&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;chrome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;kill&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lhr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And it is as simple as that. We launch the chrome browser instance with &lt;code&gt;chromeLauncher.launch&lt;/code&gt; method and then run lighthouse tests with the site url and configuration for our audits. After that is done, we close / kill the chrome instance and return the results. This is how it looks like when in use :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;launchChromeAndRunLighthouse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;testUrl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;opts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&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;// Results are available in `res`&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now, we can put this call inside our &lt;code&gt;before&lt;/code&gt; hook for the tests and then have tests for each metric, something like this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lighthouse Audits&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Timeout doesn't need to be same. It can be more or less depending on your project.&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50000&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;results&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;before&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;run test&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;done&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;launchChromeAndRunLighthouse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;testUrl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;opts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&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;// Extract the results you need for your assertions.&lt;/span&gt;
      &lt;span class="nf"&gt;done&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="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;performance test&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;done&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;// test your performance score against the threshold&lt;/span&gt;
    &lt;span class="nf"&gt;done&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="c1"&gt;// Some more tests..&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Still looks weird? Don't worry! Checkout this repository for example setup of &lt;a href="https://github.com/rishichawda/lighthouse-mocha-example" rel="noopener noreferrer"&gt;lighthouse tests with mocha&lt;/a&gt; and try that out with your web application!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(You can find a list of related projects through &lt;a href="https://github.com/GoogleChrome/lighthouse#related-projects" rel="noopener noreferrer"&gt;this link&lt;/a&gt;, including an example with jest)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This method can be applied to automate the tests in continuous integration / deployment environments so that you don't have to worry about manually auditing your web application and checking wether it meets the minimum satisfactory levels.&lt;/p&gt;

&lt;p&gt;So there you go. That's all we need to do for automating lighthouse audits for our progressive web applications to make sure they are always worthy for the internet and user's data packets!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4i8uz30qr4xztb6juehd.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4i8uz30qr4xztb6juehd.gif" alt="worthy" width="378" height="175"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;&lt;em&gt;Thank you for reading!&lt;/em&gt; 😄&lt;/p&gt;

&lt;p&gt;You can also connect with me via &lt;a href="https://twitter.com/rishiikc" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;



&lt;p&gt;Happy hacking! Cheers! 🎉&lt;/p&gt;

</description>
      <category>webperf</category>
      <category>webdev</category>
      <category>testing</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Is Web Assembly the future?</title>
      <dc:creator>Rishi Kumar Chawda</dc:creator>
      <pubDate>Tue, 02 Jul 2019 06:57:34 +0000</pubDate>
      <link>https://dev.to/rishikc/is-web-assembly-the-future-17mg</link>
      <guid>https://dev.to/rishikc/is-web-assembly-the-future-17mg</guid>
      <description>&lt;p&gt;Recently, I have seen and read articles on web assembly out of curiosity and was wondering if it would be a significant part of web development in the future since it provides better performance than Javascript.&lt;/p&gt;

&lt;p&gt;( I'm a Javascript developer - React and React Native - and was exploring more on web development so that I've something new to learn over the next couple of weekends. )&lt;/p&gt;

&lt;p&gt;What are your thoughts, devs?&lt;/p&gt;

</description>
      <category>webassembly</category>
      <category>webperf</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Managing React application state - without using Redux!</title>
      <dc:creator>Rishi Kumar Chawda</dc:creator>
      <pubDate>Sun, 02 Jun 2019 04:45:34 +0000</pubDate>
      <link>https://dev.to/rishikc/managing-react-application-state-without-using-redux-8no</link>
      <guid>https://dev.to/rishikc/managing-react-application-state-without-using-redux-8no</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;Disclaimer:&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;This solution is best suited for small scale projects and its main motive is to explore the new APIs react provides rather than trying to replace any traditional solutions.&lt;/em&gt;&lt;/p&gt;



&lt;p&gt;So when was the last time you tried managing application state, without Redux? As far as I can remember, for me, it was probably during the time I had started learning and playing around with React. Although I had known about Redux at the time when I did try not using it, I just wanted to do it because I didn't want to add &lt;strong&gt;three&lt;/strong&gt; dependencies to my react application just for managing a really small application state. Ofcourse, when we talk about using redux, we are also going to use &lt;code&gt;react-redux&lt;/code&gt; and a middleware too!&lt;/p&gt;

&lt;p&gt;I basically did it in two different ways ( which most of us, might have tried at some point as well ) :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Using localStorage and custom listeners.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using the Context API.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;&lt;strong&gt;But&lt;/strong&gt; in this article, we're not going to discuss that. We're going to see another way of managing the state which is fairly new - using &lt;strong&gt;&lt;em&gt;Hooks&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So, let's setup a react project and add a root component, like so :&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And our &lt;code&gt;ChildComponent.js&lt;/code&gt; as,&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;First, let us break down the complete flow and decide what we need :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A state, &lt;em&gt;ofcourse&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A way to mutate / update our state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A way to sync the data from state to our components wherever required.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Do all of this while keeping the code clean. ( Very important )&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's setup our state first. For this, I'm going to use the &lt;code&gt;useReducer&lt;/code&gt; hook. For those who are not familiar with the &lt;code&gt;useReducer&lt;/code&gt; hook - it is similar to the basic &lt;code&gt;useState&lt;/code&gt; hook but more suited for this case as it provides a dispatch method, the previous state while computing and updating state, etc. Basically, it will provide us a way which is similar to Redux's reducer and action flow. Let's set up our &lt;code&gt;useGlobalState&lt;/code&gt; hook which will help us initialize our state and provide us a dispatch for updating it as well.&lt;/p&gt;

&lt;p&gt;So our &lt;code&gt;redux.js&lt;/code&gt; looks like this :&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;So what's going on here? Our custom hook here takes two arguments - &lt;code&gt;initialState&lt;/code&gt; for setting an initial state to our app and &lt;code&gt;reducer&lt;/code&gt; is our reducer function which is going to be used for updating state depending on the actions. &lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;reducer.js&lt;/code&gt; might look like this : &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;With those two arguments, we can initialise our reducer and state as :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dispatch&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useReducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Since our &lt;code&gt;initialState&lt;/code&gt; might, in some cases, require some computation and may not be just a static value - we are going to use the third argument of &lt;code&gt;useReducer&lt;/code&gt; to initialise our state from a function just incase we need to. So now we can initialise our state in two ways :&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;globalState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useGlobalState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;intialState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

 &lt;span class="c1"&gt;// OR&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;globalState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useGlobalState&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;// Do stuff here.&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;But, where do we initialise our state? Well, this needs to be done inside the root component since Hooks can only be called / used inside a functional component. Let's add this to our &lt;code&gt;App.js&lt;/code&gt; file and use the classic example of making a simple counter.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;This gives us something like this : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F74chmkyjby7p1p4afwcy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F74chmkyjby7p1p4afwcy.gif" alt="globalstate redux hook" width="775" height="293"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But still we can't use the state inside our &lt;code&gt;ChildComponent&lt;/code&gt; since it has no idea of this state. So how are we going to do this?&lt;/p&gt;

&lt;p&gt;This is a slightly interesting step now. We're going to use the &lt;code&gt;createContext&lt;/code&gt; API for that. Let's update our &lt;code&gt;redux.js&lt;/code&gt; to give us a way to pass the state to our child(ren) component(s), like so :&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;I guess you can clearly see where this is going. We are going to use the &lt;code&gt;Provider&lt;/code&gt; component in our root &lt;code&gt;App.js&lt;/code&gt; and wrap our component with it. Additionally, we'll pass an initial state as the value for our 'provider'. This makes the values available in the DOM tree. But then you might wonder - we need to wrap all our children who are going to use the state with &lt;code&gt;context.Consumer&lt;/code&gt;, don't we? Well, no.&lt;/p&gt;

&lt;p&gt;Here's where our &lt;code&gt;React.useContext&lt;/code&gt; hook comes into play along with a little HOC trick. And we're going to name it &lt;code&gt;connect&lt;/code&gt; so it looks similar to redux! Also, it will be easier to understand if we can visualise it in the 'redux way'. But first, let's check if our current setup works.&lt;/p&gt;

&lt;p&gt;Update the &lt;code&gt;App.js&lt;/code&gt; file to this :&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And our &lt;code&gt;ChildComponent.js&lt;/code&gt; like this :&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;So what does &lt;code&gt;useContext&lt;/code&gt; hook do? Well, it's similar to using &lt;code&gt;context.Consumer&lt;/code&gt; tag which allowed us to access context value and subscribe to its changes. With &lt;code&gt;useContext&lt;/code&gt; hook, we no longer use the &lt;code&gt;context.Consumer&lt;/code&gt; in our component. We pass the context object to it, which then returns the value from the current context. Whenever the context data changes, the component is re-rendered with the new values.&lt;/p&gt;

&lt;p&gt;Let's see if this works.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx854lmw0lubkiv81vl8p.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx854lmw0lubkiv81vl8p.gif" alt="globalstate redux hook with provider" width="775" height="291"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Great! But there's one thing. Now we need to call &lt;code&gt;useContext&lt;/code&gt; in every component! Let's get rid of this. We're going to write a small HOC which exposes an API similar to the &lt;code&gt;connect&lt;/code&gt; HOC from &lt;code&gt;react-redux&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, our &lt;code&gt;redux.js&lt;/code&gt; should look like this :&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note :&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;As you can see, we are just spreading the props over the component here. The ideal way is to use &lt;code&gt;hoist-non-react-statics&lt;/code&gt; package to copy all non-react static methods to the new component. Please use that way since it is better than just passing the props. Complete explanation can be found in the &lt;a href="https://reactjs.org/docs/higher-order-components.html#static-methods-must-be-copied-over" rel="noopener noreferrer"&gt;React Documentation for Higher Order Components&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;connect&lt;/code&gt; HOC here takes our component and uses the context to get all the props that are required by the component as defined in the &lt;code&gt;mapStateToProps&lt;/code&gt; function in the &lt;code&gt;connect&lt;/code&gt; call. We can update our &lt;code&gt;ChildComponent.js&lt;/code&gt; now, to something like this :&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Let's check if this works.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjmllkcijf9p2blajfbzj.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjmllkcijf9p2blajfbzj.gif" alt="globalstate redux hook with connect hoc" width="775" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Incase you're wondering, you can have different counters for both of them and it'll work just fine! You just need to initialise the state with both the counters, dispatch actions from their respective buttons and use the respective values from the state to display. Like so :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// In App.js, initialise counters like this&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;globalState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useGlobalState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;anothercount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * In ChildComponent.js, update the `mapState` and `mapDispatch` methods
 * to get and update `anothercount` value from state.
 */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mapState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;anothercount&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;// Get the `anothercount` value from state.&lt;/span&gt;
  &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;anothercount&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;mapDispatch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dispatch&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;// Update the dispatch to trigger `countincrement` action.&lt;/span&gt;
  &lt;span class="na"&gt;updateCounter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;countincrement&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mapState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mapDispatch&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="nx"&gt;ChildComponent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/** 
 * Finally, update our reducer to handle `countincrement` action,
 * which updates the `anothercount` value in our state.
 */&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&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;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;increment&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="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="p"&gt;};&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;countincrement&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;anothercount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;anothercount&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="p"&gt;};&lt;/span&gt;
    &lt;span class="nl"&gt;default&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;state&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Oh, and one more thing! Don't forget to wrap your components with &lt;code&gt;React.memo&lt;/code&gt; if they're &lt;strong&gt;not&lt;/strong&gt; connected to state. This will prevent unnecessary re-renders when the state udpates!&lt;/p&gt;

&lt;p&gt;And we're done implementing a small redux-like application state management in our react application! All within just 40 lines on code! ✨&lt;/p&gt;

&lt;p&gt;You can check out the complete example in &lt;a href="https://github.com/rishichawda/globalstate-hook-example" rel="noopener noreferrer"&gt;this github repository&lt;/a&gt;. Please leave a star on the repository or comment here if you liked this article!&lt;/p&gt;



&lt;p&gt;You can connect with me via &lt;a href="http://twitter.com/rishiikc" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thank you for reading!&lt;/em&gt; 😄&lt;/p&gt;



&lt;p&gt;Happy hacking! Cheers! 🎉&lt;/p&gt;





</description>
      <category>react</category>
      <category>redux</category>
      <category>hooks</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Shrink your React Native application size dramatically!</title>
      <dc:creator>Rishi Kumar Chawda</dc:creator>
      <pubDate>Sat, 01 Jun 2019 10:05:00 +0000</pubDate>
      <link>https://dev.to/rishikc/shrink-your-react-native-application-size-dramatically-3ekj</link>
      <guid>https://dev.to/rishikc/shrink-your-react-native-application-size-dramatically-3ekj</guid>
      <description>&lt;p&gt;So you made a cool and awesome looking &lt;a href="https://facebook.github.io/react-native/" rel="noopener noreferrer"&gt;React Native&lt;/a&gt; app and now you’re ready to build it and maybe publish it to the store —&lt;/p&gt;

&lt;p&gt;But worried about it if the users would want to install it given its build size? Or maybe you just want to keep it light weight and not take too much memory unnecessarily when it can be packed into a smaller size?&lt;/p&gt;

&lt;p&gt;Or you’re just one of us who are paranoid about build sizes? Don’t worry, we got you covered! 😄 ✔️&lt;/p&gt;




&lt;p&gt;In this article, we will cover the following two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Compress our react native application size - by compressing the java bytecode that is generated while building our app and also asking it to try and shrink all the resources that are bundled with the application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Splitting our application bundle into multiple &lt;code&gt;apk&lt;/code&gt;s to remove unnecessary code which is not required by the device which is going to run it - because a lot of code is bundled with the universal apk that is device specific - meaning that we don't need a lot of code in the device we're going to install it in.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Let's get started! ✊&lt;/p&gt;

&lt;p&gt;First of all, you’ll need to eject your native app if you’re using &lt;a href="https://github.com/react-community/create-react-native-app" rel="noopener noreferrer"&gt;create-react-native-app&lt;/a&gt; for your project ( You might have already done this if you’ve built your application before reading this article ). This is important since you don’t have access to configurations until you eject, as the build folder is where we have to make changes. If you haven’t, you can simply do this by :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run eject
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note :&lt;/strong&gt; Ejecting a react native application is a permanent action! ( Unless you’re using a version control system to keep track of previous versions of your app — from where you can recover the ‘unejected’ version of your app later if you need ). &lt;a href="https://github.com/react-community/create-react-native-app/blob/master/EJECTING.md" rel="noopener noreferrer"&gt;Learn more about ejecting.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Okay so now we’re all set! Let’s get started and get that done fast. Don’t worry it just takes a few minutes and your app size will shrink dramatically!&lt;/p&gt;

&lt;p&gt;Now, navigate to the &lt;code&gt;android / app&lt;/code&gt; folder from your project root directory where you can find your &lt;code&gt;build.gradle&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy9mbo9b9q2o2kkxz9q9e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy9mbo9b9q2o2kkxz9q9e.png" width="800" height="468"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, you’ll find your default build configurations already setup, find the line which looks like this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="kt"&gt;def&lt;/span&gt; &lt;span class="n"&gt;enableProguardInReleaseBuilds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="kt"&gt;def&lt;/span&gt; &lt;span class="n"&gt;enableSeparateBuildPerCPUArchitecture&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and change their value to &lt;code&gt;true&lt;/code&gt; like this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="kt"&gt;def&lt;/span&gt; &lt;span class="n"&gt;enableProguardInReleaseBuilds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="kt"&gt;def&lt;/span&gt; &lt;span class="n"&gt;enableSeparateBuildPerCPUArchitecture&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So you might be wondering what it does. Well, if you scroll down a bit you’ll see &lt;code&gt;enableProguardInReleaseBuilds&lt;/code&gt; and &lt;code&gt;enableSeparateBuildPerCPUArchitecture&lt;/code&gt; written at a few more places like here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fet8qem22bzril66fhl8k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fet8qem22bzril66fhl8k.png" width="800" height="252"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, these variables are being used to enable or disable two build configurations —&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One for generating separate .apks for different device architectures,
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="o"&gt;...&lt;/span&gt;
&lt;span class="n"&gt;splits&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;abi&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;reset&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
      &lt;span class="n"&gt;enable&lt;/span&gt; &lt;span class="n"&gt;enableSeparateBuildPerCPUArchitecture&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Don’t worry about having to handle different .apks for each architecture — Google takes care of distributing it to the users! And separating the builds according to architecture removes unnecessary code from your file which is not required on the device it is running.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Another one for compressing the Java bytecode generated while building, as in,
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="o"&gt;...&lt;/span&gt;
&lt;span class="n"&gt;buildTypes&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;release&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;minifyEnabled&lt;/span&gt; &lt;span class="n"&gt;enableProguardInReleaseBuilds&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Phew, that was pretty easy! But wait, we’re not done yet! There’s one little thing we need to do.&lt;/p&gt;

&lt;p&gt;Now let’s add this line right below the &lt;code&gt;minifyEnabled&lt;/code&gt; configuration :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="o"&gt;...&lt;/span&gt;
&lt;span class="n"&gt;buildTypes&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;release&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;minifyEnabled&lt;/span&gt; &lt;span class="n"&gt;enableProguardInReleaseBuilds&lt;/span&gt;
        &lt;span class="n"&gt;shrinkResources&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="cm"&gt;/* &amp;lt;-- Add this line */&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we’re done! Now build your app again and check the &lt;code&gt;output&lt;/code&gt; directory. You’ll find two different &lt;code&gt;.apks&lt;/code&gt; of your app which are specified in the configuration by default, i.e., for &lt;code&gt;armebi&lt;/code&gt; and &lt;code&gt;x86&lt;/code&gt; architectures.&lt;/p&gt;

&lt;p&gt;Oh, and by the way if you need a universal &lt;code&gt;.apk&lt;/code&gt; that supports all device architectures — just set &lt;code&gt;universalApk&lt;/code&gt; to true and it’ll generate a universal &lt;code&gt;.apk&lt;/code&gt; next time you run build!&lt;/p&gt;

&lt;p&gt;That’s all! Now you’ve setup your build configuration to shrink your code along with resources and create separate &lt;code&gt;.apk&lt;/code&gt; for different architectures — thus removing unnecessary code from the final build.&lt;/p&gt;

&lt;p&gt;Thanks for reading! You can also check out more resources on &lt;a href="https://developer.android.com/studio/build/shrink-code" rel="noopener noreferrer"&gt;how to reduce the application build size&lt;/a&gt;. 😄&lt;/p&gt;



&lt;p&gt;&lt;em&gt;Did you like this article? Or did I miss something? Is there something that you have that can be added to this article -- that can make it even better?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You can connect with me via &lt;a href="http://twitter.com/rishiikc" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;



&lt;p&gt;Happy Hacking! Cheers!&lt;/p&gt;





</description>
      <category>reactnative</category>
      <category>android</category>
    </item>
  </channel>
</rss>
