<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nir Hadassi on Medium]]></title>
        <description><![CDATA[Stories by Nir Hadassi on Medium]]></description>
        <link>https://medium.com/@nirsky?source=rss-69c2729ebf4b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*rS0aQRlVFT9OlMbV3OGnCA.png</url>
            <title>Stories by Nir Hadassi on Medium</title>
            <link>https://medium.com/@nirsky?source=rss-69c2729ebf4b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 11 Apr 2026 03:22:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nirsky/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Easily deploy TypeScript project to AWS Lambda using Github Actions]]></title>
            <link>https://medium.com/aspecto/easily-deploy-typescript-project-to-aws-lambda-using-github-actions-5830c1cc3997?source=rss-69c2729ebf4b------2</link>
            <guid isPermaLink="false">https://medium.com/p/5830c1cc3997</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[github-actions]]></category>
            <category><![CDATA[lambda]]></category>
            <dc:creator><![CDATA[Nir Hadassi]]></dc:creator>
            <pubDate>Thu, 18 Mar 2021 12:31:03 GMT</pubDate>
            <atom:updated>2021-03-21T14:51:50.894Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Trying to deploy your TypeScript project to AWS Lambda and struggling to do so? You’ve come to the right place.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*vhXCF3elDEQt9g6YZraEEQ.png" /><figcaption>TypeScript and Lamda can have fun together</figcaption></figure><p>I recently worked on a small TypeScript project which I wanted to deploy as a Lambda to AWS.<br>My project had <strong>multiple files</strong> and some <strong>npm dependencies</strong>.</p><p>I found some resources online, but none of them really hit the spot. Some didn’t explain how to include the node_modules, some didn’t explain how to do it with TypeScript and some just handled a single file.</p><p>In this post, I’ll describe how to prepare the project for deployment and show a detailed example of doing it using Github Actions.</p><h3>Project Structure</h3><p>Let’s assume our project structure looks like this:</p><pre>├── package.json<br>├── tsconfig.json<br>├── node_modules<br>│    ├── module_1<br>│    ├── module_2<br>│    └── etc..<br>└── src<br>     ├── index.ts<br>     └── utils.ts</pre><p>This is our tsconfig.json :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ddc5e3fb49f38b79b106c405d16909fe/href">https://medium.com/media/ddc5e3fb49f38b79b106c405d16909fe/href</a></iframe><p>And index.ts contains as the main handler, and uses utils.ts, something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a89b0a2d0488780e503ac9544e225299/href">https://medium.com/media/a89b0a2d0488780e503ac9544e225299/href</a></iframe><h3>Problem</h3><p>AWS Lamda can only run <strong>js</strong> files!<br>Also, AWS Lambda can’t install node_modules for us.</p><p>With the default settings, AWS Lambda supports either:</p><ul><li>Deploying a single index.<strong>js </strong>file directly.</li><li>Upload a <strong>zip</strong> file containing all the project files, which has the index.js in the zip root. So we need to zip the project correctly, without including the parent directory. <br>We also need to make sure the node_modules are part of this zip file.</li></ul><figure><img alt="right zip structure when deploying a project to aws lambda" src="https://cdn-images-1.medium.com/max/450/1*sk5KNmmy_hoKpbQ2Vt31bA.jpeg" /><figcaption>Expected Zip file structure</figcaption></figure><h3><strong>Prepare for deployment</strong></h3><h4>1. Build the project</h4><p>We will use TypeScript’s tsc command to transpile the project into js files.<br>After building, this will be our project structure:</p><pre>├── package.json<br>├── tsconfig.json<br>├── node_modules<br>├── <strong>dist</strong><br>│    ├── index.js<br>│    └── utils.js<br>└── src<br>     ├── index.ts<br>     └── utils.ts</pre><h4>2. Move node_modules to dist folder</h4><p>As mentioned before, AWS Lambda can’t install the node_modules for us, so we need to assure they are located correctly before creating our zip file.</p><h4>3. ZIP</h4><p>The project needs to be zipped in the required structure, <strong>without the parent directory.</strong> The easiest way I found to do it, is to run the following command from the project root:</p><pre>$ (cd dist &amp;&amp; zip -r ../function.zip .)</pre><p>The zip is now ready for deployment.</p><h3>Deploy using Github Actions</h3><p>The deployment part will be done using <a href="https://github.com/marketplace/actions/aws-lambda-deploy"><strong><em>appleboy/lambda-action</em></strong></a>, and can be easily substituted with a corresponding AWS CLI command, which comes preinstalled on Github Actions CI machines.</p><p>Here’s a detailed example of a Github action:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0bd8dc32195ebe27ca7443b251a9982c/href">https://medium.com/media/0bd8dc32195ebe27ca7443b251a9982c/href</a></iframe><p>That’s it! Your Lambda is now ready for work.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5830c1cc3997" width="1" height="1" alt=""><hr><p><a href="https://medium.com/aspecto/easily-deploy-typescript-project-to-aws-lambda-using-github-actions-5830c1cc3997">Easily deploy TypeScript project to AWS Lambda using Github Actions</a> was originally published in <a href="https://medium.com/aspecto">Aspecto</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Attach EBS Docker Volume to AWS ECS using CDK]]></title>
            <link>https://medium.com/aspecto/attach-ebs-docker-volume-to-aws-ecs-using-cdk-931c29e0e1?source=rss-69c2729ebf4b------2</link>
            <guid isPermaLink="false">https://medium.com/p/931c29e0e1</guid>
            <dc:creator><![CDATA[Nir Hadassi]]></dc:creator>
            <pubDate>Thu, 04 Jun 2020 08:29:12 GMT</pubDate>
            <atom:updated>2021-03-31T14:17:57.628Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to attach EBS Docker Volume to AWS ECS using CDK</h3><p>Wow, lot’s of abbreviations in the title, let’s get things straight first:<br>• EBS— Elastic Block Storage.<br>• CDK — Cloud Development Kit.<br>• ECS — Elastic Container Service.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Gl1ZGinQaBy2Dlc_0KZoiA.png" /><figcaption>Bunch of logos</figcaption></figure><p>I won’t go into long intros, if you’re here you probably know why. <br>This post will walk you through the required steps to get EBS working, the same way we achieved this at <a href="https://www.aspecto.io/">Aspecto</a>.<br>I’d be using JS CDK snippets.<br>Notice EBS will only work with EC2 launch type (Fargate won’t support it).</p><p>Steps Overview:<br>• Install rexray/ebs docker plugin using “UserData”.<br>• Give your EC2 instance the required roles.<br>• Create and attach EBS volume.</p><p>First, we need the get a hold of the cluster AutoScalingGroup:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df8bbb42231fa6a689a7013d24149e6b/href">https://medium.com/media/df8bbb42231fa6a689a7013d24149e6b/href</a></iframe><h3>Install REX-Ray EBS plugin</h3><p>Now that we have the AutoScalingGroup, we can attach “UserData” to it.<br>UserData is basically a bash script that runs when the EC2 instance is instantiated.<br>This script will install the <a href="https://rexray.readthedocs.io/en/stable/user-guide/storage-providers/aws/#elastic-block-storage">REX-Ray EBS</a> plugin and will reset our ECS after.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7949af67f06418a51294ea3064686ba8/href">https://medium.com/media/7949af67f06418a51294ea3064686ba8/href</a></iframe><h3>Give your EC2 instance the required roles</h3><p>Your EC2 gonna manage your EBS, it gonna need some roles to do it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4d4f21a90710af129f9859859bf8b675/href">https://medium.com/media/4d4f21a90710af129f9859859bf8b675/href</a></iframe><h3>Attach EBS Docker Volume to your TaskDefinition</h3><p>Cool, our ECS cluster can now work with EBS, all that’s left is attaching the EBS <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/docker-volumes.html">volume</a> and mount it to our container:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7fddd45a7cb2b581e0b35c585dcd81b3/href">https://medium.com/media/7fddd45a7cb2b581e0b35c585dcd81b3/href</a></iframe><p>That’s it, you’re done!</p><p>Let me know in the comments below how it worked out for you, I’m here to answer every question.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=931c29e0e1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/aspecto/attach-ebs-docker-volume-to-aws-ecs-using-cdk-931c29e0e1">Attach EBS Docker Volume to AWS ECS using CDK</a> was originally published in <a href="https://medium.com/aspecto">Aspecto</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Make your npm package work on both Node.js and browser]]></title>
            <link>https://medium.com/aspecto/make-your-npm-package-work-on-both-node-js-and-browser-58bff1a18f55?source=rss-69c2729ebf4b------2</link>
            <guid isPermaLink="false">https://medium.com/p/58bff1a18f55</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[npm]]></category>
            <dc:creator><![CDATA[Nir Hadassi]]></dc:creator>
            <pubDate>Tue, 17 Dec 2019 14:59:24 GMT</pubDate>
            <atom:updated>2021-06-30T12:19:48.029Z</atom:updated>
            <content:encoded><![CDATA[<p><em>How to avoid native node modules (like fs, child_process, etc..) crashing your app while running on the browser (this post is </em><strong><em>not</em></strong><em> about UMD!)</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*VJExv4dGMQ7df14AqBx8xg.png" /></figure><p>I recently worked on an npm package that is supposed to be consumed on both Node.js environment and the browser (using React/Angular/etc..).</p><p>Things started getting complicated when the node branch of the code needed to require native Node.js packages — like fs.</p><p>Consider the following (simplified) code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f45413a2ddcc403eb7a2cbfc7733057/href">https://medium.com/media/6f45413a2ddcc403eb7a2cbfc7733057/href</a></iframe><p>When testing the module on my React + Webpack app, the app crashed:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/1*PzGhBSDlBZIq7gwEud3RjQ.png" /></figure><p>This happened even though the node-handler file wasn’t executed, this is due to Webpack nature of creating a bundle containing all the code.</p><h4>Step 1: Postpone your requires</h4><p>Instead of requiring fs in the global scope, we can require it only where we actually need it, that way we won’t require it when running in the browser:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5902c665d79f0f782b1d74dc2fcbb0c1/href">https://medium.com/media/5902c665d79f0f782b1d74dc2fcbb0c1/href</a></iframe><p>Cool! Our React app is not crashing anymore! But we do get an annoying compilation warning right now:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/662/1*2XjXdAOpKnXQBfceS9Fl5A.png" /></figure><p>While we can live with a warning ⚠️ , our end users will probably won’t like this too much and will end up not installing our package.</p><h4>Step 2: eval your require</h4><p>This is not the most elegant solution (to say the least..), but it’s keeping Webpack quiet and your end users happy. Instead of using require(&#39;fs&#39;), we’re gonna use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval">eval</a>(&#39;require&#39;)(&#39;fs&#39;):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/da4254e03561fc4efc6aad48f61145c7/href">https://medium.com/media/da4254e03561fc4efc6aad48f61145c7/href</a></iframe><p>Good luck! May you be blessed with tons of stars 🌟</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=58bff1a18f55" width="1" height="1" alt=""><hr><p><a href="https://medium.com/aspecto/make-your-npm-package-work-on-both-node-js-and-browser-58bff1a18f55">Make your npm package work on both Node.js and browser</a> was originally published in <a href="https://medium.com/aspecto">Aspecto</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Class features vs. Hooks equivalents]]></title>
            <link>https://medium.com/soluto-engineering/react-class-features-vs-hooks-equivalents-745368dafdb3?source=rss-69c2729ebf4b------2</link>
            <guid isPermaLink="false">https://medium.com/p/745368dafdb3</guid>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[cheatsheet]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Nir Hadassi]]></dc:creator>
            <pubDate>Tue, 25 Dec 2018 12:27:21 GMT</pubDate>
            <atom:updated>2019-12-03T08:47:30.286Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6UJ-dw4PN3RfH2TGXr-1PQ.png" /></figure><p>I gave a talk during a recent meetup at Soluto HQ — Intro to React Hooks.<br> While preparing for the presentation, I fell completely in love with Hooks.<br> Although I was skeptical at first glance, I quickly realized how easy it is to use them and how they make perfect sense.<br> You can really tell how much thought was put into the API and how it solves real life problems.</p><p>You can find a video of my talk attached to this post (Hebrew only… Sorry!).<br> In my talk I explained why we need Hooks, how they work, went over the core Hooks (useState, useEffect, useRef and useContext) and showed how to convert some of the class features into Hooks.<br> I also covered some other new features — memo, lazy and Suspense.</p><p>If you don’t speak Hebrew or just prefer reading over watching, I made a TL:DW (kind of a cheat sheet) on class features vs. Hooks. Enough intros… Let’s jump right to business!</p><h3>Class features vs. Hooks equivalents</h3><h4>State</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b6a7ae240de558a5ab7b5f23ab71fc7/href">https://medium.com/media/2b6a7ae240de558a5ab7b5f23ab71fc7/href</a></iframe><h4>ComponentDidMount</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8cd1b168274798d97bf8aa9b98b77d42/href">https://medium.com/media/8cd1b168274798d97bf8aa9b98b77d42/href</a></iframe><h4>ComponentWillUnmount</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3589aebc56d6e697511866ec79a5d248/href">https://medium.com/media/3589aebc56d6e697511866ec79a5d248/href</a></iframe><h4>ComponentWillReceiveProps \ ComponentDidUpdate</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d19db640bd56b76767b20c3b9ef18fa7/href">https://medium.com/media/d19db640bd56b76767b20c3b9ef18fa7/href</a></iframe><h4>DOM refs</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d2f4b0645f38d91d557138fae32a04b/href">https://medium.com/media/3d2f4b0645f38d91d557138fae32a04b/href</a></iframe><h4>this.myVar</h4><p>useRef has another cool usage besides DOM refs, it is also a generic container whose current property is mutable and can hold any value, similar to an instance property on a class.<br> Handy, for example, to keep an interval id:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8db65d1edbefaeaa4330bd3f12bba104/href">https://medium.com/media/8db65d1edbefaeaa4330bd3f12bba104/href</a></iframe><h4>Comparing with the previous state\props</h4><p>Some lifecycle method, like componentDidUpdate, provide the previous state and props.<br> If you really need the previous values for your Hooks, this can be imitated the following way (using yet again our good friend — useRef):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2fe77a467281f6fe793c22245e6fa6be/href">https://medium.com/media/2fe77a467281f6fe793c22245e6fa6be/href</a></iframe><h4>ShouldComponentUpdate</h4><p>We gonna use memo for this one, while this is not a Hook, it’s still part of the class-to-functional-component migration plan:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/feb26958467d4bad5281ac0ea685355e/href">https://medium.com/media/feb26958467d4bad5281ac0ea685355e/href</a></iframe><p>And that is React Hooks in a nutshell. I hope you will find this helpful when writing your next functional component.</p><p>Needless to say (but I’ll say it anyway), there are a whole lot more interesting and cool ways to use Hooks. I urge you to read the <a href="https://reactjs.org/docs/hooks-overview.html">awesome overview</a> the React team posted. Don’t miss out the <a href="https://reactjs.org/docs/hooks-faq.html">FAQ section</a>!</p><p>Last but not least, here’s me in my meetup video about React Hooks.</p><p>Watch, Like, Subscribe 😉</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FwltKai0a8VM%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DwltKai0a8VM&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FwltKai0a8VM%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/7712b9117b3298b23f8a0ec02d304a27/href">https://medium.com/media/7712b9117b3298b23f8a0ec02d304a27/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=745368dafdb3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/soluto-engineering/react-class-features-vs-hooks-equivalents-745368dafdb3">React Class features vs. Hooks equivalents</a> was originally published in <a href="https://medium.com/soluto-engineering">Soluto by asurion</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Size Matters]]></title>
            <link>https://medium.com/soluto-engineering/size-matters-5aeeb462900a?source=rss-69c2729ebf4b------2</link>
            <guid isPermaLink="false">https://medium.com/p/5aeeb462900a</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Nir Hadassi]]></dc:creator>
            <pubDate>Mon, 24 Jul 2017 11:57:22 GMT</pubDate>
            <atom:updated>2019-12-22T07:57:17.932Z</atom:updated>
            <content:encoded><![CDATA[<h3>Size Matters: How I used React Native to make my App look great on every device</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bDUGtEHrqomkuxvkSk_KfA.png" /></figure><blockquote>Every example on this post can be found <a href="https://github.com/nirsky/react-native-size-matters">here</a>.</blockquote><p>Have you ever had your designer hand you a cool design for your React Native app that you developed on, say, an iPhone 7 - and when you try to run it on a tablet, it looks like it was left in the dryer for too long?</p><p>That’s probably because the design was created using pixels whereas all dimensions in React Native are unitless, represented by ‘dp’ (density-independent pixels).</p><p>Simply put — the bigger your device is, the more ‘dp’ it’ll have.</p><p>When working with React Native, the iPhone 7 has a <strong>375dp</strong> width and <strong>667dp</strong> height and a Galaxy Tab A 8.0&quot; Tablet <em>(the one I’m using)</em> has a <strong>768dp</strong> width and <strong>1024dp</strong> height.</p><p>So while a &lt;View style={{width: 300, height: 450}}/&gt; will cover most of your iPhone&#39;s screen, it will cover less than half of your tablet&#39;s screen.</p><h3>So how can I make my app beautiful on the tablet as well?</h3><p>Glad you asked. In this blog post, I’ll show several methods for scaling your components to different screen sizes, and which one I found to work best.</p><p>To do this, I created a small example app, and after every scaling method I’ll attach the code along with screenshots for both a tablet and an iPhone.</p><h3>How it looks without scaling</h3><p>So this is the component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b754c7b2c938c3e775f803e9cf3ad75/href">https://medium.com/media/5b754c7b2c938c3e775f803e9cf3ad75/href</a></iframe><p>This is the stylesheet:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23211e2eec9419712e6405621147cb00/href">https://medium.com/media/23211e2eec9419712e6405621147cb00/href</a></iframe><p>As you can see, all my stylesheet sizes are in dp units and no scaling was done.</p><p>It will end up looking like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/669/1*WRp85Ra3y9Y4itFyb2c-ng.jpeg" /><figcaption>iPhone 7 vs. Galaxy Tab A 8.0&quot; Tablet — No scaling</figcaption></figure><p>That’s definitely not how we want our component to look like on a tablet <em>(did I say dryer already?)</em>.</p><h3>Method 1: Flex</h3><p>If you’re not familiar with flex I urge you to read about it. For starters check this <a href="https://codepen.io/enxaneta/full/adLPwv">flex playground</a> or read about it on the <a href="https://facebook.github.io/react-native/docs/flexbox.html">RN Docs</a>.</p><p>When developing a scalable component with flex you need to convert your view’s size <strong>and its margins</strong> with proportion to the parent component. If for example your container’s width is 375 and your box’s width is 300 — the box’s width is 80% of the parent (300/375) and the margins are what left — 10% on the left and 10% on the right.</p><p>Alternatively, you can keep your margins static (represented by dp) and spread it across the available space using flex: 1.</p><p>Here’s an example of how I <em>flexed</em> my component. I only flexed the white box and skipped flexing the buttons because I’m lazy, but you get the point (Stylesheet stayed the same except width and height were removed from box and container):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/618c933c98d383fd29523acb347e4033/href">https://medium.com/media/618c933c98d383fd29523acb347e4033/href</a></iframe><p>And the result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/669/1*a6nxOQk2DSYhQTbhhBYrzw.jpeg" /><figcaption>iPhone 7 vs. Galaxy Tab A 8.0&quot; Tablet — Flex</figcaption></figure><p>Flex is your best friend when creating a scalable <strong>layout</strong>, especially when wanting to spread it across the entire width or height (i.e. a list item) or when dividing a component to different sections with defined ratio. It will keep the same proportions among different devices, even when changing orientation.</p><p>While flex is an amazing tool for scaling, it’s not always enough. With flex, some components won’t scale easily and it’s limited to certain properties like width, height, margin and padding. Stuff like fontSize, lineHeight or SVG size can’t be flexed.</p><p>With that said, let’s continue to our second method.</p><h3>Method 2: Viewport Units</h3><p>With this method you basically convert every number you’d like to scale in your StyleSheet to a percentage of the device’s width or height.</p><p>If your device’s width is 375dp then 300dp will become deviceWidth * 0.8 (since 300/375 = 0.8), and you can also do it with smaller numbers, for example fontSize: 14 will become fontSize: deviceWidth * 0.037.</p><p>A nice and straight-forward library that can simplify this method is <a href="https://github.com/jmstout/react-native-viewport-units">react-native-viewport-units</a>.</p><p>This is the stylesheet after <em>viewporting</em> stuff around (Irrelevant parts were removed, component is exactly the same as the first example):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89aa6b5247cf471f28daf155530686b3/href">https://medium.com/media/89aa6b5247cf471f28daf155530686b3/href</a></iframe><p>And of course, what you have all been waiting for… the result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/669/1*itBLQoc_ULRQ2s9_lyVISQ.jpeg" /><figcaption>iPhone 7 vs. Galaxy Tab A 8.0&quot; Tablet — Viewport Units</figcaption></figure><p><em>Note: You’ll be able to achieve more or less the same result using the new </em><a href="https://github.com/facebook/react-native/commit/3f49e743bea730907066677c7cbfbb1260677d11"><em>percentage support</em></a><em> (RN 0.42 and up) or by multiplying everything with </em><em>PixelRatio.get().</em></p><p>Besides needing to do some calculation and having weird numbers around, pretty neat and easy, right?</p><p>…but still not perfect. What if you show your designer how it looks on the tablet and he thinks the buttons are too big and the box’s width should be reduced. What can you do? If you reduce the viewports it will affect the iPhone as well.</p><p>One option is to do something like HTML’s media-query using <a href="https://facebook.github.io/react-native/docs/pixelratio.html">PixelRatio</a>. But as I said, I&#39;m lazy and I don&#39;t want to write everything 2 or more times, what can I do?</p><h3>Method 3: Scaling Utils</h3><p>Here at <a href="https://www.solutotlv.com/">Soluto</a>, we wrote these 3 simple functions that make our scaling so much easier:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/beb3423996096dd26f4ba970d9c28c62/href">https://medium.com/media/beb3423996096dd26f4ba970d9c28c62/href</a></iframe><p>The purpose of these functions is to be able to take one design (from a standard mobile phone) and apply it to other display sizes.</p><p>scale function is pretty straight forward and will return the same linear result as using viewport.</p><p>verticalScale is like scale, but based on height instead of width, which can be useful.</p><p>The real magic happens at moderateScale. The cool thing about it is that you can control the resize factor (default is 0.5).</p><p>If normal scale will increase your size by +2X, moderateScale will only increase it by +X. Or if the resize factor is 0.25, instead of increasing by +2X it will increase by +0.5X.</p><p>If you want to scale a View with 300dp width, on the iPhone 7 you will get:</p><ul><li>scale(300) = 320</li><li>moderateScale(300) = 310</li><li>moderateScale(300, 0.25) = 305</li></ul><p>On the Galaxy Tab Tablet:</p><ul><li>scale(300) = 300 + 360 = 660</li><li>moderateScale(300) = 300 + 360/2 = 480</li><li>moderateScale(300, 0.25) = 300 + 360/4 = 390</li></ul><p>This allows us to write only once, keeping stuff roughly the same size across mobile phones without looking massive and bulky on tablets.</p><p>Anyways, enough talking. Here are the results after applying scaling utils on the original dp sizes until your designer is pleased:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad8d250e7b2a67cd3ba94f712c59adcf/href">https://medium.com/media/ad8d250e7b2a67cd3ba94f712c59adcf/href</a></iframe><p>Result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/669/1*EOi6WAEuXaFbF5Ec4jp-Jw.jpeg" /><figcaption>iPhone 7 vs. Galaxy Tab A 8.0&quot; Tablet — Scaling Utils</figcaption></figure><p>As mentioned, the iPhone keeps its proportions and the tablet gets a nice, fitted feel.</p><p><em>You can easily add the scaling utils to your repo using </em><a href="https://github.com/nirsky/react-native-size-matters"><em>my library</em></a><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZrJKJqBsksWd-8uKM9OvgA.png" /></figure><p>To sum up, there are many different ways to scale your component, what I found to work best for me was creating the layout with flex when possible, and scaling all other parts like margins, buttons, texts and SVGs using the scaling utils.</p><p>What I didn’t cover is scaling images and handling orientation change. We’ll keep that for a different post.</p><p>I hope you found this post useful. Scaling is super important, even if your app is not for tablets. Friends don’t let friends skip scaling!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5aeeb462900a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/soluto-engineering/size-matters-5aeeb462900a">Size Matters</a> was originally published in <a href="https://medium.com/soluto-engineering">Soluto by asurion</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>