<?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 Juan M Rodriguez on Medium]]></title>
        <description><![CDATA[Stories by Juan M Rodriguez on Medium]]></description>
        <link>https://medium.com/@7ynk3r?source=rss-f9347ecd616d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*wzYIWanctqb6BYjY.jpeg</url>
            <title>Stories by Juan M Rodriguez on Medium</title>
            <link>https://medium.com/@7ynk3r?source=rss-f9347ecd616d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 09:15:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@7ynk3r/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 UI Testing with Appium on Sauce Labs using Jest]]></title>
            <link>https://medium.com/@7ynk3r/react-native-e2e-testing-with-appium-on-sauce-labs-8920195b8a54?source=rss-f9347ecd616d------2</link>
            <guid isPermaLink="false">https://medium.com/p/8920195b8a54</guid>
            <category><![CDATA[appium]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[sauce-labs]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Juan M Rodriguez]]></dc:creator>
            <pubDate>Mon, 30 Jul 2018 03:10:32 GMT</pubDate>
            <atom:updated>2018-07-31T20:01:02.446Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article I’ll cover how to achieve UI/E2E testing for react-native using Appium and jest.</p><p>Before we start you might be thinking, why Appium? what are the alternatives? Let’s see a list of alternatives tools:</p><p><strong>Cross-platform</strong></p><ul><li>Cavy</li><li>Detox</li></ul><p><strong>Platform-specific</strong></p><ul><li>iOS: xCode UI Testing (has an amazing recorder)</li><li>Android: Expresso &amp; UIAutomator</li></ul><p>Appium in the only tool that have a decent support for both Android and iOS. It’s also the most mature compared to Cavy and Detox. It uses a web driver similar to Selenium which is a plus if you’re already familiar with web development.</p><p>As part of this article I’ve wrote a code sample. You can find it at <a href="https://github.com/7ynk3r/react_native_appium">https://github.com/7ynk3r/react_native_appium</a></p><p>Let’s get started!</p><h3>Setup</h3><p>Assuming that you have a react-native project up and running, the only thing you have to do is:</p><pre>yarn add --dev wd appium appium-doctor</pre><ul><li>wd is the web driver library that we’ll use to interact with appium.</li><li>appium is the server that will send our commands to the application.</li><li>appium-doctor is a tool to verify appium installation. If you know what you’re doing you might not need this.</li></ul><h4>Tests</h4><p>Now we have to actually write some tests, and this is the template for them:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31ff2a2551492d74b815d01efa41bab7/href">https://medium.com/media/31ff2a2551492d74b815d01efa41bab7/href</a></iframe><p>There’re two things we have to configure</p><ul><li>SERVER_URL: this will be localhost for the local Appium sever. For Sauce Labs will be https://[USER_NAME]:[USER_ACCESS_KEY]<a href="http://twitter.com/ondemand">@ondemand</a>.saucelabs.com:443/wd/hub where USER_NAME and USER_ACCESS_KEY are available at <a href="https://saucelabs.com/beta/user-settings">https://saucelabs.com/beta/user-settings</a></li><li>CAPABILITIES: This might look like a simple step but it might not work and the errors you obtain from the driver won’t tell much in most cases. We’ll see this in detail in the next section.</li></ul><p>Other things present in the test</p><ul><li>jasmine.DEFAULT_TIMEOUT_INTERVAL : Without this we’ll run into errors with async functions timing out.</li></ul><h3>Local Server Capabilities</h3><p>These are the most important ones:</p><ul><li>platform: Just use iOS/Android.</li><li>deviceName: to device we want to use for testing. For iOS, use instruments -s devices to get all devices. For Android, use Android Emulator and start your favorite emulator.</li><li>app: <strong>absolute</strong> local path or remote http URL to an .ipa or .apk file, or a .zip containing one of these. If you use .app remember to zip it.</li></ul><p>To create an .app or .apk use the following</p><ul><li>iOS: react-native run-ios --configuration Release</li><li>Android: react-native run-android --variant=release</li></ul><p>For more information about creating releases <a href="https://facebook.github.io/react-native/docs/running-on-device">https://facebook.github.io/react-native/docs/running-on-device</a></p><h4>Example — iOS</h4><pre>platformName: &#39;iOS&#39;,<br>deviceName: &#39;iPhone X&#39;,<br>automationName: &#39;XCUITest&#39;,<br>app: &#39;[ABSOLUTE_APP_PATH]/ios/build/Build/Products/Release-iphonesimulator/[APP_NAME].app.zip&#39;,</pre><p><strong>Note</strong>: we need to add automationName because the default value UIAutomator2 doesn’t work for xCode 9.4 or greater.</p><h4>Example — Android</h4><pre>platformName: &#39;Android&#39;,<br>deviceName: &#39;Android Emulator&#39;,<br>app: &#39;[ABSOLUTE_APP_PATH]/android/app/build/outputs/apk/app-release.apk&#39;,</pre><h3>Sauce Labs Capabilities</h3><p>Similar to the local server capabilities but with some differences</p><ul><li>platformVersion: This is optional while running on a local server but it’s required for Sauce Labs.</li><li>app: remove path to the applications. We have to options: upload the app to a remove server and use a public URL, or upload the app to Sauce Labs and use sauce-storage:[APP_NAME]</li></ul><p>To upload your app to Sauce Labs run:</p><pre>curl -u &quot;[USER_NAME]:[USER_ACCESS_KEY]&quot; -X POST -H &quot;Content-Type: application/octet-stream&quot; <a href="https://saucelabs.com/rest/v1/storage/[USER_NAME]/[APP_NAME].apk?overwrite=true">https://saucelabs.com/rest/v1/storage/[USER_NAME]/[APP_NAME].apk?overwrite=true</a> --data-binary @[ABSOLUTE_APP_BUILD_PATH]</pre><p>To see all your uploads</p><pre>curl -u &quot;[USER_NAME]:[USER_ACCESS_KEY]&quot; <a href="https://saucelabs.com/rest/v1/storage/[USER_NAME">https://saucelabs.com/rest/v1/storage/[USER_NAME</a>]</pre><h4>Example — iOS</h4><pre>platformName: &#39;iOS&#39;,<br>platformVersion: &#39;11.0&#39;,<br>deviceName: &#39;iPhone X&#39;,<br>automationName: &#39;XCUITest&#39;,<br>app: &#39;sauce-storage:app-release.app.zip&#39;,</pre><h4>Example — Android</h4><pre>platformName: &#39;Android&#39;,<br>platformVersion: &#39;7.1&#39;,<br>deviceName: &#39;Android GoogleAPI Emulator&#39;,<br>app: &#39;sauce-storage:app-release.apk&#39;,</pre><p>For more information about capabilities visit <a href="http://appium.io/docs/en/writing-running-appium/caps/#ios-only-using-xcuitest">http://appium.io/docs/en/writing-running-appium/caps/</a></p><h3>Tips</h3><ul><li>accessibilityLabel: To assign an id to an element to find it easily. You’d like to use testID instead but it’s not supported on Android (yet). Be careful with accessibilityLabel if you’re making your app accessible, they won’t play well. In that case create a wrapper function that depending on the context (testing or not) will return a different accessibility identifier.</li><li>Appium Desktop App: Really useful for debugging our tests and inspecting elements.</li></ul><h3>Conclusions</h3><ul><li>A huge advantage of Appium over other tools is that you can create and run your test using jest.</li><li>This is not rocket science but it requires severals steps to have your tests running.</li><li>Appium errors won’t help to know what is wrong in many occasions.</li><li>Performance isn’t great, specially on iOS. Make sure to cover most of your code with unit tests.</li><li>From version to version Appium will break your tests. Find a combination of iOS, Android, xCode, Appium that works and stay there</li></ul><h3>References</h3><ul><li><a href="https://www.polidea.com/blog/how-to-apply-ui-test-automation-in-react-native-apps/">https://www.polidea.com/blog/how-to-apply-ui-test-automation-in-react-native-apps/</a></li><li><a href="https://wiki.saucelabs.com/display/DOCS/Viewing+and+Managing+Test+Results">https://wiki.saucelabs.com/display/DOCS/Viewing+and+Managing+Test+Results</a></li><li><a href="https://medium.com/front-end-hacking/how-to-do-end-to-end-e2e-testing-for-react-native-android-using-jest-and-appium-27d75e4d831b">https://medium.com/front-end-hacking/how-to-do-end-to-end-e2e-testing-for-react-native-android-using-jest-and-appium-27d75e4d831b</a></li><li><a href="https://github.com/garthenweb/react-native-e2etest">https://github.com/garthenweb/react-native-e2etest</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8920195b8a54" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Testing Done Right]]></title>
            <link>https://medium.com/@7ynk3r/react-testing-done-right-24fdb4ef43d8?source=rss-f9347ecd616d------2</link>
            <guid isPermaLink="false">https://medium.com/p/24fdb4ef43d8</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[enzyme]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Juan M Rodriguez]]></dc:creator>
            <pubDate>Mon, 30 Oct 2017 21:15:22 GMT</pubDate>
            <atom:updated>2018-07-19T21:22:28.942Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*FI4XsUzMf1M7neXc1sDceA.png" /></figure><p>Testing is always a problem that we have to face sooner or later if we want our software to be more than a prototype.</p><p>React components have some peculiarities that make testing them different from ordinary classes. People tend to create complicated tests with numerous libraries while react and jest provide all the tools (renderers, snapshots, coverage, …) that we need to deal with this peculiarities and more.</p><p>In this article, I’ll present a series of concepts regarding testing best practices together with examples. At the same time, I’ve created a project containing all the code presented here and more: <a href="https://github.com/7ynk3r/react_test">https://github.com/7ynk3r/react_test</a></p><h4>Context</h4><p>To start, let’s say that we want to render a shopping cart component using the following data</p><pre>const cart = {<br>  user: {<br>    name: &#39;joe&#39;,<br>    email: &#39;<a href="/service/mailto:joe@mail.com">joe@mail.com</a>&#39;,<br>  },<br>  items: [{<br>    id: 1,<br>    name: &#39;item 1&#39;,<br>    price: 1,<br>  },{<br>    id: 2,<br>    name: &#39;item 2&#39;,<br>    price: 2,<br>  }],<br>};</pre><p>Let’s say as well that I’d like to include the <strong>total price</strong> for all the items and a way to <strong>delete an item</strong> from the list.</p><p>With this in mind, let’s deep into the sections.</p><h3>Use Multiple Components</h3><p>While we can solve the shopping cart problem in a single component’s render function, it’s a good idea to split the logic into smaller and simpler components that are easy to solve and test.</p><p>Looking at the payload for cart we can identify a few the components: Cart, User, Items, Item, Name, and Price. And looking at the additional requirements we can see we’ll need Total for total price and Delete to delete an item from the list.</p><p>Let’s see how Cart would look like</p><pre>const Cart = ({ cart }) =&gt; (<br>  &lt;Container&gt;<br>    &lt;User user={ cart.user } /&gt;<br>    &lt;Items items={ cart.items } /&gt;<br>  &lt;/Container&gt;<br>);</pre><blockquote>This is function component: has no state and is purely driven by the props it receives. If you want to learn more about function components, take a look at <a href="https://reactjs.org/docs/components-and-props.html#functional-and-class-components">https://reactjs.org/docs/components-and-props.html#functional-and-class-components</a></blockquote><p>This is simple and clean, and yet brings a question: can cart be null or undefined? what should we render in that case? what about cart.items? These are the kind of questions we have to ask ourselves while testing.</p><h3>One Component One Test</h3><p>Let’s take a look at another component, in this case, the more complex Items. This should render each Item in items and Total.</p><pre>class Items extends React.Component {<br>  constructor(props) {<br>    super(props);<br>    this.state = this.calculateState(props.items);<br>  }<br>  calculateState = (items) =&gt; ({<br>    items,<br>    total: items.map(item =&gt; item.value)<br>      .reduce((a, b) =&gt; a + b, 0),<br>  })<br>  deleteItem = ({ id }) =&gt; {<br>    const items = this.state.items.filter(item =&gt; item.id !== id);<br>    this.setState(this.calculateState(items));<br>  }<br>  render = () =&gt; (<br>    &lt;Container&gt;<br>      { this.state.items.map(item =&gt; (<br>        &lt;Item<br>          key={ item.id }<br>          item={ item }<br>          onDelete={ () =&gt; this.deleteItem(item) }<br>        /&gt;<br>      ))}<br>      &lt;Total total={ this.state.total }/&gt;<br>    &lt;/Container&gt;<br>  )<br>}</pre><blockquote>In this case, I’ve used a class component because I need the state to deal with items being deleted.</blockquote><p>This is a more complex component compared Cart, that has an internal state, that’s why it’s a good idea to test it be separate instead of doing it as part of Cart.</p><p>In the following sections, we’ll use Items to create a test that changes the internal component’s state.</p><h3>Test Renderer</h3><p>react comes with a package called react-test-renderer that allow us to “render” a component instead of using react-dom or react-native. What is the difference between them? Test renderer is faster than dom and native renderers but it isn’t rendering for real. That’s fine because we want to run our tests many times during development, that’s why fast is key.</p><h4>Two renderer flavors</h4><p>The package react-test-renderer provides two ways to render a component.</p><ul><li><strong>Deep</strong> (default): renders a component until it reaches a leaf component (View, Text, div, area, …).<br><a href="https://reactjs.org/docs/test-renderer.html">https://reactjs.org/docs/test-renderer.html</a></li><li><strong>Shallow</strong>: render a component’s first level only.<br><a href="https://reactjs.org/docs/shallow-renderer.html">https://reactjs.org/docs/shallow-renderer.html</a></li></ul><blockquote>For shallow rendering, you can also use enzyme, which provides extra features like support for refs. If you want to learn more about it visit <a href="https://github.com/airbnb/enzyme">https://github.com/airbnb/enzyme</a></blockquote><h4>Snapshots</h4><p>Snapshot comes as part of jest and with it, we can easily save the result of a test run and compare it with a future result, instead of having to write the result of the component by hand.</p><pre>expect(tree).<strong>toMatchSnapshot</strong>();</pre><blockquote>For more information about snapshots, visit <a href="https://facebook.github.io/jest/docs/en/snapshot-testing.html">https://facebook.github.io/jest/docs/en/snapshot-testing.html</a></blockquote><h3>Comparison</h3><p>To understand how snapshots and renderers work together let’s take User</p><pre>const User = ({ user }) =&gt; (<br>  &lt;Title&gt;{ user.name + &#39; - &#39; + user.email }&lt;/Title&gt;<br>);</pre><pre>const Title = (props) =&gt; &lt;Text { ...props } /&gt;</pre><p>the following data</p><pre>const user =  {<br>  name: &#39;joe&#39;,<br>  email: &#39;<a href="/service/mailto:joe@mail.com">joe@mail.com</a>&#39;,<br>};</pre><p>and write some tests!</p><h4>Using Deep Renderer</h4><p>We can write a test like this</p><pre>import renderer from &#39;react-test-renderer&#39;;</pre><pre>it(&#39;renders user deep correctly&#39;, () =&gt; {<br>  const tree = <strong>renderer.create</strong>(&lt;User user={ user }/&gt;);<br>  expect(tree).toMatchSnapshot();<br>});</pre><p>After running it once, the auto-generated result of the snapshot will look like</p><pre>exports[`renders user deep correctly 1`] = `<br>&lt;Text<br>  accessible={true}<br>  allowFontScaling={true}<br>  disabled={false}<br>  ellipsizeMode=&quot;tail&quot;<br>&gt;<br>  joe - <a href="/service/mailto:joe@mail.com">joe@mail.com</a><br>&lt;/Text&gt;<br>`;</pre><p>We can observe that the renderer went all the way down in the hierarchy until it found Text.</p><h4>Using Shallow Rendering</h4><p>The test will look like</p><pre>import ShallowRenderer from &#39;react-test-renderer/shallow&#39;;</pre><pre>it(&#39;renders user shallow correctly&#39;, () =&gt; {<br>  const renderer = <strong>new ShallowRenderer</strong>();<br>  <strong>renderer.render</strong>(&lt;User user={ user }/&gt;);<br>  const tree = <strong>renderer.getRenderOutput</strong>();<strong><br></strong>  expect(tree).toMatchSnapshot();<br>});</pre><p>And the result of the snapshot</p><pre>exports[`renders user shallow correctly 1`] = `<br>&lt;Title&gt;<br>  joe - <a href="/service/mailto:joe@mail.com">joe@mail.com</a><br>&lt;/Title&gt;<br>`;</pre><p>In this case the <strong>shallow renderer</strong> stopped resolving the components at the first level, instead of going to Text.</p><h4>Breaking Tests</h4><p>Imagine that we write the following test for Cart using the <strong>deep renderer</strong>.</p><pre>it(&#39;renders cart correctly&#39;, () =&gt; {<br>  const cart = {<br>    user: {<br>      name: &#39;joe&#39;,<br>      email: &#39;<a href="/service/mailto:joe@mail.com">joe@mail.com</a>&#39;,<br>    },<br>    items: [{<br>      id: 1, name: &#39;item 1&#39;, value: 7,<br>    }, {<br>      id: 2, name: &#39;item 2&#39;, value: 4,<br>    }]<br>  };<br>  const tree = renderer.create(&lt;Cart cart={ cart }/&gt;);<br>  expect(tree).toMatchSnapshot();<br>});</pre><p>What will happen if we change the implementation of Title? Should the test for Cart break? I’d like not to but it will. Because the renderer will go all the way down in the hierarchy, it will render User and Title, which has changed.</p><p>Instead, if we use the <strong>shallow render</strong>, the result of the snapshot won’t change and our test for Cart won’t break. That’s why it’s better to use the shallow renderer.</p><h3>State Changes</h3><p>All the previous examples cover rendering a component given props. We’ll move over to a more interesting case, in which a change in the state will trigger a new render result.</p><p>Using the previously defined component Items, let’s create a test that renders two items and then deletes one of them.</p><pre>it(&#39;renders one item deleted correctly&#39;, () =&gt; {<br>  const renderer = new ShallowRenderer();<br>  const items = [{<br>    id: 1, name: &#39;item 1&#39;, value: 7,<br>  }, {<br>    id: 2, name: &#39;item 2&#39;, value: 4,<br>  }];</pre><pre>  renderer.render(&lt;Items items={ items }/&gt;);<br>  const result0 = renderer.getRenderOutput();<br>  expect(result0).toMatchSnapshot();</pre><pre>  // access the first item and call `onDelete`.<br>  <strong>result0.props.children[0][0].props.onDelete();</strong><br>  const result1 = renderer.getRenderOutput();<br>  expect(result1).toMatchSnapshot();<br>});</pre><p>The only “challenging” part of this test was finding onDelete because you have to navigate the children hierarchy.</p><p>You can see that we didn’t touch the state directly but through onDelete. This is considered a <strong>black box</strong> test because we didn’t access any internals <strong>directly</strong> during the test.</p><p>The test will generate the following two snapshots that reflect the desired behavior</p><pre>exports[`renders one item deleted correctly 1`] = `<br>&lt;Container&gt;<br>  &lt;Item<br>    item={<br>      Object {<br>        &quot;id&quot;: 1,<br>        &quot;name&quot;: &quot;item 1&quot;,<br>        &quot;value&quot;: 7,<br>      }<br>    }<br>    onDelete={[Function]}<br>  /&gt;<br>  &lt;Item<br>    item={<br>      Object {<br>        &quot;id&quot;: 2,<br>        &quot;name&quot;: &quot;item 2&quot;,<br>        &quot;value&quot;: 4,<br>      }<br>    }<br>    onDelete={[Function]}<br>  /&gt;<br>  &lt;Total<br>    total={11}<br>  /&gt;<br>&lt;/Container&gt;<br>`;</pre><pre>exports[`renders one item deleted correctly 2`] = `<br>&lt;Container&gt;<br>  &lt;Item<br>    item={<br>      Object {<br>        &quot;id&quot;: 2,<br>        &quot;name&quot;: &quot;item 2&quot;,<br>        &quot;value&quot;: 4,<br>      }<br>    }<br>    onDelete={[Function]}<br>  /&gt;<br>  &lt;Total<br>    total={4}<br>  /&gt;<br>&lt;/Container&gt;<br>`;</pre><h3>Extras</h3><h4>High Order Components</h4><p>HoC simplifies our lives by providing features that we can easily “plug” to any component, but make our tests more complex because they add an extra “layer”.</p><p>A simple solution is to export the components that we want to test separately as well as the default with the HoC.</p><pre><strong>export</strong> const Component = (props) =&gt; ...<br>export default highOrderComponent(...)(Component);</pre><blockquote>To learn more about HoC visit <a href="https://reactjs.org/docs/higher-order-components.html">https://reactjs.org/docs/higher-order-components.html</a></blockquote><h4>Interfaces</h4><p>A component will receive inputs props and will return outputs components. Those inputs and outputs conform the component’s interface.</p><p>Try to keep your interfaces simple and abstract whenever possible. This’ll make your code readable, maintainable and testable.</p><p>One clear indication of a poor interface can occur during testing. If writing tests for a component becomes hard, review your interfaces.</p><h3>Conclusions</h3><ul><li>Break your components into multiple components and test each of them by separate.</li><li>Use the shallow renderer together with snapshots for simple and effective tests.</li><li>Avoid testing components and HoC at the same time, export and test each component by separate.</li><li>Spend the time to define good interfaces, they’ll make your life easier.</li></ul><h3>References</h3><ul><li><a href="https://github.com/7ynk3r/react_test">https://github.com/7ynk3r/react_test</a></li><li><a href="https://reactjs.org/docs/components-and-props.html#functional-and-class-components">https://reactjs.org/docs/components-and-props.html#functional-and-class-components</a></li><li><a href="https://reactjs.org/docs/test-renderer.html">https://reactjs.org/docs/test-renderer.html</a></li><li><a href="https://reactjs.org/docs/shallow-renderer.html">https://reactjs.org/docs/shallow-renderer.html</a></li><li><a href="https://facebook.github.io/jest/docs/en/snapshot-testing.html">https://facebook.github.io/jest/docs/en/snapshot-testing.html</a></li><li><a href="https://www.sitepoint.com/test-react-components-jest/">https://www.sitepoint.com/test-react-components-jest/</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=24fdb4ef43d8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Migrating to React Native]]></title>
            <link>https://medium.com/@7ynk3r/migrating-to-react-native-b51d280058f2?source=rss-f9347ecd616d------2</link>
            <guid isPermaLink="false">https://medium.com/p/b51d280058f2</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Juan M Rodriguez]]></dc:creator>
            <pubDate>Mon, 06 Feb 2017 18:49:05 GMT</pubDate>
            <atom:updated>2018-06-15T21:44:20.534Z</atom:updated>
            <content:encoded><![CDATA[<h4>A one way journey</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*634nX-24vpaGB7LyaUpbyQ.jpeg" /></figure><p>Building a new project with React Native and being able to rapidly iterate on it is a lot of fun. What’s not as fun: not being able to use RN in an already existing iOS / Android native application. Having to work in the old way — duplicating business logic, testing, framework, etc. — can really bring you back down to earth.</p><p>There’s no doubt that React Native is the way to go for new projects. However, that may not be the case for migrating an existing one. There’re many things to take into account and challenges on how to integrate the old and the new world.</p><p>In this article I’ll present some of the biggest challenges and solutions to those braves out there, prepared to start migrating those old native apps into RN.</p><p>I’ll include some code samples for the more interesting parts as a reference. The code will be for iOS only given that I don’t want to make the article too long, and there’s a similar solution for Android if you get the idea for iOS.</p><h3>Should we move to React Native?</h3><p>It may not be you, but many people still doubt that RN is the right tool for serious production environments, especially in the enterprise world, where applications will live for many years and you have to think on the long run, where choosing the wrong tool could cost a lot.</p><p>I won’t spend a lot of time explaining why it’s worth the effort to move to RN given that are a lot of resources about the topic, but I’ll mention the two most important reasons:</p><ol><li><strong>One source of truth</strong>: Having to support two platforms forces code duplication, which is a huge waste of time and money. But not only that, you have to keep two teams working in different languages, using different frameworks and tools. Even hiring for that is really challenging.</li><li><strong>Development speed</strong>: Given that there’s no need to compile your app every time you make a change, development with RN is much faster. You can make use of many mature javascript libraries out there, as long as they don’t use the DOM.</li><li><strong>Collaboration</strong>: Front-end developers, designers and prototypers are used to work with JS, so RN environment is much more familiar to them, even giving them the opportunity to try making changes by themselves.</li></ol><p>Still it’s true that there are some flaws like bugs in the code base, breaking changes in new releases and some missing features. I’ve also heard people having performance problems, but those in general are due to lack of experience with the platform. In any case, RN is moving forward really fast, tackling all problems mentioned before while adding new and cool features like support for Windows Phone, Apple TV and more.</p><p>And if you’re worried about something you’re currently using on native and you want to keep using it, don’t worry, it’s possible and simple. I’ll talk about this later on.</p><h3>How does React Native integrate with Native?</h3><p>Native applications are basically a composition of views. In order to use react native into a native application you have to follow the same pattern, by using RCTRootView for iOS andReactRootView for Android. We also need to indicate to these views which RN application to use, those we declare using AppRegistry.registerComponent</p><blockquote>If you’re not familiar integrating RN with an existing app I’d recommend you to follow the steps <a href="https://facebook.github.io/react-native/docs/integration-with-existing-apps.html">here</a>.</blockquote><p>We also need a <strong>container</strong> for the RN view to run. In native, views are contained by a UIViewController in the case of iOS and an Activity in the case of Android. The RN view is just another native view so it will follow the same structure, but we have to define what is going to be the relation between the container and the RN views.</p><ol><li><strong>One container, multiple RN views</strong>: As any kind of views, RN views can be added to any part of the native view hierarchy and can be combined with other types of views. Even when possible it’s not recommended given that we may run into layout and communication problems between the views.</li><li><strong>One container, one RN view</strong>: The container will have only one RN view covering the whole viewport. This is the best option because it keeps the structure simple and it allows you to handle the view hierarchy inside RN.</li></ol><p>In any either case, remember to create <strong>only one bridge</strong> and share it across all of your RN views. You can use the following UIViewController container as a reference for iOS:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/11823a7d969ef0cb1f80c76beadfea14/href">https://medium.com/media/11823a7d969ef0cb1f80c76beadfea14/href</a></iframe><h3>How to consume my current native classes?</h3><p>Sooner or later you’ll need to consume a native component that is not available in react native. For that you’ll have to expose your native component by implementing a few classes and methods.</p><p>Everything on RN works based on native <strong>UI components </strong>and <strong>modules</strong>. Both are very similar, the only difference is that native modules don’t have a native view associated with them.</p><p>UI components and modules act as the native counterpart of javascript components. Think about the standard View component in RN. In iOS it’s represented by RCTView (UIView subclass) that is managed by RCTViewManager. Similar on Android, it’s represented by ReactViewGroup (ViewGroup subclass) that is managed by ReactViewManager. This same logic applies to any RN component.</p><blockquote>If you aren’t familiar with native modules/components I’d recommend you to read Native Modules for <a href="https://facebook.github.io/react-native/docs/native-modules-ios.html">iOS</a> and <a href="https://facebook.github.io/react-native/docs/native-modules-android.html">Android</a> and Native UI Components for <a href="https://facebook.github.io/react-native/docs/native-components-ios.html">iOS</a> and <a href="https://facebook.github.io/react-native/docs/native-components-android.html">Android</a></blockquote><p>Next, I’ll present some common UI components and modules that you may have to implement:</p><ul><li><strong>State</strong>: As you start using RN, all your state is stored natively and you might have no have plans to move it to RN because of other native dependencies. So, you need a way to expose it into RN and for that I’ll use an approach similar to redux.</li><li><strong>Action</strong>: If you want to mutate your state from RN, you’ll have to expose the corresponding methods in a native module. Again, same concept exists in redux.</li><li><strong>Navigation</strong>: You may have a button in your RN view that when pressed navigates the user to a native component. For instance, having master and detail containers where master is RN and the detail pure native. You need a way to push/invoke the detail from master.</li></ul><blockquote>If the <strong>state</strong> you need to consume in RN doesn’t change, you can simple send the state as a parameter at the moment of creating the RN root view. The state will by present in RN under props.</blockquote><p>The component native model will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/398/1*ZkS_MPm-XxEcVFqyFDQfvg.png" /></figure><ul><li>In green, the RN bridge running our javascript.</li><li>In yellow, the native UI components and modules.</li><li>In blue containing everything, the native environment.</li></ul><h4>State</h4><p>To deal with the state of the native application will need a way to subscribe to changes made the state. For that, we’ll create a native module based on RCTEventEmitter:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d01fee9f95f8062123bad8434bd1885/href">https://medium.com/media/0d01fee9f95f8062123bad8434bd1885/href</a></iframe><p>This module will provide the nativeStateDidChange event to RN and a getState method to read the state. You could pass the state as part of the event but it’ll cause problems if you have several events altering the state, having to send the whole state from each of them.</p><h4>Action</h4><p>Now that we can consume the native state, we need a way to mutate it. In order to do that we’ll follow the same pattern as for <strong>state</strong>. We’ll create a native class and expose all the actions that our the business model requires to be triggered from RN. One minor change is that the native class will inherit from NSObject &lt;RCTBridgeModule&gt; instead of RCTEventEmitter because we don’t need to subscribe to any event: once the action is sent, it’ll change the native state, which will trigger an event through RNNativeState. This is the same pattern that redux follows.</p><h4>Navigation</h4><p>There are different approaches on how to navigate to a native module. You can choose to present the native module independent of the context, or if you already have some routing logic implemented natively, you can just call it. Depending on your decision, you may opt to implement it using a module or UI component.</p><p>For our sample, I’ll assume that the RNViewController that contains our RN application:</p><ul><li>Will be pushed on top of a UINavigationViewController.</li><li>Will push a nativeUIViewController by sending a route.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d92d9fcc454920f93427f94da921defb/href">https://medium.com/media/d92d9fcc454920f93427f94da921defb/href</a></iframe><p>As you can see, we have to implement a UI component by subclassing RCTViewManager because we need a reference to the native UIView to be able to access the underlying UINavigationViewController.</p><h3>Conclusions</h3><p>Migrating your current native application to react native is possible and it can be done gradually.</p><p>You’re able to use any native class by implementing a native UI component or module. Use UI components only if you need to access the native view or its hierarchy.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b51d280058f2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Native & Apple TV today]]></title>
            <link>https://medium.com/@7ynk3r/react-native-apple-tv-today-48beb398a1ab?source=rss-f9347ecd616d------2</link>
            <guid isPermaLink="false">https://medium.com/p/48beb398a1ab</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[apple-tv]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Juan M Rodriguez]]></dc:creator>
            <pubDate>Wed, 19 Oct 2016 17:41:25 GMT</pubDate>
            <atom:updated>2016-10-19T17:41:25.350Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*nIzAjosWafePu--SRxeU4Q.png" /></figure><p>I always check the notes for every release made in React Native. The other day I was amazed by the announcement of an interesting feature in <strong>v0.36.0-rc.0</strong>: support for Apple TV.</p><p>I started by looking at the commits (made by <a href="https://github.com/dlowder-salesforce">dlowder-salesforce</a>), and I found that the <strong>UIExplorer</strong> sample project already has support for it but wasn’t working because of a problem with the Status Bar, which I assume is because there isn’t a Status Bar.</p><p>After analyzing the changes made to the project plus reading some tickets reported in github about the topic, I was able to find the necessary steps to enable Apple TV.</p><p>Okay, now without any further ado, here are the steps to add support for Apple TV to a new React Native project. I’ll try to be as detailed as possible in my steps:</p><ul><li>Create a new project<br><strong>react-native init apple_tv_example</strong></li><li>Use the release candidate in <strong>package.json</strong>, by<strong> </strong>replacing the react-native version (in my case was ^0.35.0) with:<strong><br></strong> <strong>git://github.com/facebook/react-native.git#v0.36.0-rc.0</strong><br><em>Note: This will be optional in the future because it will be part of a release.</em></li><li>Install the changes made to the package, by running <br><strong>npm i</strong><br><em>Note: You’ll receive an </em><strong><em>UNMET PEER DEPENDENCY</em></strong><em> error - that’s okay. To verify that the package was properly installed, looks for references to </em><strong><em>TARGET_OS_TV</em></strong><em> in files under </em><strong><em>node_modules/react-native</em></strong><em>, there should be plenty.</em></li><li>Add a tvOS application to the project, by going to “File &gt; New &gt; Target” on xCode and selecting “tvOS &gt; Application &gt; Single View Application”.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/739/1*NoPcvVE727_wWuBZh0rNjw.png" /></figure><ul><li>Link all tvOS libraries to the your new target by pressing “+” in <strong>Linked Frameworks and Libraries</strong>. There, search in Workspace for all libraries containing “tvOS” and Add them.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/406/1*jKLB7powLrbg667IakA8tw.png" /></figure><ul><li>Add support for ObjC and LC++, by adding <br><strong>$(inherited) -ObjC -lc++<br></strong>to “Other Linker Flags” under “Build Settings”.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*gfudy7HLS5MwkKIdy_RGUw.png" /></figure><ul><li>Delete all the files and folders inside the folder created for the new target, but leave <strong>Info.plist.</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/234/1*PDVOvKLVmaJdpZNfsnBxyw.png" /></figure><ul><li>Edit <strong>Info.plist</strong> by removing <strong>Main storyboard file base name</strong> (given that we won’t use any storyboards) and by adding <strong>App Transport Security Settings </strong>(copy it from the iPhone’s target Info.plist file) to allow the use of <strong>localhost</strong>.</li><li>Add all the files belonging to the iPhone target to the TV target. For that, select <strong>AppDelegate.m </strong>and then select the TV target in the <strong>Utilities</strong> side bar. Repeat the process for <strong>main.m</strong> (and all the files that belong to your iPhone target).<br><em>Note: Ignore </em><strong><em>xib</em></strong><em> files, those are not supported on TV.</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/261/1*arZbacr5KwUNf6G018Wymw.png" /></figure><ul><li>Finally, select your new target and press <strong>Play</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/379/1*Jaj2yZYfkKybz-sMwrhRrA.png" /></figure><ul><li>Done!</li></ul><p>You may find some errors using an existing project given that some components are not supported on TV, for instance Status Bar and Notifications.</p><p>You can find the source code containing the result of these steps at <a href="https://github.com/7ynk3r/apple_tv_example">https://github.com/7ynk3r/apple_tv_example</a>.</p><p>I hope you have a lot of fun!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=48beb398a1ab" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Native Animations Done Right]]></title>
            <link>https://medium.com/@7ynk3r/react-native-animations-done-right-d5249cc1ebbf?source=rss-f9347ecd616d------2</link>
            <guid isPermaLink="false">https://medium.com/p/d5249cc1ebbf</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Juan M Rodriguez]]></dc:creator>
            <pubDate>Tue, 30 Aug 2016 04:06:25 GMT</pubDate>
            <atom:updated>2018-05-05T05:56:28.662Z</atom:updated>
            <content:encoded><![CDATA[<p>Animations in react native are great! They’re simple and at the same time powerful. Still, there are some important concepts to take into account and common pitfalls.</p><h3>Introduction</h3><p>Before we start digging into animations in react native you may ask, why do we need a special set of tools to create animations? We could simply use setState to trigger render as many times as we need, right? Yes, simple and effective, this will create the frame sequence we want.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/382b2836345b4e744db55195d135b563/href">https://medium.com/media/382b2836345b4e744db55195d135b563/href</a></iframe><blockquote>It’s a good practice to bind methods to this in the constructor.</blockquote><p>While this solution will work well for simple cases, rendering all the view hierarchy could be expensive. But more important, because react native provides a set of specific tools (animation types, interpolation, etc) to make things simpler, clearer and more performant.</p><h3>Two alternatives</h3><p>React native offers two types of animations:</p><ul><li><strong>LayoutAnimation</strong>: Automatically animates the difference between the current layout and a future one, generated after a new state is set. This animation happens natively, which is great for performance, but can be hard to deal with given that <strong>all</strong> properties are going to be animated between states.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c968d4711ffe6095fb9c4beea5d92d73/href">https://medium.com/media/c968d4711ffe6095fb9c4beea5d92d73/href</a></iframe><blockquote>In order to get this to work on <strong>Android</strong> you need to set UIManager.setLayoutAnimationEnabledExperimental(true)</blockquote><ul><li><strong>Animated</strong>: Allow us to create declarative animations by binding an animated object to a style property. It runs on javascript by using requestAnimationFrame and setNativeProps, which could be slow depending on how we’re animating. The benefits are plenty: flexibility, control and integration with gestures.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f30a70a7a4fd533bd2aa6130cb52275/href">https://medium.com/media/9f30a70a7a4fd533bd2aa6130cb52275/href</a></iframe><p>As you can see an Animated animation work based on Animated.Value associated/bound to a style property. Then, the only thing yo need to do is start the animation, making the animated value change over time, updating the style.</p><h3>Main Animated</h3><p>When I started making animations I found myself creating an Animated.Value instance per style property I wanted to animate. Then using Animated.parallel and Animated.sequence I was able to combine them.</p><p>This approach works well for simple transitions but it becomes hard to maintain when we start having multiple animations that should work synchronously.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/90fd9c0ba05f025a6e1fd5275b7645d8/href">https://medium.com/media/90fd9c0ba05f025a6e1fd5275b7645d8/href</a></iframe><blockquote>This doesn’t mean that you should never use Animated.parallel and Animated.sequence, but that in general you don’t need to use them.</blockquote><p>If you look closely to the animations you’ll find that you can extract one of them and base the others on that one. That’s when Animated.interpolate comes in place.</p><h3>Interpolation</h3><p>Animated.interpolate creates a new animation based on an existing one by mapping its values to a different range. This simple but powerful concept allow us to create a Animated.Value hierarchy based on a root Animated.Value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6bb5099f2b884835e7f64c0be81372ee/href">https://medium.com/media/6bb5099f2b884835e7f64c0be81372ee/href</a></iframe><blockquote>To read more about interpolation (and extrapolation) take a look at the references.</blockquote><p>This concept is amazing and simplifies the code a lot but has a small catch. If the root animation range changes, <strong>all</strong> the child animations will be affected. For this problem there is also a simple and elegant solution.</p><h3>Normalized range</h3><p>I found that having our main animation normalized (ranging between 0 and 1) helps keeping interpolated animations simple given the input range for all of them will always be the same</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e517a846f30bd52836a29b1cb1d5daf7/href">https://medium.com/media/e517a846f30bd52836a29b1cb1d5daf7/href</a></iframe><h3>Gestures</h3><p>Under certain circumstances we want the user to ‘control’ the animation by performing a gesture like scroll or pan. Such situations don’t vary too much from what we’ve seen so far. The idea behind this is to map one or more values in the gesture’s event to one or more animated values. For that Animated provides a handy function called event.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/12244c67978089dc55ad90d8a5b3ed61/href">https://medium.com/media/12244c67978089dc55ad90d8a5b3ed61/href</a></iframe><p>If we look closer to event(…) you’ll realize that it’s just returning a function with matches the signature of onPanResponderMove. This means that we can actually create our own custom version of the mapping.</p><h4>Gestures with custom mapping</h4><p>Animated.event is great for simple mapping but not for complex scenarios. There are situations in which we want to make some calculation with the gesture parameters before setting it to an animated value. We can achieve that by writing a custom handler and setting animated value manually with Animated.Value.setValue.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4da7b12f96763f83d6bd8462c83677af/href">https://medium.com/media/4da7b12f96763f83d6bd8462c83677af/href</a></iframe><h3>Conclusions</h3><p><strong>Layout animations</strong> are simple but not as powerful as <strong>Animated</strong>.</p><p>While working with Animated, always have a <strong>root animated</strong> value to represent the overall transition. Based on it, create as many <strong>interpolated animations</strong> as you need.</p><p>Keep the root animated value range <strong>normalized</strong>.</p><p><strong>Gestures</strong> plays really well with Animated.</p><p>If you find that you’re missing some frames while animating check:</p><ul><li><strong>Changing the state triggers render</strong>: There are situations in which a new state / properties shouldn’t change the component layout. In these situations we can use shouldComponentUpdate returning false to avoid calling render.</li><li><strong>Avoid animating height and width, use scaleX and scaleY instead</strong>: When the result is not the same it’s much faster. This is because changing the size of the view with height / width forces re-rendering the whole view hierarchy, which takes a lot more time. As a general rule use transform.</li></ul><p>Enjoy your animations!</p><h3>References</h3><ul><li><a href="https://facebook.github.io/react-native/docs/animations.html">Basics About Animations</a></li><li><a href="http://browniefed.com/react-native-animation-book/">The Animation Book</a></li><li><a href="https://github.com/browniefed/react-native-animation-book/blob/master/INTERPOLATION.md">Interpolation (and Extrapolation)</a></li><li><a href="https://facebook.github.io/react-native/docs/direct-manipulation.html">Direct manipulation</a></li><li><a href="https://speakerdeck.com/vjeux/react-rally-animated-react-performance-toolbox">React Rally: Animated — React Performance Toolbox</a></li><li><a href="https://medium.com/@housecor/react-binding-patterns-5-approaches-for-handling-this-92c651b5af56#.76my85pfn">React Binding Patterns: 5 Approaches for Handling `this`</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d5249cc1ebbf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>