<?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 Noam Elboim on Medium]]></title>
        <description><![CDATA[Stories by Noam Elboim on Medium]]></description>
        <link>https://medium.com/@noamel?source=rss-475014d94fbe------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*QfLlct0ZU3uDCU1mBP30gQ.png</url>
            <title>Stories by Noam Elboim on Medium</title>
            <link>https://medium.com/@noamel?source=rss-475014d94fbe------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 21:50:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@noamel/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 “Controllable” React components maximize reusability]]></title>
            <link>https://medium.com/myheritage-engineering/how-controllable-react-components-maximize-reusability-86e3d233fa8e?source=rss-475014d94fbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/86e3d233fa8e</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[components]]></category>
            <category><![CDATA[controllable]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Noam Elboim]]></dc:creator>
            <pubDate>Tue, 19 Dec 2017 14:14:25 GMT</pubDate>
            <atom:updated>2018-12-22T15:19:28.719Z</atom:updated>
            <content:encoded><![CDATA[<h4>A deep dive into what really makes React components reusable</h4><p>How many times have you tried using a component just to find out you could not manipulate its state?<br>How many times did the component just not work “out of the box”?</p><p>Stop pulling your hair out — we can do something about it!</p><p>In this article, I will show you how to increase your components reusability by managing your components data flow in a better way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qCq2qytmCtVsR5YvAY4J3Q.png" /><figcaption>Photo by <a href="https://unsplash.com/photos/7e2pe9wjL9M?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Riccardo Annandale</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>There are 3 different ways to manage your data flow:</p><ol><li>The <strong>Uncontrolled</strong> way, where the <strong>component manages its own data</strong>.</li><li>The <strong>Controlled </strong>way, where the <strong>parent manages the data</strong>.</li><li>The <strong>Controllable </strong>way, where you get <strong>the best of both worlds</strong>!</li></ol><p>Choosing the right way will have a tremendous effect on the component’s reusability.</p><p>FYI, all the examples are in React, but the discussion in this article is really about concepts pertaining to components data flow. Those concepts are easily applied to Vue.js or any other framework that support components.</p><h3>Introducing Tabs</h3><p>Our story is the story of <strong><em>Tabs</em></strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*RQz88mENCniABvt6Gs9ZSw.gif" /><figcaption>The Tabs component, holds the Banana and Apple tabs</figcaption></figure><p>Clicking on any tab will change the active one, indicated by a style and content change.</p><p>Let’s write a simple markup for it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc9a81dcdb50666c81b729758ae8a86f/href">https://medium.com/media/bc9a81dcdb50666c81b729758ae8a86f/href</a></iframe><p><strong>So, how do you decide which tab is active?</strong></p><h3>The Uncontrolled Way</h3><p>Uncontrolled is when the component manages its own data.</p><p>Let’s look on a simple input element:<br>&lt;input/&gt;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tqy__SF3F7aKMzUfTE0t-g.gif" /><figcaption>Typing in an input element</figcaption></figure><p>Typing text in the input will set the value of the element with that text. Changing value from the outside is possible only by a DOM manipulation, for example, by using React’s ref and accessing the element’s instance.</p><p>Applying the same approach to <strong><em>Tabs, </em></strong>we get this:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2F64q66mwjkr&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2F64q66mwjkr&amp;image=https%3A%2F%2Fcodesandbox.io%2Fstatic%2Fimg%2Fbanner.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/9ccea2751de6a013af86cc8da9df0d0f/href">https://medium.com/media/9ccea2751de6a013af86cc8da9df0d0f/href</a></iframe><h4>How does it work?</h4><p>The component has a state (<a href="https://medium.com/@juanguardado/stateful-vs-stateless-components-444b5aa21865"><em>stateful</em></a><em> component</em>), with avalue field in it.<br>To decide which child is active, we use the isSelected function, which compares the tab (one of the children) against this.state.value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3adb45798d1e578c56a76f5a83d65f7/href">https://medium.com/media/c3adb45798d1e578c56a76f5a83d65f7/href</a></iframe><p>The isSelected function runs in the render and determines which header is active right now and which tab content to show.</p><p>When the user clicks on one of the headers, we will trigger the selectTab function, which will change the state with the new value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8c8066af6291551c8f217282b337413/href">https://medium.com/media/d8c8066af6291551c8f217282b337413/href</a></iframe><p>We will even call a callback (this.props.onChange) to update the parent with changes to the inner state.</p><p>At this point, to change the active tab from the outside, you must use ref and change the <strong><em>Tabs</em></strong> instance directly.<br>Therefore, it is Uncontrolled.</p><h4>What Uncontrolled is good for?</h4><p>Uncontrolled is mostly useful when you do not need to manipulate the data from the outside. Like, ever.</p><p><strong>Uncontrolled Pros:</strong></p><ol><li>“Plug and Play” — using the component does not require any additional implementations. Usually almost no props and you get a lot in return.</li><li>Simple to read and understand from the parent side.</li></ol><p><strong>Uncontrolled Cons:</strong></p><ol><li>Hard to compose.</li><li>Does not allow advanced usages, like filtering certain values or mutating them.</li><li>Must be a <em>stateful </em>component, which adds more lines of code for overall bigger, heavier components.</li></ol><p>It is worth mentioning that Uncontrolled is usually the default pick for many developers. It is approachable, what you see in a lot of the React tutorials, and very easy to understand for beginners to React.</p><h3>The Controlled Way</h3><p>Controlled is when the parent manages the component data.</p><p>A Controlled input element may look something like this:<br>&lt;input value={valueFromParentState} onChange={this.onChange}/&gt;</p><p>Typing text in the input will not change the element value; it will trigger an onChange callback with the new value. The input on the screen will change only when thevalueFromParentState changes. Therefore we will change valueFromParentState by the onChange update.</p><p>To make <strong><em>Tabs </em></strong>Controlled, we will need to add the value prop and use the onChange callback.</p><p>Let’s build it:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fkrz1zzmq3&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fkrz1zzmq3&amp;image=https%3A%2F%2Fcodesandbox.io%2Fstatic%2Fimg%2Fbanner.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/6276648d3ed1bd3d0da7074134fa3857/href">https://medium.com/media/6276648d3ed1bd3d0da7074134fa3857/href</a></iframe><h4>How does it work?</h4><p>When this.props.value changes, it triggers a render cycle which changes the active tab. When the user clicks on one of the headers, we just execute the onChange callback with the new value. If the parent does not do anything with the callback, nothing happens in <strong><em>Tabs</em></strong> either.</p><p>To decide which tab is active, we compare each tab’s value to this.props.value in the isSelected function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd79e84b1974cec083966fd57dcff54e/href">https://medium.com/media/bd79e84b1974cec083966fd57dcff54e/href</a></iframe><p>When there is a click on one of the headers, we execute this.props.onChange with the new value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e606c57ca80781fb89f5a919e7a3288/href">https://medium.com/media/0e606c57ca80781fb89f5a919e7a3288/href</a></iframe><h4>What is Controlled good for?</h4><p>Controlled components are meant to be composed in other components and are most useful that way. A Controlled component cannot do much by itself, and is dependent on the parent to control it.</p><p><strong>Controlled Pros:</strong></p><ol><li>Allows easy composition.</li><li>Advanced usages. Open and welcoming to data manipulations.</li></ol><p><strong>Controlled Cons:</strong></p><ol><li>Cannot be used “as-is” — you must implement a state and callbacks to actually use it.</li><li>Props bloating. The more Controlled components you compose, the more props you will have to maintain in each level.</li></ol><h3>The Controllable Way</h3><p>What if you want the same component to be used as Uncontrolled in one place, and in another place as Controlled?</p><p>We just saw the same input element used both as Uncontrolled in the first example, and then as Controlled in the second. So it is indeed possible!</p><p>And a lot simpler than you may think.</p><p>We call it <strong>Controllable</strong>:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fw0mjk9z63k&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fw0mjk9z63k&amp;image=https%3A%2F%2Fcodesandbox.io%2Fstatic%2Fimg%2Fbanner.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/3008035b149b4b438f7eee8c22189eed/href">https://medium.com/media/3008035b149b4b438f7eee8c22189eed/href</a></iframe><h4>How does it work?</h4><p>We start from the Uncontrolled version of <strong><em>Tabs</em></strong>, and our only addition is this getDerivedStateFromProps implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3db8740d81b2be4988f70054ba42f7b5/href">https://medium.com/media/3db8740d81b2be4988f70054ba42f7b5/href</a></iframe><p>It binds any changes in props.value to also happen in state.value, which means if there was a change in the value prop — it would be applied to the state as well.</p><p>Note how we use prevValue to not change the state when any other prop is changing.</p><p>This mapping between prop and state is what allows the component to behave both Uncontrolled and Controlled. Though the component remains dependent on a state from the inside, we can trick it to take prop changes into account as well.</p><p>It even allows you to decide which way you want, at runtime. You may want to start as Controlled and change to Uncontrolled afterwards — all the options are available to you.</p><p>By the way, <a href="https://codepen.io/NoamELB/pen/yzMWZz?editors=0110">Controllable works very well in Vue.js with </a><a href="https://codepen.io/NoamELB/pen/yzMWZz?editors=0110">watch</a>.</p><h4>makeControllable</h4><p>Implementing the exact same getDerivedStateFromProps over and over again, to make Controllable components, can be very repetitive.</p><p>That is why, my brother <a href="https://medium.com/u/d8c4fbf20232">Tzook Shaked</a> and I created a short util function called <strong><em>makeControllable </em></strong>to help you out.</p><p>This will shorten your getDerivedStateFromProps implementations greatly.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c38195d82dcebe77c7cb05c65f3f0094/href">https://medium.com/media/c38195d82dcebe77c7cb05c65f3f0094/href</a></iframe><p>You can check the full code and readme here:<br><a href="https://github.com/NoamELB/make-controllable"><strong>https://github.com/NoamELB/make-controllable</strong></a></p><h4>What is Controllable good for?</h4><p>Controllable has all the pros of both Uncontrolled and Controlled, and, of course, it also lets you choose between them.</p><p>To make components more reusable, you need to keep more options open. That can be achieved by making Controllable components, or at least knowing about them as a possibility, to allow an easy upgrade to Controllable when someone needs it.</p><p>Maybe you need a component to be Uncontrolled now, but the next person to use it may need it to be Controlled. With such a small addition to your code, you can have both, as immediately as you need them. Amazing, right?</p><h3>Wrapping it up</h3><p>If you have too many Uncontrolled components in an app, you may find yourself dragged down by way too many refs.<br>If you have too many Controlled components in an app, you may find yourself managing tens of props in your high-level components.</p><p>I think there should be a balance in an app between the number of Controlled and Uncontrolled components that go into it. Achieving this balance is so much easier when you have components that can be used either way.</p><p>At MyHeritage, our ability to have Controllable components really changed the way we develop components. It opened our eyes to think more carefully about how the next person to use the component may need it, instead of just thinking about how it is needed right now.</p><p>Thank you for reading 😇</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=86e3d233fa8e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/myheritage-engineering/how-controllable-react-components-maximize-reusability-86e3d233fa8e">How “Controllable” React components maximize reusability</a> was originally published in <a href="https://medium.com/myheritage-engineering">MyHeritage Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to greatly improve your React app performance]]></title>
            <link>https://medium.com/myheritage-engineering/how-to-greatly-improve-your-react-app-performance-e70f7cbbb5f6?source=rss-475014d94fbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/e70f7cbbb5f6</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[components]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[dom]]></category>
            <category><![CDATA[shouldcomponentupdate]]></category>
            <dc:creator><![CDATA[Noam Elboim]]></dc:creator>
            <pubDate>Tue, 21 Nov 2017 14:18:19 GMT</pubDate>
            <atom:updated>2019-06-02T11:46:45.083Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>A review of common React performance pitfalls and how to avoid them</strong></h4><p>Performance problems in web apps are not new.<br>Everyone knows that moment when you take a new component, add it to your app — and suddenly every single user interaction you attempt has a noticeable performance lag! Sometimes, you can even use the same component multiple times and get an embarrassing animation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/764/1*oQpBxw3nPd8S61W756_k9A.gif" /></figure><p>You might think up a few choice nicknames for whoever wrote that component, but here’s a better idea: Do something about it — really, you can!</p><p>We will tackle the following common React pitfalls:</p><ol><li>Bad <strong>shouldComponentUpdate </strong>implementations and why <strong>PureComponent</strong> won’t save you.</li><li>Changing the <strong>DOM </strong>too fast.</li><li>Using <strong>events</strong> and <strong>callbacks </strong>without limitations.</li></ol><p>In each, we first explain the root of the problem, and then we present some simple ways to avoid it.</p><h3>Own your shouldComponentUpdate</h3><p>The component lifecycle hook<a href="https://reactjs.org/docs/react-component.html#shouldcomponentupdate"> <strong>shouldComponentUpdate</strong></a><strong> </strong>is meant to prevent unnecessary renders. <strong>shouldComponentUpdate </strong>gets the next props and state as arguments, and if it returns <strong>true, </strong>the render function will be executed. Otherwise, it won’t.</p><p>The default implementation for <strong>React.Component</strong> is <strong>return true</strong>.</p><p>More renders means updates take more time, so we prevent unneeded updates to reduce that extra time. To do so, you’d think we’d want to implement strict shouldComponentUpdate functions to the extent we can, right?</p><h4>The Problem</h4><p>Let’s try using a very simple shouldComponentUpdate implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/71f9d806769e60fbea7262174a3a9d3e/href">https://medium.com/media/71f9d806769e60fbea7262174a3a9d3e/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2FNoamELB%2Fembed%2Fpreview%2FbYaeQW%3Fheight%3D600%26slug-hash%3DbYaeQW%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;url=https%3A%2F%2Fcodepen.io%2FNoamELB%2Fpen%2FbYaeQW&amp;image=https%3A%2F%2Fscreenshot.codepen.io%2F1360064.bYaeQW.small.bf8ca70d-844e-448f-a254-746728198a9c.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/1dd5bd9ee36be5560cc3030dbad84191/href">https://medium.com/media/1dd5bd9ee36be5560cc3030dbad84191/href</a></iframe><p>Wait, why it doesn’t work?<br>It doesn’t work because React is creating a new instance of <strong>ReactElement</strong> on each render!</p><p>This means shallow comparison like <strong>return this.props.children !== nextProps.children;</strong> in a shouldComponentUpdate function, is almost as good as writing <strong>return true;</strong></p><p>In my experience, most components usually support <strong>ReactElement </strong>props (PropTypes.node or PropTypes.element) in some way or another, like <strong>children </strong>so this is a common case.</p><p>And what about PureComponent?</p><p><a href="https://reactjs.org/docs/react-api.html#reactpurecomponent"><strong>React.PureComponent</strong></a><strong> </strong>is an alternative to React.Component. Instead of always returning true in its shouldComponentUpdate implementation, it returns the outcome of shallow props and state comparison.</p><p>Using PureComponent will result in the same outcome:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d41f39d455d6842a78c2daf176aa21e9/href">https://medium.com/media/d41f39d455d6842a78c2daf176aa21e9/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2FNoamELB%2Fembed%2Fpreview%2FBmJzbW%3Fheight%3D600%26slug-hash%3DBmJzbW%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;url=https%3A%2F%2Fcodepen.io%2FNoamELB%2Fpen%2FBmJzbW&amp;image=https%3A%2F%2Fscreenshot.codepen.io%2F1360064.BmJzbW.small.0f80dcce-c9be-4f81-88a2-a57a98686e68.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/e16eae80bc547e0f35dbee039e3d9ff4/href">https://medium.com/media/e16eae80bc547e0f35dbee039e3d9ff4/href</a></iframe><p>Is this is a bug or a feature of PureComponent? I can’t be sure. What we do know is that <strong>PureComponent is not useful in most real cases</strong>, and will not prevent updates.</p><h4>Possible Solutions</h4><p>The first thing that may come to mind is — let’s make a deep comparison! This actually works, but it has 2 major cons:</p><ol><li>Running a deep comparison can be a long, heavy, slow process by itself, and the render function will not execute until the shouldComponentUpdate function finishes running.<br>Performance may therefore deteriorate even further.</li><li>It depends on the <em>current </em>implementation of React Elements, and may break in future versions.</li></ol><p>Therefore, in my opinion, using a deep comparison is not a very good solution.</p><p>In searching for a better solution, I looked at how other libraries with Virtual DOM have already been handling this problem.</p><p>I found a very interesting comment by<a href="https://github.com/yyx990803"> Evan You</a> (Vue.js creator) in a<a href="https://github.com/vuejs/vue/issues/4255#issuecomment-274143903"> feature request to add a React-like shouldComponentUpdate to Vue.js</a>; He explained that this can’t be solved by “diffing” Virtual Elements, as it is likely to have many edge cases. Relying on React Elements to detect state change in your component is therefore not a viable solution.<br>Taking that into a practical use — React Elements should be skipped in shouldComponentUpdate implementations. Instead, use some sort of a state change to indicate that the component should be updated.</p><p>Instead of checking the comparison <strong>this.props.children !== nextProps.children</strong>, we will depend on a different prop to indicate a state change, preferably a string/numeric one, in order to make the comparison a very fast one.</p><p>We may even use a new prop, specially designated to indicate that the component should be updated.</p><p>Taking this even further, my colleague (<a href="https://medium.com/u/d8c4fbf20232">Tzook Shaked</a>) and I created a<a href="https://facebook.github.io/react/docs/higher-order-components.html"> HOC</a> that uses <a href="https://medium.com/@franleplant/react-higher-order-components-in-depth-cf9032ee6c3e#5247">Inheritance Inversion</a> to extend components with a generic shouldComponentUpdate implementation, a PureComponent alternative that actually works.</p><p>You can check the code here:<a href="https://github.com/NoamELB/shouldComponentUpdate-Children"><strong> https://github.com/NoamELB/shouldComponentUpdate-Children</strong></a></p><p>I should note that it’s a generic implementation, and therefore it may not fit for all situations. Read more about <a href="https://github.com/NoamELB/shouldComponentUpdate-Children#usage">it in the repository’s readme</a>.</p><p>You can also see in the live example below that the only one <strong>using a custom shouldComponentUpdate implementation</strong>, as proposed here, doesn’t render unnecessarily:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2FNoamELB%2Fembed%2Fpreview%2FRLoxLv%3Fheight%3D600%26slug-hash%3DRLoxLv%26default-tabs%3Dhtml%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;url=https%3A%2F%2Fcodepen.io%2FNoamELB%2Fpen%2FRLoxLv&amp;image=https%3A%2F%2Fscreenshot.codepen.io%2F1360064.RLoxLv.small.c889e5a6-eb41-4a05-b404-bc725a28c9c3.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/5810b8c7486d59f2029e765b5563dfc0/href">https://medium.com/media/5810b8c7486d59f2029e765b5563dfc0/href</a></iframe><h3>Allow your components to scale up</h3><p>Are you using the same component multiple times in your app and it is that making the app very laggy? Do animations look crappy? Sometimes, it only takes one use to result in a performance toll on the whole app?</p><h4>The Problem</h4><p>When creating complex components, you may need to do some custom DOM manipulations. In creating those, you may encounter the following two problems:</p><ol><li><a href="https://developers.google.com/web/fundamentals/performance/rendering/avoid-large-complex-layouts-and-layout-thrashing">Triggering “Layout”</a> too much — when you can trigger Composite or Paint instead.</li><li><a href="https://developers.google.com/web/fundamentals/performance/rendering/avoid-large-complex-layouts-and-layout-thrashing#avoid_layout_thrashing">Layout Thrashing</a> — where you trigger unnecessary DOM recalculations by reading from the DOM right after you have written to it multiple times.</li></ol><p>Let’s look at a naive Collapse component, changing the height between 0 and the content height:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2FNoamELB%2Fembed%2Fpreview%2FMOrpyP%3Fheight%3D600%26slug-hash%3DMOrpyP%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;url=https%3A%2F%2Fcodepen.io%2FNoamELB%2Fpen%2FMOrpyP&amp;image=https%3A%2F%2Fscreenshot.codepen.io%2F1360064.MOrpyP.small.41a8da68-14ad-4537-9eb0-9ebd1d7ef549.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/405a27de690a4422462fa8efdd135b21/href">https://medium.com/media/405a27de690a4422462fa8efdd135b21/href</a></iframe><p>This component works great when it is used alone, but when you decide to use it a few more times…</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2FNoamELB%2Fembed%2Fpreview%2FvWpxvJ%3Fheight%3D600%26slug-hash%3DvWpxvJ%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;url=https%3A%2F%2Fcodepen.io%2FNoamELB%2Fpen%2FvWpxvJ&amp;image=https%3A%2F%2Fscreenshot.codepen.io%2F1360064.vWpxvJ.small.456eccff-1f05-493d-985a-8ae8b95c6965.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc5714c29ca89ffd5ac48a24f09acb4f/href">https://medium.com/media/dc5714c29ca89ffd5ac48a24f09acb4f/href</a></iframe><p>If you are not on mobile right now, try changing your performance to “6x slowdown” to mimic most people’s experience.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*2xOju9iTPGs22U0joILHmQ.png" /><figcaption>Change performance to 6x slowdown on Chrome</figcaption></figure><h4>Possible Solutions</h4><p>Let’s analyze what is happening in the Collapse — this is the part where we change the height:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8528ddd48f4415863adfa22687bc0f4a/href">https://medium.com/media/8528ddd48f4415863adfa22687bc0f4a/href</a></iframe><p>There are 2 things we should notice:</p><ol><li>We are changing <em>height</em>, which according to<a href="https://csstriggers.com/"> csstriggers.com</a> is triggering a Layout recalculation. If we managed to change something like <em>transform </em>instead, that would only trigger Composite and should be much smoother, right?<br>Indeed, that would perform better, but it would also leave a blank space under the Collapse, since we would then never be changing its height.</li><li>Line 3 is a classic example of Layout Thrashing: We do a read from the DOM by this.contentEl.scrollHeight, and then a write to the DOM by setting this.containerEl.style.height. Multiply it by the number of Collapse components.<br>Wouldn’t it be nice if we could group all the reads, perform them together, and after that do all the writes?</li></ol><p>Batching together DOM readings and writings is a good trick to deal with Layout Thrashing, and we can use<a href="https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame"> requestAnimationFrame</a> to do the batching in the following manner:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/884f05d1da1b75048155acf395eec0f8/href">https://medium.com/media/884f05d1da1b75048155acf395eec0f8/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2FNoamELB%2Fembed%2Fpreview%2FWXdjEV%3Fheight%3D600%26slug-hash%3DWXdjEV%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;url=https%3A%2F%2Fcodepen.io%2FNoamELB%2Fpen%2FWXdjEV&amp;image=https%3A%2F%2Fscreenshot.codepen.io%2F1360064.WXdjEV.small.37e30f35-4b16-4c7c-a7a3-00381205b02e.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/6431a590aef0c62dd16328f851fbef9f/href">https://medium.com/media/6431a590aef0c62dd16328f851fbef9f/href</a></iframe><p>This can be very cumbersome. Instead, use inside components, or check<a href="https://github.com/wilsonpage/fastdom"> Fastdom</a> library and use it instead.</p><p>It is worth mentioning that you may not always get a good enough performance, as you are limited by the browser and device capabilities. In those cases, your solution may be a product change.</p><p>For example: Yes, browsers find it hard to open a thousand Collapse components at once, but do you really need to open all 1,000 on the screen at once?</p><p>One last thing: You may hear about something called<a href="https://css-tricks.com/almanac/properties/w/will-change/"> will-change</a>. It may help you in specific cases, but you also risk lesser performance. Take care not to overuse it.</p><h3>Put your callbacks on a leash</h3><p>Having a debounced or throttled version of our functions is useful when we attach any DOM event. It lets us reduce the number of calls to this function to the bare minimum we wanted and thereby improve the performance.</p><p>Write something like this:<br>window.addEventListener(‘resize’, _.throttle(callback)) is very common. But why don’t we use it in components callbacks as well?</p><h4>The Problem</h4><p>Let’s look at the following component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c8c30072f4c87b8c4d2186a7340afebb/href">https://medium.com/media/c8c30072f4c87b8c4d2186a7340afebb/href</a></iframe><p>Have you noticed that we call <strong>this.props.onChange</strong> on every change? This is called a lot of times, where most of the calls are unnecessary. If the parent is making DOM changes, or any other heavy operations, according to the <strong>onChange</strong> callback, we may start seeing a lag in user interactions on the app.</p><h4>Possible Solution</h4><p>Instead, we can implement something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7372e7cbaa5ab9b746ae8d88f9c82ca/href">https://medium.com/media/f7372e7cbaa5ab9b746ae8d88f9c82ca/href</a></iframe><p>Now it calls the <strong>props.onChange</strong> callback only after the user has finished typing, and prevents a lot of unnecessary events along the way.<br>(You can read about the differences between <a href="https://lodash.com/docs/4.17.4#throttle">_.throttle</a> and <a href="https://lodash.com/docs/4.17.4#debounce">_.debounce</a> <a href="https://css-tricks.com/debouncing-throttling-explained-examples/">here</a>)</p><h3>In Conclusion</h3><p>These tools should help you handle the performance pitfalls we can encounter in a React app. By using shouldComponentUpdate wisely, controlling the changes you do to the DOM, and putting your callbacks on delay with debounce/throttle, you can improve your app’s performance greatly.</p><p>If you want to test all of those in real life situations, check out<a href="https://github.com/myheritage/UiZoo.js"> UiZoo</a>. It’s a dynamic component library for React components, and it parses your components and showcases them for you to either develop, test, or share with others.</p><p>Thank you for reading. Drop me a line and let me know if this helped you 😄</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e70f7cbbb5f6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/myheritage-engineering/how-to-greatly-improve-your-react-app-performance-e70f7cbbb5f6">How to greatly improve your React app performance</a> was originally published in <a href="https://medium.com/myheritage-engineering">MyHeritage Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[UiZoo.js — A Dynamic React Components Library]]></title>
            <link>https://medium.com/myheritage-engineering/bibliotheca-dynamic-react-components-library-b4344ff052ca?source=rss-475014d94fbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/b4344ff052ca</guid>
            <category><![CDATA[components]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[uizoo]]></category>
            <category><![CDATA[es6]]></category>
            <dc:creator><![CDATA[Noam Elboim]]></dc:creator>
            <pubDate>Thu, 27 Jul 2017 14:19:25 GMT</pubDate>
            <atom:updated>2017-10-14T15:33:54.618Z</atom:updated>
            <content:encoded><![CDATA[<h3>UiZoo.js — A Dynamic React Components Library</h3><h4>Repository link: <a href="https://github.com/myheritage/uizoo.js">https://github.com/myheritage/uizoo.js</a></h4><p>How many times have you created a new component just to find out later that someone else has already done the exact same thing? How many times have you discovered that the “generic” component you wanted to use was actually tightly-coupled to the environment it was developed for? Well, no more! Introducing — <strong>UiZoo.js.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*6tzlpn9PUNwc8RZXT8oqGQ.png" /><figcaption>An example of how a component looks in UiZoo.js</figcaption></figure><p>UiZoo.js showcases your components, letting you develop in a sterile environment and browse what you already have and can use, to make better use of your components. And all this with almost no effort!</p><p>This tool can be used for development, for product managers to know what is possible, and for UX to see what we have so far. It can also be a playground for cooperation between all of the above.</p><h3>Background</h3><p>UiZoo.js started as an internal tool at <a href="https://www.myheritage.com/">MyHeritage</a> for developing React components and to improve coordination between departments.</p><p>It became a necessity for us so rapidly that we felt we had to share it. It just makes component-creating life so much easier!</p><p>Currently, every React component in the <a href="https://www.myheritage.com/">MyHeritage</a> website is developed using UiZoo.js.</p><p>The following GIF demonstrates how easily a component can be created in UiZoo.js — how changes in the tool reflect your component, and vice versa:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7KYJfDZkEujV_GMHatxukA.gif" /><figcaption>Trying out a component called RaisedButton, first loading an example and then executing an onClick function</figcaption></figure><h3>How it works</h3><p>You provide the documentation of your components and the components to UiZoo.js, which in turn creates a page for each component, as well as a sidebar to search and filter the available components.</p><p>We use <a href="http://usejsdoc.org/">JSDoc</a> parsing from your component’s documentation to show details on the component page, including description, possible props and full JSX examples. We used a well known JSDoc parser called <a href="https://github.com/eslint/doctrine">Doctrine</a> and then created a <a href="https://www.npmjs.com/package/doctrine-standalone">standalone version</a> that we can use in a browser. In this way, we support each and every JSDoc tag imaginable.</p><h4>For example</h4><p>To support <em>examples</em>, we first get all of the needed details from the example JSDoc tags, which contains a JSX as a string. In order to use them dynamically in UiZoo.js, we need to compile them to be React elements. Therefore, we use <a href="https://github.com/babel/babel-standalone">Babel Standalone</a>, a variation of <a href="https://babeljs.io/">Babel</a> that can run in a browser, and compile JSX to javascript. Using an iframe, we can execute the code with a context to have all the needed components available in a closure.</p><p>In the following code snippet, you can see how UiZoo.js compiles and executes examples dynamically:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c068e9224c84d5eeda7cb9f4470d037d/href">https://medium.com/media/c068e9224c84d5eeda7cb9f4470d037d/href</a></iframe><p>Once we have a React element, we can use the power of JSX to <a href="https://facebook.github.io/react/docs/jsx-in-depth.html#choosing-the-type-at-runtime">choose a type at runtime</a> and render the <em>example,</em> as seen in this code snippet:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4efcbd04f253bac793f38ec649e6948c/href">https://medium.com/media/4efcbd04f253bac793f38ec649e6948c/href</a></iframe><h4>Props selectors</h4><p>If the JSDoc holds information on <em>props</em>, we render selectors according to their types. This allows the user to play with the component and try different <em>props </em>combinations.</p><p>In the following code snippet, you can see we choose the selector based on the prop type:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f574260e90e35bad3a4b124562472b8/href">https://medium.com/media/1f574260e90e35bad3a4b124562472b8/href</a></iframe><p>In this GIF, you can see a component called <em>RaisedButton</em> and what happens when the JSDoc of a prop contains <em>literal</em> values, like “pink” and ”purple.”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*anK6t0FfagiuNTygOoF9Dg.gif" /><figcaption>Changing types in a prop to get a custom selector (ParamSelectorVariant)</figcaption></figure><h3>JSDoc Block Tags and their meanings</h3><h4>@name (synonyms: @class, @type)</h4><p>The component name — it becomes the title of the component page.</p><h4>@module (synonyms: @namespace, @memberOf)</h4><p>The section of the component — it appears inside this section on the SideBar.</p><h4>@description (synonyms: @summary, @desc, @classdesc)</h4><p>The component description — it is shown below the title.</p><h4>@<strong>example</strong></h4><p>A JSX example, this tag can be used multiple times for more examples. The 1st example is loaded automatically when you browse through to reach to a component page. You can add // comments to explain what a given example contains.</p><h4>@prop (synonyms: @property, @arg, @argument, @param, @member)</h4><p>A possible prop for the component, use <a href="http://usejsdoc.org/tags-param.html#optional-parameters-and-default-values">JSDoc optional syntax</a> to indicate a prop as optional. For some prop types, like function, we have a special input selector.</p><h3>What’s the difference between UiZoo.js and Storybook?</h3><p>You may have already heard about a similar solution called <a href="https://storybook.js.org/">Storybook</a>. We actually began our journey to UiZoo.js by using Storybook ourselves, and it is an absolutely great tool. You create a “story” for each variant of a React component and showcase all of your “stories” on the tool UI.</p><p>We created UiZoo.js, however, because we already had good JSDoc documentation on all of our components and wanted to use it to create the “stories” for us. Also, we maintain JSDoc anyway, so using UiZoo.js ensures that we maintain the most updated components version and variations automatically.</p><h3>Contributing</h3><p>We have a roadmap of features that we would like to add: URL sharing of a specific props combination, better error messages, and even live JSX coding for <em>children props</em> and other react’s <em>node </em>types<em>.</em></p><p>Contributions and feedback are very welcome! Please check our <a href="https://github.com/myheritage/UiZoo.js/blob/master/CONTRIBUTING.md">guidelines</a> if you want to contribute.</p><h3>In Conclusion</h3><p>Using UiZoo.js has made our everyday practice of developing React components so much easier. It has even improved our coordination with our product managers and designers.</p><p>More than that, it enables us to see what we have without having to go through thousands of lines of code only to figure out that the component you have been working on is too tightly coupled to the environment it was developed on for your current functionality.</p><p>Try our live example <a href="https://myheritage.github.io/UiZoo.js/">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b4344ff052ca" width="1" height="1" alt=""><hr><p><a href="https://medium.com/myheritage-engineering/bibliotheca-dynamic-react-components-library-b4344ff052ca">UiZoo.js — A Dynamic React Components Library</a> was originally published in <a href="https://medium.com/myheritage-engineering">MyHeritage Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How Using Yeoman Changed the Way We Work]]></title>
            <link>https://medium.com/myheritage-engineering/how-using-yeoman-changed-the-way-we-work-befd0eeaf5d6?source=rss-475014d94fbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/befd0eeaf5d6</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[generator]]></category>
            <category><![CDATA[yeoman]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[scaffolding]]></category>
            <dc:creator><![CDATA[Noam Elboim]]></dc:creator>
            <pubDate>Fri, 03 Jun 2016 07:00:00 GMT</pubDate>
            <atom:updated>2017-07-14T08:21:11.129Z</atom:updated>
            <content:encoded><![CDATA[<h4>I dove head-first into <a href="http://yeoman.io/">Yeoman</a> — from not knowing anything or understanding how it could help at work, to building a custom generator just for MyHeritage uses.</h4><p><em>This article also appeared on CSS-Tricks: </em><a href="https://css-tricks.com/using-yeoman-changed-way-work/"><em>https://css-tricks.com/using-yeoman-changed-way-work</em></a></p><p>On my first day at <a href="https://www.myheritage.com/">MyHeritage</a>, I remember this exchange with the web development team leader:</p><blockquote><strong>Team Lead:</strong> You have some experience working with Node.js modules like Yeoman, right?</blockquote><blockquote><strong>Me:</strong> Yeah, I know a bit.</blockquote><blockquote><strong>Team Lead:</strong> I think using Yeoman here would save us a bunch of time on creating new pages. Do you think you can manage something like that?</blockquote><p>Well, that was my first day; I barely knew where my seat was. However, I remember clearly what I was thinking, and I didn’t understand how Yeoman could help here.</p><p>For me, and for many others, Yeoman was a tool to generate a full working application, from zero to hero, in no more than two seconds. It was not for adding parts to an existing application.</p><p>I was completely wrong. Allow me to share with you how we created a Node.js module at MyHeritage that extends the functionality of Yeoman to save us tons of time on everyday tasks.</p><h3>The Problem</h3><p>Creating new pages in MyHeritage used to be a challenging task. Each new page that was created demanded about two days of work to prepare everything, including asset management, a server-side component and a client-side bootstrap of an application. In short: a large number of files.</p><p>Most developers do not create new pages on a regular basis. Before the real work could even begin, a lot of time was required just to understand how to create the new page. Then, once the page was created and the work was “done”, there were still hours of time remaining to debug anything that didn’t work.</p><p>Two days was just way too much time to invest in a simple task like that.</p><h3>A Ray of Hope</h3><p>A couple of months after my conversation with our team lead, the toll of creating new pages had become significantly greater and we couldn’t continue down that path. We knew that an immediate solution was necessary and that it must be simple, easy to maintain, and most obviously, very fast to use.</p><p>First, we broke down the problems associated with creating a new page. The process required a lot of steps, including routing configurations and translations services, running terminal commands for creating certain files (like A/B tests or Sass files) and even running some Gulp tasks (like compiling Sass or creating sprites).</p><p>At this point we were certain that extending Yeoman would serve us best, but we still had to figure out how it could satisfy all of these needs while still maintaining its core simplicity and ease of use.</p><h3>A Few Words on Yeoman</h3><blockquote>Yeoman helps you to kickstart new projects, prescribing best practices and tools to help you stay productive.</blockquote><p>That is the first line on Yeoman’s website and it’s 100% right.</p><p>It’s a Node.js module with thousands of generators to choose from. All generators basically work the same — first the user is asked a set of questions in user interfaces that are based on Inquirer.js. Then, using the user’s input, Yeoman generates the proper new files.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/959/1*MbdjXYoyFOOe7JTn4c5PMw.png" /><figcaption>A string input prompt for providing the name of the project.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/938/1*l-IADaFqj8jqOPxbEwEw5w.png" /><figcaption>A checkbox prompt is used to set the “Master Page” configuration.</figcaption></figure><h3>Why Yeoman</h3><p>We had a range of options from using a full working generator to building something from scratch. There are a couple of reasons we chose Yeoman:</p><p>First of all, simplicity. Yeoman features a clear API, a generator’s generator for getting started, and serious ease-of-use as a Node.JS module.</p><p>Second, maintainability. Yeoman is being used by thousands of people worldwide and it’s based on the Backbone.js Model which allows it to be extended in a way that is easy to understand.</p><p>Finally, speed. Simply put: speed is a given with Yeoman.</p><h3>Our solution</h3><p>We wanted to add another stage between the two halves of Yeoman’s flow to extend the data that we collect from the user through an ordered set of functions. For example, we may want the ability to get snake_case from CamelCase, or to capture the output of a command executed in the terminal. We require this stage to be configurable via a JSON file.</p><p>We break every function down to an object that includes the name of the function to execute, the arguments, and where to store the output. We use Node.JS so that those functions run asynchronously and return promises. When all promises resolve, we generate the files. Here is an example of how the Generator class uses the Yeoman-generator module, which is named “generators”:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe015065c08c633a222de7a4bc6089c5/href">https://medium.com/media/fe015065c08c633a222de7a4bc6089c5/href</a></iframe><h3>How to Build a Generator</h3><p>Once the base is ready, creating a new generator is a piece of cake. We just need to create a new folder containing 4 files and a folder of templates in it. The name of the folder is the name of the generator. The 4 files are:</p><ul><li><strong>Prompts</strong> — a JSON file full of objects which map prompted questions to the stored data.<br>In this example, the first item will ask the user to “Name your generator:” with the default answer of “newGenerator”. It stores the answer in the data object by the field “name”.<br>The second item asks a multi-choice question with the options “yes” and “no”. The default is the first choice, “yes”.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1b64656a3520cc5948ef8df8422c6e1/href">https://medium.com/media/d1b64656a3520cc5948ef8df8422c6e1/href</a></iframe><ul><li><strong>Services</strong> — a JS file with all the functions we need for extending the data from the user.<br>This example explains a service called “generateSprite”, which will run a terminal command that is already configured for the project. This specific script creates a sprite folder. If the function manages to run the command successfully, it will resolve a promise that will mark the operation as successful. Templates can be generated based on the outcome.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/443b0ac84b6f9cf5bcb6584f7b2d8262/href">https://medium.com/media/443b0ac84b6f9cf5bcb6584f7b2d8262/href</a></iframe><ul><li><strong>Actions</strong> — a JSON file that maps of all the functions that we need to execute before generating files — the “pre” section — and another mapping of template files and their final location where the files will be generated, which is the “main” section.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6792a84de8a7df39fbe82fb9ff73031e/href">https://medium.com/media/6792a84de8a7df39fbe82fb9ff73031e/href</a></iframe><p>In the “pre” section there is a task which will run only if the field “name” is defined or true (based on the requirements defined in the “dependencies” array of fields). The arguments to provide in the service function inside the “args” object by the “arguments” array. The output of the function is saved in the location specified by the “output” field. The name of the function is defined by the “action” field. In this implementation you can provide only one function, but it could easily support multiple functions by using an array in “action”.<br>In the “main” section there is a task with similar functionality. If a field in the “dependencies” is missing or faulty, then the file will not be generated. However, this is not the case with “optionalDependencies” which are, in fact, optional. The fields exposed to the templating engine are defined in the “dependencies” and “optionalDependencies”.<br>The “templatePath” has the path to the template file and the “destinationPath” is the destination for the file. “destinationPath” can use data from the “pre” section as we see here with the “name” field (&lt;%= name %&gt;).<br>Notice that we use EJS templating language. Yeoman actually supports additional templating engines beside EJS, like handlebars.</p><ul><li><strong>Index</strong> — a JS file. Yeoman requires that each generator has an index file. The index links the 3 other files and inherits from the base. Basically, it creates an instance of the base with separated prompts, services and actions.<br>As you can see in this example, it creates the instance and exports it to work with Yeoman.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b7de40db2c6918ef8f86ffc3e7dcb03/href">https://medium.com/media/2b7de40db2c6918ef8f86ffc3e7dcb03/href</a></iframe><ul><li><strong>Templates</strong> — easy as it sounds: just templates to create files.<br>Note that the balance between extending the data and creating templates is completely dependant upon the kind of generator you would like to create. For example, a generator that only creates new files with no sophisticated data may not require extension. On the contrary, we might want a generator that just runs a set of system commands, which will have no templates.<br>Both cases are possible, and even likely, for some uses. The ability to do both, or just either one, exhibits how flexible it is to use and create a generator for any need.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9eb19455826d7d895e3e6f5159e48af3/href">https://medium.com/media/9eb19455826d7d895e3e6f5159e48af3/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AQj8gzwi_XekNPpuZ_3CqQ.png" /></figure><h3>What We Have Gained</h3><p>Since we solved our problem by creating the generator for new pages, we have been able to use Yeoman to create more generators, including one that creates a new part in our backend API and another that creates a full working Angular application (with unit tests and all).</p><p>As we use Yeoman more and more every day, the pros for us are quite obvious, but there are some cons that we have noticed and you should keep them in mind.</p><h4>Pros</h4><ul><li><strong>Fast</strong> — Creating a new page now takes us about 1 minute.</li><li><strong>Best practices and conventions</strong> — Yeoman forces developers to use best practices and helps ensure that conventions are followed and learned by our team. A team can make sure every page made will use the same conventions, no matter which developer created it. Do you want to make sure every page uses some new and shiny service? Just add it in the “new page” generator and you are done.</li><li><strong>Cleaner code environment</strong> — Because we removed the ability for developers to copy/paste sections of code that they don’t completely understand, bugs tend to appear far less frequently and the unnecessary duplication of code is much less of a problem.</li></ul><h4>Cons</h4><ul><li><strong>Maintenance</strong> — This is the hard part. Best practices tend to evolve and change. You always have to make sure that the generator is in the forefront of the codebase, which requires investment. It’s not a “hit and run” project — you will have to update it as your codebase and technology evolve.</li><li><strong>Fit for all uses</strong> — This is more of a challenge than a con, but failure to implement a generator properly could lead to developers who don’t use the generator you have worked long and hard to build. It is important for the generator to be generic but still save the developer a significant amount of time on repetitive and frustrating tasks. For example, instead of implementing a function, put a TODO with some explanation.</li></ul><h3>In Conclusion</h3><p>Our generators are very specific to our projects, so those aren’t open source, but there are lots of more widely useful generators you can use or reference as you build your own, like <a href="https://github.com/kriasoft/react-starter-kit">this one for React</a>.</p><p>Using Yeoman really made our everyday work easier and better after we created generators for a lot of repetitive tasks. We even made a generators’ generator! But it also brought to our lives unexpected challenges that we cannot ignore. There is a balance between what should be automated and what needs to be done manually by a developer and it’s a matter of figuring this out for each project and need… But that’s a topic for another time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=befd0eeaf5d6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/myheritage-engineering/how-using-yeoman-changed-the-way-we-work-befd0eeaf5d6">How Using Yeoman Changed the Way We Work</a> was originally published in <a href="https://medium.com/myheritage-engineering">MyHeritage Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>