<?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 Aaron Dail on Medium]]></title>
        <description><![CDATA[Stories by Aaron Dail on Medium]]></description>
        <link>https://medium.com/@adail_67917?source=rss-6d14a426854------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*N_fMbKBfeYgL0KjABCHNsg.jpeg</url>
            <title>Stories by Aaron Dail on Medium</title>
            <link>https://medium.com/@adail_67917?source=rss-6d14a426854------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 22:08:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@adail_67917/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[React Native at Tableau 3 Years in]]></title>
            <link>https://medium.com/tableau-engineering-blog/react-native-at-tableau-3-years-in-89d43c38354c?source=rss-6d14a426854------2</link>
            <guid isPermaLink="false">https://medium.com/p/89d43c38354c</guid>
            <category><![CDATA[tableau]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Aaron Dail]]></dc:creator>
            <pubDate>Thu, 10 Sep 2020 17:27:30 GMT</pubDate>
            <atom:updated>2023-03-24T16:26:00.962Z</atom:updated>
            <content:encoded><![CDATA[<p>Three years ago the Tableau Mobile team decided to rewrite our mobile app in React Native. A bit more than a year ago we released the new app on <a href="https://apps.apple.com/us/app/tableau-mobile/id434633927">iOS</a> and <a href="https://play.google.com/store/apps/details?id=com.Tableau.TableauApp">Android</a>. It has been a great experience. Using React Native has enabled us to ship more features, more often, with better quality. There have been challenges of course, but on balance it was the right decision for us.</p><p>In this blog post I want to share why we decided to use React Native, and what went well and what didn’t. Hopefully this will be useful to other teams out there considering React Native.</p><p>For a little context on the app, Tableau Mobile is a companion app to Tableau Online (Tableau’s cloud offering) and Tableau Server (Tableau’s on-prem offering) that lets you view your organization’s visualizations, dashboards, and metrics on your phone or tablet.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jL0e4L_Hq6IcqXwXs_DWNA.png" /></figure><h3>Where we were</h3><p>Back in early 2017 the mobile team had to make a decision: rewrite our app or undertake some pretty significant refactoring. We had shipped both iOS and Android apps and our users were generally happy, but developers on the team were unhappy working in the codebase and our feature development was painfully slow.</p><p>The core problem was how our architecture had developed. When we first started working on the app we adopted Xamarin. This was mainly due to it being the most promising cross-platform solution. React Native didn’t exist yet, and solutions like Cordova were too limited. Code-sharing was very important because we didn’t feel like we were a large enough team to successfully maintain two separate codebases.</p><p>Xamarin Forms wasn’t viable at that time, so we decided to implement the UI separately for both platforms and share all non-UI logic. Fatefully though, we decided to ship on iOS first and hold off on working on Android. That seemed like a reasonable choice at the time, but after we released the iOS app we went to do Android and found that a lot of the non-UI logic we hoped to share was much too heavily tied to iOS paradigms. While building the Android app we were constantly refactoring the supposedly sharable non-UI code to get things to work, and eventually we gave up on sharing large pieces.</p><p>By the time we released the Android app we had ended up with a codebase that had a lot of platform-specific code, some of which was very awkwardly structured in layers below the UI. The codebase was very time consuming to work in and we were not even really leveraging Xamarin’s cross-platform capabilities. You could have argued that writing two separate apps from the start would have been better.</p><p>Eventually we collectively felt like we couldn’t continue in this state and so we had to decide between rewriting the app, or doing some significant refactoring.</p><h3>Why did we rewrite in React Native?</h3><p>Generally when choosing between rewriting and refactoring the safe choice is refactoring because of all the well-known <a href="https://www.joelonsoftware.com/2000/04/06/things-you-should-never-do-part-i/">dangers of rewriting a codebase</a>.</p><p>However, that is a general rule and there are exceptions. We decided to rewrite because our problems ran so deep that we really did need to fundamentally rearchitect the app. Rewriting was the cheaper and more realistic way to do that.</p><p>Once we had decided to rewrite, that opened the door to choosing a different tech stack to build on. Xamarin had its fans on the team, but also its detractors. We debated rewriting with Xamarin, giving up on the cross-platform dream and going full native, and trying out this new kinda wild React Native thing people were talking about.</p><p>In the end we chose React Native for the following reasons.</p><h4>Developer Productivity</h4><p>First and foremost we were drawn to the promise of much faster iteration and higher productivity. We heard from other companies that things just went faster with React Native, and an initial React Native proof of concept app we wrote bore this out.</p><h4>Cross-Platform</h4><p>This was still very important to us. React Native promised to really let us be cross-platform with one codebase and a high level of code-sharing.</p><h4>Sharing Technologies with Web</h4><p>Being able to use the same technology and libraries that the web development teams at Tableau use, such as React, Redux and TypeScript, was a huge plus. Aside from the technologies themselves being great, it makes it easier for someone from the web world to come work on mobile, and lets us share knowledge and experience across both domains. Additionally the possibility of sharing code between mobile and the web was very appealing.</p><h4>Community and Ecosystem</h4><p>Even in 2017 the React Native community was large and thriving, tons of libraries were being created, and people were excited about it. This was really important; it gave us confidence that React Native was not going to just disappear and that we could find help when we needed it.</p><h4>Ease of Integrating with Native code</h4><p>We knew pretty much from the start that we would have to have some native code for certain features, so the ability to easily drop down to native when it was needed was a must have.</p><h3>Our Experience</h3><p>So we chose React Native and spent around a year and a half rewriting our app. We released the new app to the Apple App Store and Google Play Store in February 2019. Since then we have shipped new versions regularly, enjoyed a low crash rate, and have had lots of positive feedback from our users. Our usage has also grown by around 60%.</p><h3>What went well?</h3><h4>Developer Productivity</h4><p>Our team velocity is much higher now, and people are much happier working in the new codebase. One big reason for that is admittedly our new architecture, but it is also because of the great framework, tools, libraries, and especially the quick feedback cycle you get with React Native.</p><h4>Cross-Platform</h4><p>Maintaining feature parity between iOS and Android has been easy. Roughly 90% of our code, excluding authentication, is in shared TypeScript.</p><p>The reason for the qualification there about authentication is that we split out most of our authentication code into two separate native modules, one for each platform. Our app has to authenticate not only to our Tableau cloud-hosted servers, but also to servers hosted by our customers. There are lots of complicated scenarios we have to handle due to the variety of different authentication mechanisms that customers can use (SAML, NTLM/SSPI/Kerberos, OpenID, etc), and the fact that the servers can be behind corporate firewalls and reverse proxies. Very early on in the rewrite we realized that dealing with these authentication scenarios would require a lot of native code, very little of which would be related to the UI. So it made sense to split that code out.</p><h4>Sharing Technologies with Web</h4><p>There has been a lot more knowledge sharing thanks to using shared libraries and technologies. For example, when designing our architecture we leveraged the experience other teams at Tableau had had with libraries like redux-thunk and redux-saga.</p><p>We haven’t ended up sharing that much code between web and mobile, but we are making some headway there.</p><h4>Community and Ecosystem</h4><p>People were and still are excited about building things with React Native and the community is going strong.</p><p>There are many amazing community projects. We have saved developer years, maybe decades, because of these projects. Often you can just think of something you need, search for it, and find that someone has already implemented it and open-sourced it. The community is great and pretty responsive.</p><p>We have also open sourced a <a href="https://github.com/tableau/react-native-dns-lookup">small library for DNS lookups</a>, contributed back a bunch of PRs to various projects, and have aspirations to do more in the future.</p><p>When we started we were a bit worried about the possibility of Facebook losing interest in the project, but that hasn’t been the case. They have invested a lot in maturing and updating the framework and moving it forward, and they are more transparent now than when we started.</p><p>That said, on occasion we have taken a dependency on a project that appeared active only to have the maintainer switch jobs or lose interest and let the project stagnate. This has led to some fretting and at least one time-consuming migration.</p><p>As a brief aside, one great thing we found is that when there are bugs in our open source dependencies, we can just use <a href="https://github.com/ds300/patch-package">patch-package</a> to fix them locally. This way we aren’t blocked waiting on issues to get fixed or pull requests to get accepted, and we don’t have to resort to forking. This was not an option for third-party libraries consumed via NuGet with Xamarin.</p><h4>Ease of Integrating with Native Code</h4><p>As mentioned we have a good amount of native code that handles authentication, and a small amount for other miscellaneous things. We even use Swift and Kotlin. Exposing this native code to the JavaScript side of the app is well documented and has been a breeze for us.</p><h4>Declarative UI w/ React</h4><p>The React way of writing declarative UI has proven to be really effective for everyone on the team. Our UX designer can even work directly in the code sometimes to get the UI exactly the way they want. This is quite a bit different from the traditional back-and-forth workflow we had before, where the designer would create redlines, the developer would attempt to implement them, then the designer would update the redlines, the developer would update the code, etc etc.</p><h4>Redux</h4><p>When we started the rewrite we made <a href="https://redux.js.org/">Redux</a> a core part of our architecture, and it has proven to be a great choice. It’s a somewhat unfamiliar architecture pattern at first, but once it clicks it just makes everything easier to understand. Having all state management and business logic in a consistent pattern really simplifies things. It also makes development easier in ways you might not expect, like being able to easily log any state changes in the app, or even log the entire state of the app.</p><h4>TypeScript</h4><p>We decided very early to use <a href="https://www.typescriptlang.org/">TypeScript</a>, and this has also worked out very well. Most of the web development at Tableau happens in TypeScript because it makes working in large codebases with large teams significantly easier. A lot of the React Native community uses JavaScript, so we were worried about that initially, but in practice it hasn’t been an issue. There are TypeScript type definitions for most of the libraries we use and actually quite a few libraries are written in TypeScript now.</p><h4>Tools</h4><p><a href="https://code.visualstudio.com/">VS Code</a> is a great IDE and won over basically everyone who came to it from Xcode, Android Studio, or Xamarin Studio. We are also particularly big fans of <a href="https://github.com/infinitered/reactotron">Reactotron</a>, which gives you a few methods to inspect and manipulate your app when it is running without having to activate the debugger.</p><p>The debugging tools for React Native are really powerful though there are some occasional pain points we hit. Some issues aren’t really React Native’s fault, like how stepping through async/await JavaScript code in the debugger doesn’t work well, but others have been, like how the JavaScript engine can behave differently in some cases when <a href="https://medium.com/@suyogkrazz/you-should-not-always-rely-on-remote-debugging-react-native-787a850c7ad8">debugging vs. not debugging</a>.</p><h4>Testing</h4><p>With React Native there are some great tools and libraries that make it easy to add automated tests. <a href="https://jestjs.io/">Jest</a> takes a lot of the pain out of writing unit tests. We have unit tests covering just about all of our TypeScript code. We also use both <a href="https://airbnb.io/enzyme/">enzyme</a> and <a href="https://github.com/callstack/react-native-testing-library">react-native-testing-library</a> in different cases for testing our React component rendering and behavior.</p><p>For on-device integration testing we have started to use <a href="https://cavy.app/">Cavy</a>, and so far it has filled the gap between our Jest tests and E2E tests nicely. We do still have a small suite of Appium E2E tests, but we rarely add new ones due to the cost to write and maintain them.</p><h3>What were the biggest Challenges?</h3><h4>Ramp Up</h4><p>The more familiar you are with web technologies, web development, and JavaScript, the easier React Native will be to pick up. The less familiar you are, the harder it will be. The learning curve can be very steep, even for developers that have a lot of industry experience. This is due to having to understand: JavaScript and all its quirks as a language, TypeScript which mitigates some of those quirks but introduces its own ramp-up cost, new and different coding patterns and practices common in JavaScript projects, how React works and renders UI, what Redux does and how to use it, how React Native connects to Native code, and all the different new tools you will be using for development. It is a lot of stuff to take in before you can be, and feel, productive.</p><p>Developers new to mobile will also have to learn about native iOS and Android development, as React Native doesn’t completely shield you from it in our experience.</p><h4>Living Further Away from Native</h4><p>Using new flashy platform-specific stuff from Apple or Google can require finding third-party libraries or writing a React Native wrapper for it yourself. You don’t always get them for free, at least not quickly.</p><p>Additionally, cross-platform UI development requires some level of genericism, and, to be honest, compromise at times.</p><p>This is something worth accepting rather than fighting. Use third-party libraries where you can and try to keep things less complex. When you want the latest and greatest platform-specific feature allow some time for the community to support it, or support the community by filing issues and submitting PRs. Doing these things will let you share a lot more code and you will be leaning on React Native’s strengths.</p><h4>Navigation</h4><p>There is no first-party navigation functionality built into React Native, and the closest thing is <a href="https://reactnavigation.org/">react-navigation</a>. Which is a great library and we make heavy use of it, but it has struggled at times to seamlessly accommodate new things like the iPhone X notch and display cutouts on Android. We have invested a lot of time fixing issues here. It does seem like things have improved over time, but navigation is so fundamental to building a mobile app of any complexity that as a framework React Native feels incomplete without support for it.</p><p>Getting modals to look great on various form factors (both phones and tablets), resize appropriately (due to orientation changes as well as split screen), and accommodate things like the notch correctly has also been a big pain.</p><h4>Upgrading</h4><p>Dealing with upgrading React Native varies, in terms of enjoyability, between “boring chore” and “getting a cavity filled sans anesthesia”.</p><p>Overall it has gotten better since we adopted React Native, but the upgrades have rarely gone completely smoothly. We usually end up investing about 2 weeks of developer time on doing the upgrade and then trying to fix whatever broke. We try to upgrade every 3 months so we don’t lag too far behind, since the longer you wait the harder it tends to be. Using <a href="https://react-native-community.github.io/upgrade-helper/">React Native Upgrade Helper</a> has been a huge timesaver so take advantage of that.</p><p>Additionally, we have a large number of dependencies apart from React Native itself. We try to update them every 3 months, but offset from the React Native upgrades by 6 weeks. These upgrades are more consistently at the “boring chore” level of enjoyability, but the sheer number of dependencies still makes the task pretty time-consuming.</p><h3>Summary</h3><p>Choosing a new framework on its own won’t build a great app. A lot of the success we had rewriting our app was admittedly due to rearchitecting, but moving to React Native was also a huge factor. It was a very good choice for the Tableau mobile team, and we would be struggling to get to where we are today without it. While every team and project is different, we would definitely recommend it.</p><p>May your journey be as fruitful as ours.</p><figure><a href="https://www.salesforce.com/form/other/privacy-request/"><img alt="" src="https://cdn-images-1.medium.com/max/100/1*ImBy4Z_J1PCcv4T0kP9DJg.png" /></a><figcaption>Your Privacy Choices</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=89d43c38354c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tableau-engineering-blog/react-native-at-tableau-3-years-in-89d43c38354c">React Native at Tableau 3 Years in</a> was originally published in <a href="https://medium.com/tableau-engineering-blog">Tableau Engineering Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Native Performance Learnings]]></title>
            <link>https://medium.com/tableau-engineering-blog/react-native-performance-learnings-85f47ba38acb?source=rss-6d14a426854------2</link>
            <guid isPermaLink="false">https://medium.com/p/85f47ba38acb</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[tableau]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[performance]]></category>
            <dc:creator><![CDATA[Aaron Dail]]></dc:creator>
            <pubDate>Wed, 05 Aug 2020 19:51:18 GMT</pubDate>
            <atom:updated>2023-03-24T16:27:32.682Z</atom:updated>
            <content:encoded><![CDATA[<p>In mobile apps, performance is critical for a good user experience. At Tableau we have been using React Native for three years now, and we have learned a lot about getting good native-like performance with it. For the most part it doesn’t take much effort, as long as you have an understanding for how things work, follow some good patterns, and avoid some pitfalls. In this article we share the most important things we have learned.</p><h3>Understand React Native Performance Basics</h3><p>React Native maintains a good <a href="https://reactnative.dev/docs/performance.html">page on performance</a> with a wealth of information. Make sure you are somewhat familiar with all of it, though not everything applies to every project (we have never had to enable RAM bundles or use inline requires for example).</p><p>A couple things that are mentioned on that page are worth emphasizing here. First performance problems should be checked on a real device with a release build. Especially on Android. The difference between the Android emulator and a real device, and between a debug and release build, can be enormous. Secondly, if you are doing any kind of animation, make sure to enable <a href="https://facebook.github.io/react-native/blog/2017/02/14/using-native-driver-for-animated.html#how-do-i-use-this-in-my-app">useNativeDriver</a> in the animation. It can make a huge difference.</p><h3>Understand Reconciliation</h3><p>The most common performance problem we have had is when the JavaScript side of the app is bogged down while the native side isn’t. This means that native UI elements are responsive (scrolling is fine, tap animations are fine, etc) but the app otherwise feels unresponsive or laggy. This is because the JavaScript thread is too busy processing something and not responding to user interactions.</p><p>When we have hit this it has always been related to rendering some expensive React component, or a ton of simple React components, too often and unnecessarily.</p><p>React also has a good <a href="https://reactjs.org/docs/optimizing-performance.html">page about performance</a> that is worth absorbing. A lot of the page isn’t all that relevant to React Native, but the parts on <a href="https://reactjs.org/docs/optimizing-performance.html#avoid-reconciliation">Avoiding Reconciliation</a> and the <a href="https://reactjs.org/docs/optimizing-performance.html#profiling-components-with-the-devtools-profiler">React Profiler</a> are (more on the profiler below).</p><p>Briefly, reconciliation is the process React uses to decide how to update the underlying DOM (React web) or native UI elements (React Native) based on what is rendered by your JavaScript components. Read the React <a href="https://reactjs.org/docs/reconciliation.html">documentation on reconciliation</a> for more details.</p><p>In React Native we have found that we really benefit from avoiding reconciliation, more-so than in web apps. Avoiding reconciliation translates to the JavaScript thread spending much less time on rendering, freeing it up to be responsive to user interactions.</p><h3>Use PureComponent (or React.memo)</h3><p>To avoid unnecessary rendering and reconciliation we made it a rule that all React components we write should derive from PureComponent. We currently only use class-style components, so if you are writing functional components and using hooks you can use <a href="https://reactjs.org/docs/react-api.html#reactmemo">React.memo</a> to get the same behavior.</p><p>All PureComponent does is prevent rendering in cases where the props or state haven&#39;t changed. It detects changes via a simple shallow equality comparison: iterating over all the keys of the old and new props or state objects, and then doing a strict equality comparison on each pair of values. If there is a change, the component is re-rendered, but if there is no change it won’t be re-rendered and (importantly) neither will all its children.</p><p>You will find advice out there suggesting that you should be more nuanced in your use of PureComponent, but as a blanket rule it has worked out well for us at Tableau. Aside from reducing the overall amount of rendering that happens it also has the nice effect of making it easier to tell why a component is being rendered too often, since you know it should only be doing so because of changes to its props or state.</p><h3>Understand how react-redux works</h3><p>Redux is a core part of our architecture in the Tableau mobile app, and if the same is true for your app you should understand how changes to your Redux state tree cause React components to update. We haven’t started using hooks yet so we are going to primarily be talking about mapStateToProps, but most of what we say should apply to the newer useSelector hook (though it is not exactly the same, as noted below).</p><p>The most important thing to understand is that your mapStateToProps (or useSelector) functions will be run <em>every time</em> a Redux action is dispatched.</p><p>That means that presentational components (<a href="https://redux.js.org/basics/usage-with-react/">in redux terminology</a>) which get their props from mapStateToProps (or useSelector) may be re-rendered every time any action is dispatched.</p><p>Now react-redux will try to avoid rendering the presentational components by comparing the new result of mapStateToProps with the prior result, doing a shallow equality comparison similar to how PureComponent works, but it is easy to break this optimization and cause lots of unnecessary rendering if you aren’t careful about what you return.</p><p>Note thatuseSelector does not do a shallow comparison on objects, rather just a simple === comparison, so you should definitely <a href="https://react-redux.js.org/api/hooks#equality-comparisons-and-updates">read up on how to use it</a>.</p><h3>Avoid creating new objects, arrays, or functions during rendering and in mapStateToProps</h3><p>It is easy to accidentally break the shallow equality checks that both PureComponent and mapStateToProps use to avoid unnecessary rendering by always creating new objects, arrays, or functions.</p><p>A simple silly example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/802e6942b53dec602e15f4eedde3d3bb/href">https://medium.com/media/802e6942b53dec602e15f4eedde3d3bb/href</a></iframe><p>In this example each of the three props getting passed to MyPureComponent would, by themselves, break PureComponent. Meaning that whenever the parent component re-renders, MyPureComponent will also re-render.</p><p>This is because of how the shallow equality checking works. For objects, arrays, and functions it is checking whether the new prop is the same <em>instance</em> as the old prop. In the example above, the props are always new instances so the equality check with the old props will always fail.</p><p>Not avoiding this is quite a bit more risky in the mapStateToProps case than in the PureComponent case. Often, a component isn&#39;t individually expensive to render so you may not ever notice a problem, but because mapStateToProps gets executed so often it can trigger lots of unnecessary rendering.</p><p>In some cases, you can solve this by turning the objects, arrays, and functions into constants or otherwise pulling them out of the function so they aren’t being recreated all the time. You can also avoid passing objects as props. For example instead of passing an object with two properties as a single prop, just create two different props.</p><p>Memoization is a good solution too, since memoized functions will compute the result (i.e. the array or object) once and cache it. Subsequent calls to the memoized function will just return the cached result. That means it will be the same instance as whatever was previously returned, and thus it won’t fail a shallow equality check. Consider using lodash’s <a href="https://lodash.com/docs/4.17.15#memoize">memoize</a> function, <a href="https://github.com/reduxjs/reselect">reselect</a>, <a href="https://github.com/toomuchdesign/re-reselect">re-reselect</a>, as well as React’s <a href="https://reactjs.org/docs/hooks-reference.html#usememo">useMemo</a> if you use hooks.</p><h3>Avoid doing repeated expensive computations when rendering and in mapStateToProps</h3><p>This may be obvious but you don’t want to be doing anything remotely expensive <em>repeatedly</em> during rendering, and similarly in mapStateToProps since that gets run on every redux action. There are a variety of ways to avoid this. If you have a class component you can move the computation to your constructor (or componentDidMount), though make sure you recompute it if you need to when the props change. You could also do the computation asynchronously by using the React Native <a href="https://reactnative.dev/docs/interactionmanager.html">InteractionManager</a>’s runAfterInteraction function or the standard setTimeout function and storing the result in the state. You can also use memoization.</p><h3>Understand how caching works in memoized functions</h3><p>To get the benefit of memoized functions you have to understand how they cache values. You can accidentally cause a memoized function to recompute the cached value every time it is called, which defeats the whole purpose of memoization. The behavior varies by library, but we’ll cover a few problems we have hit here.</p><p>Selectors created by <a href="https://github.com/reduxjs/reselect">reselect</a> have a cache size of one. This means that whenever they are called with different arguments they will recompute the result. So if a selector is being called often with different arguments it will not be effectively using its cache and will be creating new objects all the time. <a href="https://github.com/toomuchdesign/re-reselect">re-reselect</a> was created to address this problem.</p><p>Also for both reselect and re-reselect you will want to make sure that the functions that you pass to createSelector or createCachedSelector <em>before</em> the computation function (e.g. F1 to FN-1 in createSelector(F1, F2, ..., FN-1, FN), FN being the computation function) narrow down the inputs to the computation function to what it really needs.</p><p>For a contrived example, compare these two selectors created using reselect:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be5cb95f94d588942358052f1c217931/href">https://medium.com/media/be5cb95f94d588942358052f1c217931/href</a></iframe><p>These two selectors are very similar, and compute the same result, but the second function will only recompute its result when stateTree.allFoos changes (specifically when it is a new array). The first function will recompute its result whenever stateTree changes, producing a new array. This will mean that any PureComponent relying on the result of getAllFooNames1 will re-render a lot more often than you&#39;d expect.</p><p>If you are using lodash’s memoize function, make sure you aren&#39;t calling memoize itself in during rendering or in mapStateToProps. Every time you call memoize it returns a new function with a new (empty) cache. So you will want to store that created function somewhere.</p><h3>Avoid passing props unnecessarily through multiple layers of components</h3><p>This can also break PureComponent. You might have a component high up in your component tree that sets a prop (e.g. &quot;isRefreshing&quot;) and passes that down to its child components. The child components don&#39;t use it, but pass the prop on down to their own children. Repeat that a few times, and finally you get to the components deep in the tree that care about the prop.</p><p>The issue is that when the prop does change all the components in the tree have to be re-rendered, even though most of them don’t even use the prop.</p><p>You can fix this a couple ways. If that prop is coming from redux (via a mapStateToProps function), consider just turning the components that need the prop into container components so they can retrieve it from redux themselves.</p><p>A different solution would be to use the <a href="https://reactjs.org/docs/context.html">React context API</a>. The parent component can create a context and put the prop in there. Then the components that care about the prop can just use the context to get the value.</p><h3>Use the built-in profiler and the React profiler</h3><p>Even if you follow all sorts of good practices you can still end up with a performance problem. When you do need to dive deeper, there are some good tools to use.</p><p>The built-in profiler (the thing you see floating over your UI when you select “Show Perf Monitor” from the developer menu) isn’t all that comprehensive but it will give you a quick feel for whether a performance problem is happening on the JavaScript side of the app or the native side.</p><p>If you think you have a problem with a specific component frequently re-rendering you can run your app with a debugger attached and try some targeted uses of console.count in render functions. If you think some of your render functions are too expensive you could try using console.time to time them.</p><p>That said, <a href="https://www.npmjs.com/package/react-devtools">react-devtools</a> lets you use the great React profiler and it will probably help you more quickly identify your problem.</p><p>The <a href="https://reactjs.org/blog/2018/09/10/introducing-the-react-profiler.html">React profiler</a> will tell you in great detail where React is spending time rendering across the whole app, and it works in React Native. It also makes it easier to test changes and be confident that you have fixed the problem.</p><p>Note that you may have to use a slightly older version of the react-devtools if you aren’t on React Native 0.62 yet:</p><p>npm install -g react-devtools@^3</p><p>It probably is obvious but things to look out for are commits that took a long time and which components took the longest time to render in those commits. <a href="https://reactjs.org/blog/2018/09/10/introducing-the-react-profiler.html#flame-chart">The Flame chart</a> can give you a good overall picture of what components took the most time to render, while <a href="https://reactjs.org/blog/2018/09/10/introducing-the-react-profiler.html#ranked-chart">the Ranked chart</a> is good for finding problems in a particular commit. Also if you are using PureComponent as a rule, also look for components that seem to be re-rendered a lot when they shouldn&#39;t be.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fKCqM6X6bYUR0LEOO1UEZw.png" /><figcaption>The React profiler, with the Flame chart on the left and the Ranked chart on the right.</figcaption></figure><h3>Writing Rendering Tests</h3><p>Finally, if you have gone to the trouble of identifying and fixing a rendering problem it would be good to write a unit test to prevent it from recurring. This isn’t practical in all cases, but if you are using PureComponent it can be pretty easy. Being able to do this easily is a great strength of React Native and the ecosystem.</p><p>At Tableau we use <a href="https://github.com/callstack/react-native-testing-library">react-native-testing-library</a> to do this because it supports deep rendering (enzyme only supports shallow rendering on React Native).</p><p>For class-based components, writing such a test basically involves rendering either the component you want to test directly or a component that contains the one you want to test, spying on the render function of the component you want to test, triggering some action or calling a method that might cause re-rendering to happen, and finally checking whether re-rendering happened using the spy.</p><p>Here is an example test that makes sure dispatching a random Redux action doesn’t accidentally trigger re-rendering on MyPresentationalComponent:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/722b6683c1b3026f3ec6b991f02f00fd/href">https://medium.com/media/722b6683c1b3026f3ec6b991f02f00fd/href</a></iframe><h3>Wrapping up</h3><p>Well, that is a lot to take in. Hopefully this will be useful to you when getting started on a new React Native app or dealing with performance problems in your current one. Getting native-like performance with React Native is, in our experience at Tableau, in large part just understanding how things work, and knowing what patterns to use and which pitfalls to avoid.</p><figure><a href="https://www.salesforce.com/form/other/privacy-request/"><img alt="" src="https://cdn-images-1.medium.com/max/100/1*ImBy4Z_J1PCcv4T0kP9DJg.png" /></a><figcaption>Your Privacy Choices</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=85f47ba38acb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tableau-engineering-blog/react-native-performance-learnings-85f47ba38acb">React Native Performance Learnings</a> was originally published in <a href="https://medium.com/tableau-engineering-blog">Tableau Engineering Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>