<?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 Mukul Jain on Medium]]></title>
        <description><![CDATA[Stories by Mukul Jain on Medium]]></description>
        <link>https://medium.com/@mukuljainx?source=rss-2681293830e6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*3JK6jizWvzrhkCywggbTEQ.png</url>
            <title>Stories by Mukul Jain on Medium</title>
            <link>https://medium.com/@mukuljainx?source=rss-2681293830e6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 10:28:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mukuljainx/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[How to Create Scalable Frontend architecture]]></title>
            <link>https://medium.com/@mukuljainx/how-to-create-scalable-frontend-architecture-e05287cd9eda?source=rss-2681293830e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/e05287cd9eda</guid>
            <category><![CDATA[domain-driven-design]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[scalable]]></category>
            <category><![CDATA[architechture]]></category>
            <dc:creator><![CDATA[Mukul Jain]]></dc:creator>
            <pubDate>Mon, 08 Mar 2021 17:45:17 GMT</pubDate>
            <atom:updated>2021-03-08T17:45:17.782Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WEvRwDZPUiHSb4JFsGVVbg.png" /></figure><p>The architecture always felt like a heavy word, involving a lot of services and Databases, but it exists everywhere the way we arrange components and service in an app is the app’s architecture.</p><p>We all have been there when setting up a repo apart from choosing a bundler like webpack or fusebox what should be the directory structure? where services should go, should I keep components and API calls together or in a separate folder? This might sound silly but has a long-lasting effect on development and the way we define things because this is the architecture of that app that might belong to a far bigger ecosystem or it might be the ecosystem.</p><p>For small apps, anything will work but problems start when the app grows, especially when you are working in an ecosystem. I work as a senior UI developer at <a href="https://innovaccer.com/">InnovAccer</a> and here we have a bunch of UI apps coupled together in a shell and work flawlessly as a single page app, we call it <strong>Engine, </strong>you can read more about it <a href="https://dev.to/aregee/breaking-down-the-last-monolith-micro-frontends-hd4">here</a>.</p><p>Now one of them was growing at a fast pace and two teams were working on it, from releasing the new version and maintaining the codebase, we were dependent on each other and we decided it’s not working anymore and we decoupled the app into two relatively smaller apps.</p><p>We followed a flat structure ( never liked it though 🤷🏿‍♂️) and while decoupling I was going through every part of the app, I realized how much easier it would have been if we would have followed domain-driven design principles or domain-driven structure not only to decouple but also maintaining it and using some component inside other that would have been easier. What are DDD and flat structures? Let’s start with the flat structure.</p><p><strong>Flat Structure</strong></p><p>Now it doesn’t mean each file should be at the root level, that would be the worse structure to follow even for small apps. It means having a folder for each kind of functionality.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*zPnpmTGbwEszLr0HSatW7w.png" /></figure><p>Like here they have divided the functionality or behaviour they pose but not by how &amp; where they are used.</p><p>Now this structure is not bad at all and you must have seen this at many tutorials and it sort of makes sense for small projects until it stays small but when the app grows it should be migrated to Domain-Driven Structure, do it even before it becomes this big and you know it is going to be, but why?</p><h4>Domain-Driven Structure</h4><p>What is domain-driven design architecture, we have to read the whole book to fully understand it or maybe this <a href="https://www.infoq.com/minibooks/domain-driven-design-quickly/">quick book</a>, loosely in layman’s term it’s the plan of your architecture around the domain or problem, it emphasises placing the primary focus of a project on the core area of the business (the core domain). Let say we are developing a social media site similar to Facebook.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/955/1*S7uTEeTp9V7mtNACvynbzg.png" /></figure><blockquote>A domain-Driven Structure is a collection of flat structures, similar to a large app which is nothing but a collection of a number of small apps.</blockquote><p>Some folder may have helpers/reducers some may not it depends on the use case, helpers might also exist at the global level if there any common helpers like formateDate , you might also need a store (reducer) at the global level which will be stitching together all reducers.</p><p><a href="https://www.djangoproject.com/">Django Project</a> follows the same structure by default, there we have separate apps which have views, models etc.</p><h4><strong>Why it’s better</strong></h4><p><strong>Driven by the domain:</strong> Each subfolder contains what that particular sub-domain demands</p><p><strong>Truly Modular:</strong> You can plug &amp; play where ever want to, move it to another project or break this project into a small one, though obviously, you will have to set up a mechanism where they share data and pass on events, like routing and hot patching the reducers (<a href="https://shanebdavis.medium.com/modular-redux-a-design-pattern-for-mastering-scalable-shared-state-82d4abc0d7b3">https://shanebdavis.medium.com/modular-redux-a-design-pattern-for-mastering-scalable-shared-state-82d4abc0d7b3</a>).</p><p><strong>Independent: </strong>Different service/packages can be used in different submodule though they might increase bundle size or one can have a different config for the same services.</p><p><strong>Easier Imports:</strong> With or without the Webpack alias ( + TypeScripts base) we need to import from sibling folder instead of root. You can add each submodule as a module option in webpack too.</p><p><strong>Chunking:</strong> All the dependent code is inside the single folder and can be shipped as a chunk.</p><p>There will be cases when you need to show one component inside another, let say you need to show comments component inside post, you must export a component from comment to do so then rest should be handled by the comment component because you plugged this component there, data flow can be maintained using callbacks (props) or events.</p><h4><strong>Perfect Architecture?</strong></h4><p>No, I won’t say it’s perfect, it might have downfalls, one is too many folders, that’s why flat structure is better for a small project, but once it’s started growing switch to DDD before it’s too late.</p><p>So what&#39;s the catch, people are a little reluctant towards it because having an API folder in each module seems more troublesome than having one API folder at the root level, there are two reasons behind this feeling, one is familiarity as all tutorials have flat structure so it seems more familiar and easy to navigate, another is they haven’t realized the power of it.</p><h4><strong>Conclusion</strong></h4><p>At the end of the day, the structure should be easy to navigate and comfortable for your team, for a small project one can choose a flat structure, for a bigger project there is no harm in trying or migrating to DDD and choose which is the best for you.</p><p>Please let me know in the comments which one you and your team follow and what pros and cons you see in both patterns.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e05287cd9eda" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Smart Grid: Lightweight alternate to ag-grid]]></title>
            <link>https://medium.com/@mukuljainx/smart-grid-lightweight-alternate-to-ag-grid-9d8c3d38c351?source=rss-2681293830e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/9d8c3d38c351</guid>
            <category><![CDATA[ag-grid]]></category>
            <category><![CDATA[grid]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[web-components]]></category>
            <category><![CDATA[virtualization]]></category>
            <dc:creator><![CDATA[Mukul Jain]]></dc:creator>
            <pubDate>Sun, 08 Mar 2020 13:04:43 GMT</pubDate>
            <atom:updated>2020-03-13T04:20:46.946Z</atom:updated>
            <content:encoded><![CDATA[<h3>Smart Grid: How to create a virtualized grid with dynamic height support</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TC1MNWlp0XkgZoMB0j5WhQ.png" /></figure><blockquote>Live Demo: <a href="https://mukuljainx.github.io/smart-grid/">Smart Grid</a></blockquote><p>There are penality of React tables, everyone got its pros and cons but they all are pretty heavy, they all have almost the same features and few outstanding features and then there is <a href="https://www.ag-grid.com/">ag-grid</a> full of awesome feature but it is pretty heavy half of our vendor bundle was ag-grid ~ 200kb (gzipped 😱).</p><p>Okay so change the table, right? but there is no another package which provides the pinning feature so well, in most of the package, the tables scroll goes out of sync due to prioritization in JS. One fine Saturday it clicked me what if those two tables don’t have different vertical scroll but they are inside one container which is scrollable and they have different horizontal scrolls. Obviously the height of the tables will be the same, so scroll will always be in sync.</p><p>Something like this:</p><pre>&lt;div<br>  style={{<br>    height: 500,<br>    width: 700,<br>    border: &quot;1px solid&quot;,<br>    display: &quot;flex&quot;,<br>    overflow: &quot;auto&quot;<br>  }}<br>&gt;<br>  &lt;div<br>    style={{<br>      height: 2000,<br>      width: 400,<br>      border: &quot;1px solid red&quot;,<br>      flexShrink: 0<br>    }}<br>  /&gt;<br>  &lt;div<br>    style={{<br>      height: 2000,<br>      width: 800,<br>      border: &quot;1px solid blue&quot;,<br>      flexShrink: 0<br>    }}<br>  /&gt;<br>&lt;/div&gt;;</pre><p>If you will run this code you will both <strong>div</strong>’s are in sync vertically but the whole container is scrolling horizontally, but we want our unpinned table, i.e. right one for now to scroll, how to solve that? Simply add the right table inside a div with `overflowX:auto`, something like:</p><pre>&lt;div<br>  style={{<br>    height: 500,<br>    width: 700,<br>    border: &quot;1px solid&quot;,<br>    display: &quot;flex&quot;,<br>    overflow: &quot;auto&quot;<br>  }}<br>&gt;<br>  &lt;div<br>    style={{<br>      height: 2000,<br>      width: 400,<br>      border: &quot;1px solid red&quot;,<br>      flexShrink: 0<br>    }}<br>  /&gt;<br>  &lt;div style={{ overflowX: &quot;auto&quot; }}&gt;<br>    &lt;div<br>      style={{<br>        height: 2000,<br>        width: 800,<br>        border: &quot;1px solid blue&quot;,<br>        flexShrink: 0<br>      }}<br>    /&gt;<br>  &lt;/div&gt;<br>&lt;/div&gt;;</pre><h4>Working Demo</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fsad-kilby-0y4pl%3Ffontsize%3D14%26hidenavigation%3D1%26theme%3Ddark&amp;display_name=CodeSandbox&amp;url=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fsad-kilby-0y4pl%3Ffontsize%3D14%26hidenavigation%3D1%26theme%3Ddark&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2F0y4pl%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/3939277bcbdf893862916e63786728a8/href">https://medium.com/media/3939277bcbdf893862916e63786728a8/href</a></iframe><p>Major Problem Solved 🎉 then I inspected ag-grid demo page to check what they do and voila, they are doing almost the same thing.</p><p>Cool! but what about virtualization?</p><h4>Virtualization</h4><p>I am very lazy and hate reinventing the wheel, so I searched for packages which provide virtualization and found some, so I tried using them with the above code, the result was something like this, refer to table with <strong>virtual table</strong> heading. The package I used is <strong>react-tiny-virtual-list </strong>which obviously is not at fault, it is doing what it is supposed to.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fj1o8u&amp;display_name=CodeSandbox&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fj1o8u&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fj1o8u%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/13446a879c80c4c247406d66b12704b1/href">https://medium.com/media/13446a879c80c4c247406d66b12704b1/href</a></iframe><p>There is one more table there, below that one is also virtualized else tab will be hanged with large data, so how I achieved it, by calculating current position using `<strong>scrollOffset` </strong>then calculating a start &amp; end position something like</p><pre>const start = Math.max(position-buffer-visibleCount, 0);<br>const end = Math.min(position+buffer+visibleCount, data.length-1);</pre><p>Then just rendering just those rows, It was not expected to go fine in one go as I didn’t think virtualization was this simple, but that is what it is! 🙅‍♂️</p><p>I also tried using fast-memoize to memoize rows so they won’t be recalculated thus saving some work which results in smooth scrolling experience but page started crashing for the huge data set due to increased JS heap size.</p><p>I went through react-virtualized to what they have done and I found very amazing thing there, cache while scroll and clear it when scroll stops. Create a local cache always look for grid row in the cache first if not create row &amp; cache it. Assign an empty object to the cache when the user stops scrolling.</p><h4>Dynamic Height</h4><p>Dynamic height is also supported in version v0.1.0 , this is achieved by rendering rows twice once for getting the height of the row at some fixed position and the second time with calculated height &amp; positions, these values are cached so when the user scrolls to the previously viewed rows they are only rendered once as we already have their height and position stored in cache.</p><p>When data changes for any row, recalculation may be needed but this has been left user by providing gridActions as the user can predict when the height may change and the cache will not be cleared every time the data changes. You view the live demo at <a href="https://mukuljainx.github.io/smart-grid/">https://mukuljainx.github.io/smart-grid/</a></p><p>That’s how <a href="https://mukuljainx.github.io/smart-grid/">smart-grid</a> came to existence, it’s an open-source project please open a PR or issue if needed on <a href="https://github.com/mukuljainx/smart-grid">Github</a>. 😇</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9d8c3d38c351" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Theming App with Styled Components]]></title>
            <link>https://medium.com/@mukuljainx/theming-app-with-styled-components-856d14b7110a?source=rss-2681293830e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/856d14b7110a</guid>
            <category><![CDATA[themes]]></category>
            <category><![CDATA[styled-components]]></category>
            <category><![CDATA[web-app-development]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Mukul Jain]]></dc:creator>
            <pubDate>Sun, 08 Dec 2019 10:29:33 GMT</pubDate>
            <atom:updated>2019-12-08T10:29:33.807Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SWQFmpdb-utrRdkI0grz3Q.png" /></figure><p>You must have seen “Dark Theme” in many apps nowadays some app even support the totally customizable theme, but how theming works and how it changes on the go? Well, it’s pretty simple with styled-components, most of the steps will be same for any library.</p><h3>Maintain theme at single place</h3><p>Never refer colours, font-weight and font-size in your app directly always use a central place to keep theme-related info, it can be redux too.</p><p>It will look something like</p><pre>const theme = {<br>  colors: {<br>    primary: &#39;#0043E8&#39;,<br>    secondary: &#39;#F45D01&#39;,<br>    alert: &#39;#F73F52&#39;,<br>    success: &#39;#1BAA53&#39;<br>  }<br>  fontSize: {<br>    body: 14<br>    ..<br>  }<br>}</pre><p>Half the work is done</p><h3>Theme Provider</h3><p>With <a href="https://www.styled-components.com/">styled-components</a>, we have an API <a href="https://www.styled-components.com/docs/api#themeprovider"><strong>ThemeProvider</strong></a></p><blockquote>A helper component for theming. Injects the theme into all styled components anywhere beneath it in the component tree, via the context API. Check the section on Theming.</blockquote><p>This is how it is used</p><pre>&lt;ThemeProvider theme={theme}&gt;<br>  &lt;App /&gt;<br>&lt;/ThemeProvider</pre><p>Here `theme` is just an object which will be available in each styled-component in `props` can be accessed using `props.theme`</p><p>for example</p><pre>const Alert = styled.p`<br>  color: ${({props}) =&gt; props.theme.colors.alert};<br>`;</pre><h3>Theming</h3><p>Just as we created `theme` variable above we have to create a `darkTheme`, it should be same as `theme` structure-wise or if you are using <strong>TypeScript </strong>then there interface should be same, some values can be optional them we have to merge both before using.</p><pre>const defaultTheme = {<br>  colors: {<br>    primary: &#39;#0043E8&#39;,<br>    secondary: &#39;#F45D01&#39;,<br>    alert: &#39;#F73F52&#39;,<br>    success: &#39;#1BAA53&#39;<br>  }<br>  fontSize: {<br>    body: 14<br>    ..<br>  }<br>}</pre><pre>const darkTheme = {<br>  colors: {<br>    primary: &#39;black&#39;,<br>    secondary: &#39;white&#39;,<br>    alert: &#39;#F73F52&#39;,<br>    success: &#39;#1BAA53&#39;<br>  }<br>  fontSize: defaultTheme.fontSize<br>}</pre><p>While passing theme to ThemeProvider</p><pre>&lt;ThemeProvider theme={this.props.theme.value === &#39;dark&#39; ? darkTheme : defaultTheme}&gt;<br>  &lt;App /&gt;<br>&lt;/ThemeProvider</pre><p>In the above example, the component is connected to redux so we can get theme from redux and on change by the user, it will be reflected instantly.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=856d14b7110a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setup a React Component Library using Create React App, React Styleguidist and TypeScript]]></title>
            <link>https://medium.com/@mukuljainx/setup-a-react-component-library-using-create-react-app-react-styleguidist-and-typescript-b24608cb097e?source=rss-2681293830e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/b24608cb097e</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[styleguidist]]></category>
            <category><![CDATA[components]]></category>
            <category><![CDATA[libraries]]></category>
            <dc:creator><![CDATA[Mukul Jain]]></dc:creator>
            <pubDate>Sun, 02 Jun 2019 11:23:17 GMT</pubDate>
            <atom:updated>2019-06-05T18:12:32.648Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s-7nAU69dN7f0zsMYk3SoA.jpeg" /></figure><p>I was working on a personal project, where I developed a few components which I thought are worth sharing, but how? I can publish them on NPM, but the user should be able to tinker with them no just see the default examples, that’s when I found out <a href="https://react-styleguidist.js.org/">React Styleguidist</a>, I struggled around for 1 day to get this for the first time and now it takes around 10 minutes with styled-components setup.</p><p>We will use React Styleguidist, as not only one can interact with the components, one can also change the props and write a custom function to see how the component behaves.</p><p>React Styleguidist does not provide boilerplate like <a href="https://facebook.github.io/create-react-app/docs/getting-started">Create React App</a> but its a plugin and can be integrated easily with CRA or to any React App with webpack, I have divided steps into 3 parts.</p><ol><li>Create React App with <a href="https://www.typescriptlang.org/">TypeScript</a></li><li>Add React Styleguidist</li><li>Add TypeScript Support</li></ol><h3>Create React App</h3><p>Create a React setup using CRA using npx for the latest version or yarn create</p><pre>// Using yarn<br>yarn create react-app my-component-library --typescript</pre><pre>// Using npm<br>npx create-react-app my-app --typescript</pre><pre>cd my-component-library</pre><pre>// Make sure App is running<br>yarn start</pre><h3>Add React Styleguidist</h3><p>Let’s React Styleguidist to our project using</p><p>yarn add -D react-styleguidist</p><p>As we are going to use this project as a component library will never use the default start and build script. So we will replace them with <strong>styleguidist </strong>scripts, it should look like this</p><pre>&quot;start&quot;: &quot;npx styleguidist server&quot;,<br>&quot;build&quot;: &quot;npx styleguidist build&quot;,<br>&quot;test&quot;: &quot;react-scripts test&quot;,<br>&quot;eject&quot;: &quot;react-scripts eject&quot;</pre><p>Let’s start our server using yarn start , the following page will show up.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WP5aEz4KkRO04wRBVKl34A.png" /></figure><p>This is because we haven’t told the styleguidist where our components are, we even don’t have any component yet.</p><p>Let&#39;s add a simple button component in src/components/button folder.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%28171%252C%2520184%252C%2520195%252C%25201%29%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D1x%26wm%3Dfalse%26code%3D%25252F%25252F%252520src%25252Fcomponents%25252Fbutton%25252FButton.tsx%25250A%25250Aimport%252520%2A%252520as%252520React%252520from%252520%252522react%252522%25253B%25250A%25250A%25250Aclass%252520Button%252520extends%252520React.PureComponent%252520%25257B%25250A%252520%252520render%28%29%252520%25257B%25250A%252520%252520%252520%252520return%252520%28%25250A%252520%252520%252520%252520%252520%252520%25253Cbutton%25250A%252520%252520%252520%252520%252520%252520%252520%252520style%25253D%25257B%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520border%25253A%252520%252522none%252522%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520padding%25253A%252520%2525228px%25252012px%252522%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520background%25253A%252520%252522hotpink%252522%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520borderRadius%25253A%252520%2525224px%252522%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520color%25253A%252520%252522white%252522%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25257D%25257D%25250A%252520%252520%252520%252520%252520%252520%25253E%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25257Bthis.props.children%25257D%25250A%252520%252520%252520%252520%252520%252520%25253C%25252Fbutton%25253E%25250A%252520%252520%252520%252520%29%25253B%25250A%252520%252520%25257D%25250A%25257D%25250A%25250Aexport%252520default%252520Button%25253B%25250A&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%28171%25252C%252520184%25252C%252520195%25252C%2525201%29%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D1x%26wm%3Dfalse%26code%3D%2525252F%2525252F%25252520src%2525252Fcomponents%2525252Fbutton%2525252FButton.tsx%2525250A%2525250Aimport%25252520%2A%25252520as%25252520React%25252520from%25252520%25252522react%25252522%2525253B%2525250A%2525250A%2525250Aclass%25252520Button%25252520extends%25252520React.PureComponent%25252520%2525257B%2525250A%25252520%25252520render%28%29%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520return%25252520%28%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525253Cbutton%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520style%2525253D%2525257B%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520border%2525253A%25252520%25252522none%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520padding%2525253A%25252520%252525228px%2525252012px%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520background%2525253A%25252520%25252522hotpink%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520borderRadius%2525253A%25252520%252525224px%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520color%2525253A%25252520%25252522white%25252522%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525257D%2525257D%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525257Bthis.props.children%2525257D%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525253C%2525252Fbutton%2525253E%2525250A%25252520%25252520%25252520%25252520%29%2525253B%2525250A%25252520%25252520%2525257D%2525250A%2525257D%2525250A%2525250Aexport%25252520default%25252520Button%2525253B%2525250A&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=now" width="1024" height="473" frameborder="0" scrolling="no"><a href="https://medium.com/media/c500a09f6160a77c67ac21b85e88fc44/href">https://medium.com/media/c500a09f6160a77c67ac21b85e88fc44/href</a></iframe><p>As we have created our component inside `src/components` it will be automatically detected but we surely can add our component anywhere we want and tell the styleguidist where they are, by adding styleguide.config.js to root folder of the app and write following code inside it</p><pre>module.exports = {<br>  components: &quot;path/to/your/components/**/*.tsx&quot;<br>};</pre><p>Start the server again using yarn start , note that we have made a change to styleguidist configuration for that to reflect we have to restart the server.</p><p>It should look something like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p2ovHDgJYwZrmmIj8teoNw.png" /></figure><p>To our component show up there we have to create a README.md inside the button folder, like this, every component will have it’s own README.md file where you can show a various example with different props.</p><pre>// src/components/Button/README.md</pre><pre>```js<br>&lt;Button&gt;Button&lt;/Button&gt;</pre><pre>```</pre><p>Talking of different props lets add a prop to our Button</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%28171%252C%2520184%252C%2520195%252C%25201%29%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%252F%252F%2520src%252Fcomponents%252Fbutton%252FButton.tsx%250A%250Aimport%2520%2A%2520as%2520React%2520from%2520%2522react%2522%253B%250A%250Ainterface%2520IProps%2520%257B%250A%2520%2520%252F%2A%2A%250A%2520%2520%2520%2A%2520Size%2520of%2520the%2520button%250A%2520%2520%2520%2A%252F%250A%2520%2520size%253A%2520%2522regular%2522%2520%257C%2520%2522large%2522%253B%250A%257D%250A%250Aclass%2520Button%2520extends%2520React.PureComponent%253CIProps%253E%2520%257B%250A%2520%2520render%28%29%2520%257B%250A%2520%2520%2520%2520const%2520%257B%2520size%252C%2520children%252C%2520...rest%2520%257D%2520%253D%2520this.props%253B%250A%2520%2520%2520%2520return%2520%28%250A%2520%2520%2520%2520%2520%2520%253Cbutton%250A%2520%2520%2520%2520%2520%2520%2520%2520%257B...rest%257D%250A%2520%2520%2520%2520%2520%2520%2520%2520style%253D%257B%257B%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520border%253A%2520%2522none%2522%252C%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520padding%253A%2520size%2520%253D%253D%253D%2520%2522regular%2522%2520%253F%2520%25228px%252012px%2522%2520%253A%2520%252212px%252016px%2522%252C%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520background%253A%2520%2522hotpink%2522%252C%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520borderRadius%253A%2520%25224px%2522%252C%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520color%253A%2520%2522white%2522%250A%2520%2520%2520%2520%2520%2520%2520%2520%257D%257D%250A%2520%2520%2520%2520%2520%2520%253E%250A%2520%2520%2520%2520%2520%2520%2520%2520%257Bchildren%257D%250A%2520%2520%2520%2520%2520%2520%253C%252Fbutton%253E%250A%2520%2520%2520%2520%29%253B%250A%2520%2520%257D%250A%250A%2520%2520static%2520defaultProps%2520%253D%2520%257B%250A%2520%2520%2520%2520size%253A%2520%2522regular%2522%250A%2520%2520%257D%253B%250A%257D%250A%250Aexport%2520default%2520Button%253B%250A&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%28171%25252C%252520184%25252C%252520195%25252C%2525201%29%26t%3Dseti%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25252F%25252F%252520src%25252Fcomponents%25252Fbutton%25252FButton.tsx%25250A%25250Aimport%252520%2A%252520as%252520React%252520from%252520%252522react%252522%25253B%25250A%25250Ainterface%252520IProps%252520%25257B%25250A%252520%252520%25252F%2A%2A%25250A%252520%252520%252520%2A%252520Size%252520of%252520the%252520button%25250A%252520%252520%252520%2A%25252F%25250A%252520%252520size%25253A%252520%252522regular%252522%252520%25257C%252520%252522large%252522%25253B%25250A%25257D%25250A%25250Aclass%252520Button%252520extends%252520React.PureComponent%25253CIProps%25253E%252520%25257B%25250A%252520%252520render%28%29%252520%25257B%25250A%252520%252520%252520%252520const%252520%25257B%252520size%25252C%252520children%25252C%252520...rest%252520%25257D%252520%25253D%252520this.props%25253B%25250A%252520%252520%252520%252520return%252520%28%25250A%252520%252520%252520%252520%252520%252520%25253Cbutton%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25257B...rest%25257D%25250A%252520%252520%252520%252520%252520%252520%252520%252520style%25253D%25257B%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520border%25253A%252520%252522none%252522%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520padding%25253A%252520size%252520%25253D%25253D%25253D%252520%252522regular%252522%252520%25253F%252520%2525228px%25252012px%252522%252520%25253A%252520%25252212px%25252016px%252522%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520background%25253A%252520%252522hotpink%252522%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520borderRadius%25253A%252520%2525224px%252522%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520color%25253A%252520%252522white%252522%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25257D%25257D%25250A%252520%252520%252520%252520%252520%252520%25253E%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25257Bchildren%25257D%25250A%252520%252520%252520%252520%252520%252520%25253C%25252Fbutton%25253E%25250A%252520%252520%252520%252520%29%25253B%25250A%252520%252520%25257D%25250A%25250A%252520%252520static%252520defaultProps%252520%25253D%252520%25257B%25250A%252520%252520%252520%252520size%25253A%252520%252522regular%252522%25250A%252520%252520%25257D%25253B%25250A%25257D%25250A%25250Aexport%252520default%252520Button%25253B%25250A&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=now" width="1024" height="473" frameborder="0" scrolling="no"><a href="https://medium.com/media/0fabe52c0bd2044afd469fc5bcace780/href">https://medium.com/media/0fabe52c0bd2044afd469fc5bcace780/href</a></iframe><pre>// src/components/Button/README.md</pre><pre>```js<br>&lt;Button&gt;Button&lt;/Button&gt;<br>&lt;Button size=&quot;large&quot;&gt;Button&lt;/Button&gt;</pre><pre>```</pre><p>The Above code and README.md will result into</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nno_PNz41FxndBdkAcK2hg.png" /></figure><p>Props &amp; Method section has been automatically added to our component description but the type is unknown, which should not be the case as we are using TypeScript and it should show ”regular” | “large” , React Styleguidist doesn’t support TypeScript by default it read props from PropType here it is using defaultProps to show the above, we need to add a parser which will parse props defined in typescript.</p><h3>TypeScript Support</h3><p>Add react-docgen-typescript using</p><p>yarn add -D react-docgen-typescript</p><p>Add parser tostyleguide.config.js</p><pre>module.exports = {<br>  propsParser: propsParser: require(&quot;react-docgen-typescript&quot;).parse<br>};</pre><p>You can also add descriptions to IProps using JSDoc just above the prop, it will be shown in the description section</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AQ9yF6kwaXadXb8CPf5SIQ.png" /></figure><p>High five 🙌 We have a component library, now we can add an individual package.json file to every component and publish them on npm.</p><p>I have pushed the above setup on <a href="https://github.com/paper-x/crafts/tree/1471778403300a4d4ad35db8073e2914c0fa1452">GitHub</a>, you can check out for reference or use it directly.</p><p>Thank you for Reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b24608cb097e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript under the Hood]]></title>
            <link>https://medium.com/@mukuljainx/javascript-under-the-hood-c9c1e7d31225?source=rss-2681293830e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/c9c1e7d31225</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Mukul Jain]]></dc:creator>
            <pubDate>Tue, 23 May 2017 08:01:36 GMT</pubDate>
            <atom:updated>2017-05-23T08:01:36.240Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is JavaScript</h3><p>Let’s talk about one of the most famous languages today, JavaScript, one of three basic component (HTML, CSS &amp; JS) of the web. Wikipedia says ( For understanding purpose I will breakdown definition in few parts)</p><blockquote>JavaScript is prototype-based with first-class functions</blockquote><p>To understand this, first understand what is <strong>Prototypal inheritance</strong>, In the prototypal inheritance form, objects inherit directly from other objects. In JS every object has a secret link to its parent object, thus whenever an object is asked for any property, it looks for it, if it does not have its parent will be asked then its parent, process will be continued till property is find or it will give an error.</p><p>For example</p><blockquote>function Circle(radius) {<br> this.radius = radius;<br>}</blockquote><blockquote>Circle.prototype.area = function () {<br> var radius = this.radius;<br> return Math.PI * radius * radius;<br>};</blockquote><blockquote>Circle.prototype.circumference = function () { <br> return 2 * Math.PI * this.radius;<br>};</blockquote><blockquote>var circle = new Circle(5);<br>var circle2 = new Circle(10);</blockquote><blockquote>circle.area() // 78.53981633974483<br>circle.diameter() // Uncaught TypeError: circle2.diameter is not a function</blockquote><p>Here <em>radius</em> is a <em>Number</em> which also inherits properties from ```Number``` thats why it outputs <em>5</em> on <em>circle.radius.toString()</em></p><p>By <strong>first-class functions</strong>, it meant that function can be stored in variables and then can be passed around.</p><p>It also supports <strong>High Order Functions</strong>, it means function can take other function as input and return a function as output.</p><blockquote>Making it a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles.</blockquote><p><strong>Multi-paradigm</strong> means, you can program in with various coding styles like OOP or Functional or mix of both.</p><blockquote>It has an API for working with text, arrays, dates and regular expressions, but does not include any I/O, such as networking, storage, or graphics facilities, relying for these upon the host environment in which it is embedded.</blockquote><p>JS have API for text, arrays etc, this mean JS can count elements in an array or can pop something out from it but it can’t perform an API call itself, yes you read it right JS Engine doesn’t do it, browser does.</p><h3>How JavaScript Works</h3><p>Let’s see how things work under the hood.</p><p>Javascript is single threaded, it means it can execute one thing at a time, check the figure given below, whenever there some operation like <em>console.log</em> or <em>a + b</em> it pushes it into the stack operates it and pop it out.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x7NIs6d3BXnXkMXxRsgcqg.png" /></figure><p>If it is function call then it puts the function in the stack operates it, pops everything out, like shown below</p><blockquote>// taking this code as example<br>var add = function(a,b){<br> return a + b;<br>}</blockquote><blockquote>var x = 2;<br>var y = 7;</blockquote><blockquote>var z = sum(x,y);</blockquote><blockquote>console.log(z);</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*q-u2WyjkeUehm9SPxu_s-A.png" /></figure><p>Then how it is non-blocking? why not things like <em>setTimeout</em> or asynchronous call complete first blocking other code?</p><p>Whenever there is something like asynchronous call or timeout function, it comes stack then it passes to the browser which can do it multiple things together, after finishing the tasks browser puts callback queue, event loop keep looking at the stack and callback queue, when it finds the stack empty it puts first from queue to stack.</p><p>Let’s see a working example, for simplicity we use will ```console.log``` and time <em>setTimeout</em> only.</p><blockquote>setTimeout(function(){<br> console.log(“a”);<br>},1500)</blockquote><blockquote>console.log(“d”);</blockquote><blockquote>setTimeout(function(){<br> console.log(“b”);<br>},500)</blockquote><blockquote>setTimeout(function(){<br> console.log(“c”);<br>},1000)</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*b2HkRiEkJI5fGRBnlsQSOA.gif" /></figure><p>Thanks to Philip Roberts for <a href="https://www.youtube.com/watch?v=8aGhZQkoFbQ">wonderful explanation</a> on working of JS.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c9c1e7d31225" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>