<?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 Ofir Dagan on Medium]]></title>
        <description><![CDATA[Stories by Ofir Dagan on Medium]]></description>
        <link>https://medium.com/@ofirdagan?source=rss-972258ae730e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*3_C-d3inIfGKphyICVVIJQ.png</url>
            <title>Stories by Ofir Dagan on Medium</title>
            <link>https://medium.com/@ofirdagan?source=rss-972258ae730e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 18:37:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ofirdagan/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[(Don’t) Blame It On React Native]]></title>
            <link>https://medium.com/hackernoon/dont-blame-it-on-react-native-2eb0be3000b6?source=rss-972258ae730e------2</link>
            <guid isPermaLink="false">https://medium.com/p/2eb0be3000b6</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Ofir Dagan]]></dc:creator>
            <pubDate>Sat, 03 Nov 2018 15:54:16 GMT</pubDate>
            <atom:updated>2019-05-24T07:05:38.751Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Abstract</strong><br>It’s always fun to blame <a href="https://hackernoon.com/tagged/react">react</a> native for everything that’s wrong in our lives. However, what we keep finding, again and again, is that in most cases, writing better performant react code fixes our app’s performance and laggy UI issues.</p><p><strong>Part 1 — Blaming React Native</strong><br>My story begins three years ago, when react native was in version 0.12, it didn’t even support android! We just made a major decision at my company (<a href="https://www.wix.com/">wix.com</a>) to gamble “all in” on react native and write our main app solely in react native using as little native code as possible. You see, Wix is a web company. We have hundreds of front end developers working on tens of products. For us, re-writing these products both for ios and android was too big of a pain. We saw the potential in react native and went for it.<br>Jump ahead two years, we now have an amazing app for both android and ios. We’re writing and maintaining dozens of <a href="https://github.com/wix">open source libraries</a> for react native with thousands of stars, and we’re about 60 developers constantly contributing code to the Wix app.</p><p>Even though react native has improved a lot since version 0.12, things are not so peachy as they’re often portrayed. If you ever wrote a real, complex react native app on production with a lot of traffic then you know what I’m talking about.<br>It comes down to these two images:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*tF4GTxa_ydKv4lqv7GS0ZQ.png" /><figcaption>When I think about React Native I want to…</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*UuOb29orEAHiAf1ENY3rRw.png" /><figcaption>But eventually I just…</figcaption></figure><p>I’m not going to lie, while react native gives you an easy way (if you’re a front end developer) to write native apps, <strong>writing react native is no walk in the park.</strong></p><p>It’s hard because React Native…</p><ul><li>Adds an abstraction layer over native APIs</li><li>Performance sucks</li><li>Is single threaded</li><li>Makes it difficult to accomplish things that otherwise are very easy in regular native apps (Efficient long lists for example)</li><li>Build is more complex</li></ul><p>Here’s a quote every RN developer said or thought to himself at least once</p><blockquote><strong>“<em>Our only option for a truly performant app is to write a pure native app</em>”<br>- A<em>ny react native developer</em></strong></blockquote><p>While indeed it’s an option, and it might be a valid one, depends on your use case. I claim it’s not the only one!</p><p><strong>Part 2 — Aha moment</strong></p><p>About a year ago we did a lot of performance improvements in our app. And time after time, bug after bug, issue after issue, we found that with better performant <a href="https://hackernoon.com/tagged/javascript">javascript</a> and react code we could solve most of the issues. This was my “aha moment”. It changed things for me.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*iX95dA6Lpg5imCIqekNIHQ.jpeg" /><figcaption>What if I told you we can solve 90% of the problems in javascript</figcaption></figure><p>Let’s go over a few code snippets and case studies to demonstrate this.</p><p><strong><em>Problem #1- Send button enable / disable</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/280/1*qoh6ILL-VVPPWVy7903Pkw.gif" /><figcaption><em>Send button enable / disable</em></figcaption></figure><p>Part of our app includes a chat module, so site owners can talk with visitors on their site. The feature is super simple. There’s an input field and a “send” button. The button should be disabled when the input is empty and enabled when the input is not. What happened was, it took about a second (sometimes even more) for the button to become enabled after the user started typing.</p><p>We discovered that we rendered the entire list of chat rooms, even when we were inside a single chat room screen. This happened because we created a <strong>new chatroom object</strong> for each chatroom after every change in any of the other chat rooms. It might sound like a horrible mistake on our part, but when writing in a functional manner — it looks right and harmless. The solution we choose to overcome this issue was to save the unchanged chatrooms using memoization.</p><p><strong><em>Problem #2- Messages arrive in delay</em></strong></p><p>The second issue we tackeled was the slow rendering of the chat messages as they came in realtime. Here are two videos (before and after) showing the problem.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FgCmSf67won4%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DgCmSf67won4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FgCmSf67won4%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/62a3d39fdeeacf5f48310a460622f686/href">https://medium.com/media/62a3d39fdeeacf5f48310a460622f686/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fd6fJaLqBlvY%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dd6fJaLqBlvY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fd6fJaLqBlvY%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/14e6eff2d9f3882a0ce5fce319c3d814/href">https://medium.com/media/14e6eff2d9f3882a0ce5fce319c3d814/href</a></iframe><p>As you can see, there’s a substantial difference between the two versions. The problem was <em>componentWillReceiveProps</em> was called <em>way</em> too many times. And for each time it was called, it triggered a new render cycle, due to a call to <em>setState.</em></p><p><strong><em>Problem #3- Typing Animation</em></strong></p><p>This one I really like. Our very talented and creative designers challenged us with a super slick and fancy typing animation. The kind where the bubble heads slide over each other, pushing up the previous messages, sliding up and down and transforming magicly into message bubbles. You know... any front end dev’s nightmare. :) At first, we went with the naive approach and calculated our animations on javascript using <em>Animated</em>. Well, that didn’t work. Because of the way Animated works, you can’t set properties such as “height” which we needed to animate while using the “useNativeDriver” flag. This meant that all of the Animated calculations were done on the javascript side which made the animation look very laggy. Read more about why you can’t change height while using the native driver <a href="https://github.com/facebook/react-native/issues/13150">here</a>.</p><p>Then we said, wait a minute! React native gives you the promise to do anything you want in native. So we did. We tried native, but it sucked as well. While the animation itself worked very smoothly on the native side, when we tried to embed it into our react native controlled view we got into trouble. Why? Part of our animation included changing the container of the animated section’s height which in turn changed the layout of its sibling which were written in RN. And so, we were back to square one. We could’ve done the animation in the native side but then we needed to tell RN that we did so (changed the height). In turn, RN needed to do his animations based on that which he only could do on the javascript thread due to the same problem as before.</p><p>Just before giving up, we discovered that we could do it using react native’s <a href="https://facebook.github.io/react-native/docs/animations#layoutanimation-api">LayoutAnimations</a>.</p><p><strong><em>Problem #4- Module load time</em></strong></p><p>The last one I want to share with you is a story about a simple “require vs import”. Our app consists of many independent modules. You can think about it as if each module has a different bundle. What we found was that just delaying the import statements by requiring it in only when needed, saved us a lot of time. I’m talking about seconds across the app.</p><p>Again, all we did was remove the “import” statements from the top of the file (which you might think is harmless) and instead require what we needed, only when we needed it.</p><p><strong>Part 3— That’s just how javascript and react works</strong></p><p>So what am I really saying here? Basically what I’m trying to say is that react native is not to blame in any of these cases. That is just how javascript and react works. It’s just a lot easier to see the consequences in react native due to the react native bridge and the low computing powers of mobile devices.</p><p>To prove my point I was set out to create a simple react web app that will suffer from poor performance and that it would be fixable by changing very few lines of code (actually just one line of code is needed). I call this project the <em>“Good Card / Bad Card”</em>. It’s a very simple react app. It shows a list of cards, an total of 100, nothing crazy. Each card has an input field. In the bad card you can see the lag in the UI when you type in the input field. The keyboard just doesn’t react as you’d expect. The good card works flawlessly. I’ve put the project on <a href="https://github.com/ofirdagan/good-card-bad-card">github</a> and I encourage you to check it out. The code is very simple and it’s only a single line of code that has a tremendous effect. For those of you who don’t have time, you can just check out the two versions here:</p><ul><li><a href="https://ofirdagan.github.io/good-card-bad-card/dist/bad-card/">Bad Card</a></li><li><a href="https://ofirdagan.github.io/good-card-bad-card/dist/good-card/">Good Card</a></li></ul><p><strong>Part 4— It’s up to us</strong></p><p>Now that we know all of this. What can we do about it? Well it’s up to us. We must be <strong>more careful and responsible react developers</strong>.</p><p>We can do that by following these guidelines:</p><ul><li><strong>Know your crucial app flows and keep monitoring them</strong><br>- It doesn’t matter how you monitor your app as long as you’re making sure that you got your crucial app flows covered and that you will get notified when performance degradation happens</li><li><strong>Avoid writing anonymous functions as props</strong><br>- These could potentially have a huge impact</li><li><strong>Know your components</strong><br>Don’t think that just writing <em>PureComponents</em> will solve everything</li><li><strong>Know your components lifecycle methods</strong><br>Each component is different and might need to use different lifecycle method</li></ul><p>Last but not least <strong>know your reducers</strong>. For those of you that are familiar with redux. I want to show you a cool analogy between databases and reducers</p><p>Let’s start with the database. Let’s say that we have a table where we got many writes to one field and many reads to another field. Any write locks the table and delays the read. The solution is to separate the fields into two tables.</p><p>In redux, let’s say we have a lot of actions that changes a field (it can also be an inner field of some object) but not many reads for this field. Any state change triggers the rendering cycle. The solution here is the same, separate to two reducers.</p><p><strong>Part 4 — Epilogue</strong></p><p>By now you might be thinking well, that is nice and all but where and how should I begin profiling my app. This is too F*** vague and hard!</p><p>Luckily for all of us, I have really good news. Help is just around the corner and its name is <em>“why-did-you-update”</em>. “Why did you update” is a library that monkey patches React and notifies you in the console when potentially unnecessary re-renders occur. I really urge you to check it out on <a href="https://github.com/maicki/why-did-you-update">github</a> and I promise you that you won’t be disappointed. Also, you’re welcome to print this amazing performance cheat sheet I did with the key takeaways of this post. Cheers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*r0W5isalVkx9QB7_m5KY9Q.jpeg" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2eb0be3000b6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/dont-blame-it-on-react-native-2eb0be3000b6">(Don’t) Blame It On React Native</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build Your Own React]]></title>
            <link>https://medium.com/hackernoon/build-your-own-react-48edb8ed350d?source=rss-972258ae730e------2</link>
            <guid isPermaLink="false">https://medium.com/p/48edb8ed350d</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[how-to]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Ofir Dagan]]></dc:creator>
            <pubDate>Fri, 22 Sep 2017 14:23:06 GMT</pubDate>
            <atom:updated>2019-05-24T07:07:14.622Z</atom:updated>
            <content:encoded><![CDATA[<h3>Build Your Own React — A Step By Step Guide</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HSisLuifMO6KbLfPOKtLow.jpeg" /></figure><p><strong>Abstract- </strong>In this post I’ll create a full working version of <a href="https://hackernoon.com/tagged/react">react</a> step by step. It won’t be an efficient version but it give you a glimpse to how react works under the hood. If you’re familiar with react / web / dom etc.. you can go ahead and skip to step 1. Alternatively if you’re the video type, you can check out my talk on <a href="https://www.youtube.com/watch?v=RmAIwZeY0tk">youtube</a>.</p><p><strong>Intro</strong></p><p>Before writing react apps, like many others, I wrote in angular. The thing about angular is that if you want to be a good angular developer you need to know how angular works. The more you know, the better you’ll be. With react though, it doesn’t seems to be the case. You learn about class components and stateless components. You learn about props and state and that you should never change the props. Then you learn that in case you want something to be dynamic you call setState and that’s pretty much it. Excluding lifecycle methods and other small features there is not a lot more you need in order to write good react apps. There’s no doubt that the learning curve of react vs angular is much smaller.</p><p>But, I became a programmer because I like knowing how stuff works. What makes things tick. It doesn’t matter if it’s the international space station, tesla car or a vending machine. When I see something that interests me, soon then after I start questioning how it works. The same happened with react. I saw a great talk about react under the hood and I thought to myself.. That’s doesn’t look so complicated.. I can try it out.</p><p><strong>What is react anyway?</strong></p><p>Before we’ll dive in writing react we should know what is it that we want to build. As I see it, react is a library for building dynamic trees. In the web use case react will build a DOM tree. However it doesn’t have to be DOM. With <a href="https://facebook.github.io/react-native/">react native</a>, react builds a native app hierarchy of ui controllers. You could even write <a href="https://github.com/iamdustan/react-hardware">arduino apps</a> in react.</p><p><strong>What is a DOM tree anyway?</strong></p><p>DOM stands for document object model. Every html document is built from elements that are called “dom elements”. The <em>&lt;html&gt;</em> is a dom element. Also <em>&lt;div&gt;</em> and <em>&lt;h1&gt;</em> etc. The tree has a root, usually the html tag. Every node in that tree has its own properties and it may have children which are also dom elements. For example. In the following image you can see a part of an html document and its representation as a tree of objects.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*de8SJEwnxDCBfLw9Jq4ZdA.png" /></figure><p><strong>A few words about react and jsx</strong></p><p>If you’re already familiar with react you probably use jsx. So a simple component can look like this:</p><pre>const mySimpleComponent = () =&gt; {<br> return &lt;h1&gt;Hello World&lt;/h1&gt;;<br>}</pre><p>After running babel or your other favorite transpiler this component will look like this:</p><pre>const mySimpleComponent = () =&gt; {<br> return React.createElement(‘h1’, null, `Hello World`);<br>}</pre><p>A special babel plugin will transpile the jsx call to the underlying js api that react provides. In our case the &lt;h1&gt; tag got transpiled into a React.createElement call where the first argument is the tag name, the second argument is the properties and the third is its children.</p><p>Let’s look at a bit more complicated jsx example:</p><pre>&lt;div style={{color: yellow}}&gt;<br>  &lt;Title text=`I&#39;m a title`/&gt;<br>  &lt;Person name=`moki`/&gt;<br>&lt;/div&gt;</pre><p>Which will result in:</p><pre>React.createElement(`div`, {style: {color: yellow}},<br>  React.createElement(Title, {text: `I&#39;m a title`}, null),<br>  React.createElement(Person, {name: `moki`}, null)<br>);</pre><p>Now that we’re in sync we can start building our own react.</p><h3><strong>Step 1- DOM elements (Hello World)</strong></h3><p>First, I’ll create a host html document for my app. It will load my react.js version, app.js for the app’s logic and it will have a div with id root to attach my react app to.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/840160e07eaa00ebb80b230169de5297/href">https://medium.com/media/840160e07eaa00ebb80b230169de5297/href</a></iframe><p>The way we’re going to build react will be in what I like to call ADD. It’s like TDD but instead of Test Driven Development it’s stands for Application Driven Development. In each step I’ll show the app.js that I would like to render. Then I’ll implement it in the react.js file.</p><p>My firstapp.js looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8d40b9e63e451f575700a380a842ac5/href">https://medium.com/media/e8d40b9e63e451f575700a380a842ac5/href</a></iframe><p>In order for this code to work I will need to implement React.createElement and ReactDOM.render</p><p><em>*ReactDOM is a separate module from react but for the sake of simplicity I’ll write them both together.</em></p><p>Let’s do this. Here’s myreact.js :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66dd1baecd63f0dbee80bbf2df917c0f/href">https://medium.com/media/66dd1baecd63f0dbee80bbf2df917c0f/href</a></iframe><p>And we got an hello world working! You can see the result <a href="https://cdn.rawgit.com/ofirdagan/build-your-own-react/2e8bad05/v1-element/index.html"><em>here</em></a>.</p><h3><strong>Step 2 — Render non DOM elements or as we like to call them in react `components`</strong></h3><p><strong>2.1- </strong>We want to add support for stateless components</p><p>app.js :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1285826a9a9b770bcf15a20e342df5df/href">https://medium.com/media/1285826a9a9b770bcf15a20e342df5df/href</a></iframe><p>The only thing changed here is that now the element can be a function. In case it is, we’ll invoke it.</p><p>react.js :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2dcc359bbc88c52ebd9ab88655ea9a1e/href">https://medium.com/media/2dcc359bbc88c52ebd9ab88655ea9a1e/href</a></iframe><p><em>*</em><a href="https://cdn.rawgit.com/ofirdagan/build-your-own-react/2e8bad05/v2-non-dom-element/v2-step1/index.html"><em>result</em></a></p><p><strong>2.2-</strong> Not all children are born equally. We want to add support for non plain text children. Let’s refine the way we handle children.</p><p>app.js :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a84cc388e0501b68daeff9d3cbc4021/href">https://medium.com/media/9a84cc388e0501b68daeff9d3cbc4021/href</a></iframe><p>react.js :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e5f8d686cada7f0ca2648e98128a7a1/href">https://medium.com/media/7e5f8d686cada7f0ca2648e98128a7a1/href</a></iframe><p><a href="https://cdn.rawgit.com/ofirdagan/build-your-own-react/2e8bad05/v2-non-dom-element/v2-step2-dom-children/index.html"><em>*result</em></a></p><p><strong>2.3</strong>- Add support for class components</p><p>app.js :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3c994fad4d038b4c011a960ecf7f575/href">https://medium.com/media/f3c994fad4d038b4c011a960ecf7f575/href</a></iframe><p>As we did with the stateless components. We will find out if it’s a class and in case it is, we’ll create a new class instance and call its render function.</p><p>react.js:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/747d40d13b80a03dda54b2cabfdfb0dc/href">https://medium.com/media/747d40d13b80a03dda54b2cabfdfb0dc/href</a></iframe><p><em>*</em><a href="https://cdn.rawgit.com/ofirdagan/build-your-own-react/2e8bad05/v2-non-dom-element/v2-step3-classes/index.html"><em>result</em></a></p><p><strong>2.4-</strong> It’s time for some good old refactor. Our anElement function is getting too long. Let’s extract some methods out. Also, let’s create a react-utils.js for miscellaneous functions such as isClass, isStatelessComponent etc..</p><h3><strong>Step 3- Props and State</strong></h3><p>To recap, up until now we rendered dom elements, stateless components and class components. Let’s add props and state to the mix</p><p><strong>3.1-</strong> Stateless component props</p><p>app.js :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4be64372fdbfa65fae326df52db913f6/href">https://medium.com/media/4be64372fdbfa65fae326df52db913f6/href</a></iframe><p>This one is easy. We just need to pass in the props to the component (function)</p><p>react.js :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5e04c494af07aabc6c0711ae0f5b1e4/href">https://medium.com/media/b5e04c494af07aabc6c0711ae0f5b1e4/href</a></iframe><p><em>*</em><a href="https://cdn.rawgit.com/ofirdagan/build-your-own-react/2e8bad05/v3-props-and-state/v3-step1-stateless-comp-props/index.html"><em>result</em></a></p><p><strong>3.2-</strong> Class component props</p><p>app.js :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b7e706460df0bb79cc11ac7102ed552/href">https://medium.com/media/0b7e706460df0bb79cc11ac7102ed552/href</a></iframe><p>You should notice that the Hello class now extends React.Component. I did this so I will have a common parent class for all of my react classes for purposes such as assigning the props on the class instance.</p><p>Component class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5cd70f25ff3f6342408bab80863f9047/href">https://medium.com/media/5cd70f25ff3f6342408bab80863f9047/href</a></iframe><p>Now that we have the Component class, we’ll pass the props in the constructor and we’re done.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c13b8e3d37c17c39d3725bc9136d629/href">https://medium.com/media/9c13b8e3d37c17c39d3725bc9136d629/href</a></iframe><p><strong>3.3-</strong> Attributes</p><p>This simple component should show an alert on button click.</p><p>app.js :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/807c3ab746768b50814b9d37802d1a6e/href">https://medium.com/media/807c3ab746768b50814b9d37802d1a6e/href</a></iframe><p>As with the children, we’ll iterate over the attributes and set event listeners to attributes that starts with on* and set attribute for the rest.</p><p>react.js :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cdd7f27b275763f4965813219654b954/href">https://medium.com/media/cdd7f27b275763f4965813219654b954/href</a></iframe><p><em>*</em><a href="https://cdn.rawgit.com/ofirdagan/build-your-own-react/2e8bad05/v3-props-and-state/v3-step3-attributes/index.html"><em>result</em></a></p><p><strong>3.4-</strong> Refactor. handleHtmlElement became too big. We’ll extract out appendChild and appendProp functions. Also we can clean the code a little bit using lodash.</p><p><strong>3.5-</strong> State</p><p>We’re ready to write a real react app. Let’s write a counter app. It will have two buttons to increment and decrement the counter and will present the current value</p><p>app.js :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/07b6f5c8503667858490f69056767feb/href">https://medium.com/media/07b6f5c8503667858490f69056767feb/href</a></iframe><p>To make this work we need to implement the setState function. My naive algorithm will delete the entire dom on every setState call and will render it again. For that I will need to save a reference to the root react element and to the root dom element.</p><p>react.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/82bf6656932c50700a936842b918277b/href">https://medium.com/media/82bf6656932c50700a936842b918277b/href</a></iframe><p>Next I would like to save the classes that I’ve created so when reRender happens I won’t create them again and lose my state. To achieve this I will do the following:</p><ul><li>Hold a cache of the instantiated classes</li><li>When handling a class. In case of a match, return the cached instance</li><li>Instead of calling render straight away return an object with a special flag that marks it as a react class.</li><li>When appending a child node. If it’s a react class call its render</li></ul><p>handleClass :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a7b20c199bced9015b547013a13e45df/href">https://medium.com/media/a7b20c199bced9015b547013a13e45df/href</a></iframe><p>handleChild :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a718e3782a7172dd61825511283f943/href">https://medium.com/media/3a718e3782a7172dd61825511283f943/href</a></iframe><p><em>*</em><a href="https://cdn.rawgit.com/ofirdagan/build-your-own-react/2e8bad05/v3-props-and-state/v3-step5-state/index.html"><em>result</em></a></p><p>That’s it. We have our own working react. Don’t believe me? Check out <a href="https://cdn.rawgit.com/ofirdagan/build-your-own-react/2e8bad05/v5-examples/v5-step1-todo/index.html">this todo app</a> that I took from the web. I didn’t changed anything except for the references to the real react and react-dom.</p><p>The todo app doesn’t impress you much? How about <a href="https://cdn.rawgit.com/ofirdagan/build-your-own-react/2e8bad05/v5-examples/v5-step2-minesweeper/index.html">this minesweeper game</a>?</p><p><strong>Now what?</strong></p><p>Let’s reflect on what I’ve showed you a little bit. The react version I wrote… It doesn’t seem super efficient. On every call to setState I clear the entire dom and creates it from scratch. Also the first thing you hear when someone talks about how react works is the mysterious virtual dom. I didn’t write anything virtual in my code nor did I mention it.</p><p><strong>Time to face the truth</strong></p><p>Time for some performance analysis. I timed the time for first render of the minesweeper game both with my react and with the real react. The results are going to surprise you.</p><p>It seems that my react is doing about 2.5 times better than the real react. But let’s hold our horses for a minute. If we continue to play we can see the time it takes for every setState to finish. With my react it takes between 2–8 ms as opposed to 0.01ms for the real react. This difference is why we love react and use it so often. Besides being super intuitive and easy to learn, it’s very fast and keep getting faster.</p><p><strong>The react algorithm</strong></p><p>First my algorithm: on every re-render I</p><ul><li>Clear the dom</li><li>Render the DOM from scratch</li></ul><p>If we look at the code we will see that everything we wrote was <a href="https://hackernoon.com/tagged/javascript">javascript</a>. We didn’t use any html or other languages. So what if, instead of creating the dom elements straight away we will keep a tree model of the dom made by javascript objects. It’s very easy to represent our DOM as a javascript object right? We saw that in the beginning.</p><p>So a better algorithm will be:</p><ul><li>Call render on the js tree model</li><li>Read the current dom</li><li>Figure out the changes</li><li>Apply only these changes on the dom</li></ul><p>This will make things faster. But we’re smarter than that. We want to keep our reads and writes to the dom to as minimal as we can. We already have a js tree model (let’s call it virtual dom from now) that reflects the way our dom should look like.</p><p>So we can have an even better algorithm. On re-render, the idea here is to have two virtual doms one always represents the current dom and the other will represent the future dom. On reRender:</p><ul><li>Create a new virtual dom</li><li>Figure out the difference between it and the current virtual dom (diffing between two js tress can be done pretty fast)</li><li>Apply only the changes on the real dom</li></ul><p>The process of diffing and merging the diffs into the real dom tree is called reconciliation. And this is how react really works.</p><p><strong>Fiber</strong></p><p>You might have heard about fiber. Fiber is the new reconciliation algorithm the guys at react are working on. It should be out soon. The idea behind fiber is simple. Up until now the reconciliation process was linear. Once it started you couldn’t stop it. Fiber aims to change that. The motivation for it is priorities. We realize there are some changes that are more important than others. For example, if you have a text node that needs to be change and a heavy animation to draw. It’s important that you will draw the animation first otherwise the user will notice the ui lagging. If the text node will change in a few ms delay the user won’t notice it. Fiber has a built in interrupt mechanism. Think of it as a pause button. At any given moment during the reconciliation. React can tell fiber to pause, do other calculations such as animations and then continue. This will cause our apps to look much more fluent.</p><p>The beauty of it all is that it’s totally transparent to the developer. The reconciliation algorithm is an implementation detail. We saw that with my own lousy implementation of it. The apps worked but not very well.. The simplicity of react’s api and the complexity of its implementation is what makes it such a powerful tool.</p><p>All the code you’ve seen on this post is available <a href="https://github.com/ofirdagan/build-your-own-react">here</a>. Catch me on twitter at <a href="https://twitter.com/ofirdagan2">@ofirdagan2</a> if you have any questions or just to say hi :)</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=48edb8ed350d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/build-your-own-react-48edb8ed350d">Build Your Own React</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The crowd-wisdom challenge [Results]]]></title>
            <link>https://medium.com/hackernoon/the-crowd-wisdom-challenge-results-9369db1dab82?source=rss-972258ae730e------2</link>
            <guid isPermaLink="false">https://medium.com/p/9369db1dab82</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[experimenting]]></category>
            <category><![CDATA[crowdsourcing]]></category>
            <category><![CDATA[fun]]></category>
            <category><![CDATA[experiment]]></category>
            <dc:creator><![CDATA[Ofir Dagan]]></dc:creator>
            <pubDate>Sun, 19 Mar 2017 12:58:23 GMT</pubDate>
            <atom:updated>2019-05-24T07:07:58.668Z</atom:updated>
            <content:encoded><![CDATA[<p>This is the follow-up post on <a href="https://hackernoon.com/the-crowd-wisdom-challenge-e6afacbdd73e#.fvi3rgy3j">The crowd-wisdom challenge post</a> I’ve posted on March 7th.</p><p>The real results just came in.. Out of the 145 people who have guessed during the last 7 days. Linas Medžiūnas and someone named ikajava were the closest to the correct answer which was….. drums… 35,905 seeds! Their guess was 35,000.</p><p>The <a href="https://hackernoon.com/tagged/experiment">experiment</a> didn’t go as well as I hoped though ;). The Median guess was 8500 and the Average was 158,319. The margin of error from the median is 76.3%. An interesting note. If I would remove 5.5% of the high and low guesses I’ll get an average of 37,513 which is 4.5% error margin. If I’ll only remove the high 5.5% I’ll get an average of 35,375 which is 1.47% error margin.. Not bad.</p><p>So, is <a href="https://hackernoon.com/tagged/crowd-wisdom">crowd-wisdom</a> really a thing? Depends on how you digest your numbers 😉.</p><p>In the histogram below you can see the distribution of the guesses. As expected from the median, most guesses were around the 8500 or log(3.93). I did the X axis based on the log of the guess so both high and low guesses would make it into the graph. They Y axis show the number of people who’ve guessed in this range.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TSfi44fr1uoBWh4LCfbKfA.jpeg" /></figure><h4>How did I count so many seeds?</h4><p>The truth is I didn’t ;). I counted two large groups. One of 1060 seeds and one of 1430 seeds. I weighed the first group and got 8.0g. Then I weighed the second group and got 10.8g. So If I wanted to know how many were there in the second group I would multiply 1060 with 10.8 and divide it by 8.0 which gets me 1431 instead of the real number 1430. I’ve decided this was close enough and then carried on with weighing the rest of the seeds.</p><figure><a href="http://bit.ly/HackernoonFB"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0hqOaABQ7XGPT-OYNgiUBg.png" /></a></figure><figure><a href="https://goo.gl/k7XYbx"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vgw1jkA6hgnvwzTsfMlnpg.png" /></a></figure><figure><a href="https://goo.gl/4ofytp"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gKBpq1ruUi0FVK2UM_I4tQ.png" /></a></figure><blockquote><a href="http://bit.ly/Hackernoon">Hacker Noon</a> is how hackers start their afternoons. We’re a part of the <a href="http://bit.ly/atAMIatAMI">@AMI</a> family. We are now <a href="http://bit.ly/hackernoonsubmission">accepting submissions</a> and happy to <a href="/service/mailto:partners@amipublications.com">discuss advertising &amp; sponsorship</a> opportunities.</blockquote><blockquote>If you enjoyed this story, we recommend reading our <a href="http://bit.ly/hackernoonlatestt">latest tech stories</a> and <a href="https://hackernoon.com/trending">trending tech stories</a>. Until next time, don’t take the realities of the world for granted!</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*35tCjoPcvq6LbB3I6Wegqw.jpeg" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9369db1dab82" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/the-crowd-wisdom-challenge-results-9369db1dab82">The crowd-wisdom challenge [Results]</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The crowd-wisdom challenge]]></title>
            <link>https://medium.com/hackernoon/the-crowd-wisdom-challenge-e6afacbdd73e?source=rss-972258ae730e------2</link>
            <guid isPermaLink="false">https://medium.com/p/e6afacbdd73e</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[experiment]]></category>
            <category><![CDATA[crowdsourcing]]></category>
            <category><![CDATA[fun]]></category>
            <category><![CDATA[experimenting]]></category>
            <dc:creator><![CDATA[Ofir Dagan]]></dc:creator>
            <pubDate>Tue, 07 Mar 2017 07:29:24 GMT</pubDate>
            <atom:updated>2019-05-24T07:06:31.735Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Dwi-dFD8-fQvlPlHcXBqBw.jpeg" /><figcaption>Flax seed jar</figcaption></figure><p>In 1906 a british scientist named Francis Galton went to a country fair where he saw a weight judging contest for a large ox. The participants paid money to put their wagers and try their luck guessing the ox weight. A total of 800 people took place in the contest. Of course none of them have guessed the correct weight of the ox. Some of them were way off either underestimating or overestimating the ox weight but what Francis found out was nothing less than amazing. He found that the median of the answers was 1207 pounds which was accurate within 1% of the true weight of 1198 pounds. I read about Francis’s findings recently and found it pretty impressive and so I decided to do a small <a href="https://hackernoon.com/tagged/experiment">experiment</a> of my own… Let’s see if the <a href="https://hackernoon.com/tagged/crowd-wisdom">crowd wisdom</a> works on guessing the number of flax seeds in a jar. Above is a picture of my flax seed jar. I put it next to a glass so you’ll be able to grasp its size. I’ve also created a very simple form where you can add your guess. Please don’t write your guess in the comments.</p><p><a href="https://goo.gl/forms/SfYJbEtTmbIBiMgM2">https://goo.gl/forms/SfYJbEtTmbIBiMgM2</a></p><p>I’ll post the answer in a few days once enough people will guess. The key here is crowd so feel free to share this post among your friends if you’re curious like me to find out if crowd-wisdom really works :)</p><p>Update 3/18/2017</p><p>Read all about the experiment’s results <a href="https://medium.com/@ofirdagan/the-crowd-wisdom-challenge-results-9369db1dab82#.rtqfr8qm4">here</a>.</p><figure><a href="http://bit.ly/HackernoonFB"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0hqOaABQ7XGPT-OYNgiUBg.png" /></a></figure><figure><a href="https://goo.gl/k7XYbx"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vgw1jkA6hgnvwzTsfMlnpg.png" /></a></figure><figure><a href="https://goo.gl/4ofytp"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gKBpq1ruUi0FVK2UM_I4tQ.png" /></a></figure><blockquote><a href="http://bit.ly/Hackernoon">Hacker Noon</a> is how hackers start their afternoons. We’re a part of the <a href="http://bit.ly/atAMIatAMI">@AMI</a> family. We are now <a href="http://bit.ly/hackernoonsubmission">accepting submissions</a> and happy to <a href="/service/mailto:partners@amipublications.com">discuss advertising &amp; sponsorship</a> opportunities.</blockquote><blockquote>If you enjoyed this story, we recommend reading our <a href="http://bit.ly/hackernoonlatestt">latest tech stories</a> and <a href="https://hackernoon.com/trending">trending tech stories</a>. Until next time, don’t take the realities of the world for granted!</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*35tCjoPcvq6LbB3I6Wegqw.jpeg" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e6afacbdd73e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/the-crowd-wisdom-challenge-e6afacbdd73e">The crowd-wisdom challenge</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Secret Life of Side Projects]]></title>
            <link>https://medium.com/@ofirdagan/the-secret-life-of-side-projects-32cf2af4d4e6?source=rss-972258ae730e------2</link>
            <guid isPermaLink="false">https://medium.com/p/32cf2af4d4e6</guid>
            <category><![CDATA[side-project]]></category>
            <category><![CDATA[life-lessons]]></category>
            <category><![CDATA[self-improvement]]></category>
            <dc:creator><![CDATA[Ofir Dagan]]></dc:creator>
            <pubDate>Wed, 16 Nov 2016 22:41:24 GMT</pubDate>
            <atom:updated>2016-11-16T22:46:28.859Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y7xN6khy9VPp-nSLTRNoyg.jpeg" /></figure><p>So you’re a ninja software engineer. You work at a really cool company with a lot of extremely smart peers. You really like your team and the project you’re working on but it’s not enough for a curious mind like yourself. You might want to expand your horizons and learn a new language / platform. It can be that you want to write something from scratch and do more than just developing. You’re interested in marketing and the product side of things and want a taste from these worlds as well. Last but not least last night while having beers with your mates you had a vision of the next big thing and you gotta write it!</p><p>Are any of these scenarios sounds familiar? This is exactly what happened to me. In the following blog posts I will share my “lessons learned” and two cents about how I was able to learn a new language, write an app, monitor it, market it, give a lightning talk in a conference and even write a blog post about it ;)</p><p>The fun part about side projects is that in order to implement these ideas you often need a different set of skills than what you have. It might be an app but you’re a web developer. It might be a website but you’re a backend developer. Whatever the case might be, you need to expand your knowledge and learn a set of skills.</p><p>The problem is time, right? With the excessive workload, the tight deadlines, the release of the next version, with spending quality time with your significant other, doing your sports, going out with friends. Ain’t nobody got time for that. Fear not my friends, not all is lost. Let me introduce you to the 3 rules of successful side projects. If you follow them, your path to a life full of side projects awaits.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*UfvzNB5gGWkFq4A56tVnaA.gif" /><figcaption>ain’t got time for this</figcaption></figure><h3>The 3 Rules of Successful Side Projects</h3><p>I’ve defined 3 base rules that any successful side project must meet. By successful I don’t mean financially wise. For me, a successful side project means I’ve accomplished my goals for this project and shipped it. Whether it’s on heroku / github / app store / conference talk or a blog post. It doesn’t really matter as long as I kept working on it long enough so other people can enjoy my work and learn from it. Unshipped side projects are where dreams go to die :)</p><p><strong>Rule #1 - Decide on a project</strong></p><p>While it seems obvious, it’s really not. Many times I’ve heard about a cool technology that I wanted to try out or a new framework. Most of the times doing the tutorial or a watch a youtube from the latest conference was enough. But if you really want to learn something new you have to get your hands dirty. Thinking on an Idea for a side project might be harder than it sounds. My tip? Keep a document with all kinds of side projects ideas. Then, when the time is right and you found a match between the project and the goal you want to achieve you can jump right to work.</p><p><strong>Rule #2 - Commit to a schedule</strong></p><p>Another key element in making a successful side project is a tight schedule and a routine. Along the years I’ve tried many different ways to tackle this problem. From a “pizza night” every week to a “once a month” weekend hackathon and up to “late night” coding sessions. The thing that by far worked best for me was to introduce a change in my daily routine. I started waking up 1 hour earlier each day and arrive to the office 20 min later than I usually would. Don’t get me wrong. I’m NOT a morning person. Quite the opposite I LOVE to sleep and the more the better. Nevertheless, for me waking up was game changer. In any of other methods I couldn’t persist for too long. One disclaimer though.. It doesn’t get any easier. It’s a pain in the ass to wake up early you don’t get used to it but it’s worth it.</p><p><strong>Rule #3 - Commit on shipment</strong></p><p>The last rule for a successful side project is commit for shipment. The thing that I found most effective for me is spending money. It can be on hiring a freelance designer / buying domain / buy ads on facebook or anything else you can think of. Spending money is a good indicator for showing serious intentions and it will make you do that extra mile which won’t let your side project slip away. Another thing I use is to commit to a hard deadline which I have no control of like giving a talk about a new technology that I’m checking out at a meetup.</p><p>In the next post I’ll dive into one of my latest side projects and how I came around doing it. Stay tuned 🙂.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32cf2af4d4e6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Enzyme Drivers — Readable React Native Components Tests]]></title>
            <link>https://medium.com/@ofirdagan/enzyme-drivers-readable-react-native-components-tests-7a5cf43c490c?source=rss-972258ae730e------2</link>
            <guid isPermaLink="false">https://medium.com/p/7a5cf43c490c</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[tdd]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Ofir Dagan]]></dc:creator>
            <pubDate>Mon, 14 Nov 2016 09:20:09 GMT</pubDate>
            <atom:updated>2016-11-14T09:20:09.180Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NBVKHz3cLZ1OeMD2XQc8oQ.png" /></figure><h3>Say What??</h3><p>Not long ago I’ve started writing mobile apps using the greatest and latest react native. Coming from angular, tests are a second nature for me. Writing TDD and clean code are not just fancy buzz words. It’s what we do.</p><p>React Native is awesome, no doubt about it. It gives you an amazing power for writing first class cross platform native apps. But (there’s always a `but` right?) when it comes to testing, react native still have a long way to go.</p><p>Airbnb did a great job and wrote enzyme. Enzyme is a tool for writing react based component tests. However, there is a lot of boilerplate involved especially in the react native world where you can’t render anything that is related to react native. Every react native based component must be mocked. We also faced some annoying things such as writing the relative paths to the components. Most importantly our tests looked full of messy selectors and they weren’t really readable.</p><p>So what did we do? <strong>Enzyme Drivers</strong> to the rescue.</p><p>Enzyme Drivers introduce a new way of organizing your enzyme based tests. Well, it’s not really new. Test drivers have been with us for a long time now and we like them a lot. Think about it as a cool way to organize your tests and make them more readable than ever.</p><p>Want to know how? keep reading.</p><p>First let’s install (yes I know… yet another npm package):</p><pre>npm install enzyme-drivers --save-dev</pre><p>For the sake of this example we’ll write a simple component that gets a text and a onTap handler as properties.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f5ce0da655814267f23866c1ab02c6c/href">https://medium.com/media/8f5ce0da655814267f23866c1ab02c6c/href</a></iframe><p>And add its test</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/afbf76ccc7070a49a4008da88a2c86b9/href">https://medium.com/media/afbf76ccc7070a49a4008da88a2c86b9/href</a></iframe><p>Looks neat right? The test is very readable and clear. Let’s write another one, let’s test that tap is working:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/356c8a231cf85e6259f77f267be5238e/href">https://medium.com/media/356c8a231cf85e6259f77f267be5238e/href</a></iframe><p>By now you might ask yourself what is this driver object? The driver is what helps us keep our tests nice and clean. It holds inside all of the nasty logic of selecting the right components and expose the stuff we really want to test.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3237bbc1c37beab565808ffd89f6d6e1/href">https://medium.com/media/3237bbc1c37beab565808ffd89f6d6e1/href</a></iframe><p>For more info check out the <a href="https://github.com/wix/enzyme-drivers">git repository</a> and if you liked what you just read give us some github stars love :)</p><p>P.S - If you’re writing your app in angular and you liked enzyme drivers. You might want to check out <a href="http://wix.github.io/turnerjs/">turnerjs</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7a5cf43c490c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>