<?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 Barak Zadiki on Medium]]></title>
        <description><![CDATA[Stories by Barak Zadiki on Medium]]></description>
        <link>https://medium.com/@BarakZadiki?source=rss-91aaa63fe02------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*HwPByc9lO3obt5wXIm1V7g.jpeg</url>
            <title>Stories by Barak Zadiki on Medium</title>
            <link>https://medium.com/@BarakZadiki?source=rss-91aaa63fe02------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 03:38:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@BarakZadiki/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[Practical concepts for unit testing in React Native]]></title>
            <link>https://medium.com/att-israel/practical-concepts-for-unit-testing-in-react-native-d8ad72fdb659?source=rss-91aaa63fe02------2</link>
            <guid isPermaLink="false">https://medium.com/p/d8ad72fdb659</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[react-testing-library]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Barak Zadiki]]></dc:creator>
            <pubDate>Thu, 02 Mar 2023 11:45:18 GMT</pubDate>
            <atom:updated>2023-03-02T15:33:30.227Z</atom:updated>
            <content:encoded><![CDATA[<h3>Practical Concepts for Unit Testing in React Native</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GE8uhCsWk1H-OQfoetn76A.jpeg" /></figure><p>Unit testing is an essential aspect of software development. It allows developers to ensure that their code is working as expected and catch any bugs early on in the development process. React Native, being a popular framework for building mobile applications, also has its own set of testing tools and libraries. In this article, we will discuss a few best practices that I find useful for unit testing React Native applications using the React Native Testing Library (RNTL) and Jest.</p><p><strong>Background:</strong></p><p>When we started our project (now we have more than 50M users per month) in React Native we used Jest and Enzyme for testing. After using this method for one year, we found that it was a bit difficult and inflexible for our specific needs. The reason for this is that in Enzyme, we test component properties and states. Therefore, the tests tend to be unstable and don’t represent the actual user experiences.</p><p>After that year, we started using the RNTL, which we found to be easier to work with and more stable. The goal of the RNTL team is to increase confidence in your tests by testing your components as they would be used by the end user. Users don’t care what happens behind the scenes. They just see and interact with the output. Instead of tests that access the components’ internal APIs or evaluate their state, you’ll feel more confident with writing your tests based on component output.</p><p>It’s important to mention that we aren’t following all of the RTNL official best practices. We create our own practices to suit our needs. I encourage you to take a look at them with an objective viewpoint and experiment with them yourself.</p><h3>Before you start writing code, think about these strategies:</h3><p>1. <strong>Keep your tests focused:</strong> Each test should only test one thing at a time. Avoid testing complex logic or multiple components in one test.</p><p>2. <strong>Test behavior, not implementation:</strong> Test what the component does, not how it does it. For example, test that a button changes color when pressed, not the specific Style class used.</p><p>3. <strong>Use test-specific data:</strong> Avoid using real data from your application in tests. Instead, use data specifically created for the test. This ensures the test is reliable and repeatable.</p><p>4. <strong>Test for accessibility:</strong> Accessibility is an important aspect of mobile development. Test that your component has appropriate usability support for screen readers.</p><p>5. <strong>Keep tests organized:</strong> Group tests by related functionality and consider using a pattern such as “test description” for the test names and “each” loop on the data.</p><p>6. <strong>Always test edge cases:</strong> Test for edge cases such as empty or null input, to ensure that your component can handle those scenarios.</p><h3>Structure:</h3><p><strong>Where to locate the’ __tests__’ folder?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/1*qHmRCC28QdgledSCq-drEA.png" /></figure><p>Each component has its own folder and inside that folder, we have the component file and the __tests__ folder with the test file of the component.</p><p>This approach keeps the test files close to the component files, making it easy to find and maintain them by identifying which test corresponds to which component.</p><p><strong>__mocks__</strong></p><p>These mock implementations are used to isolate the component or module under test and to prevent it from making real network requests or from accessing real storage.</p><p><strong>Where to locate the’ __mocks__ ‘folder?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/653/1*zHM4G6oPOmaijVHA8a2_JA.png" /></figure><p>A common location for the __mocks__ folder is inside the __tests__ folder. This keeps all the mock modules and implementations close to the test files, making it easy to understand the relationship between the mocked modules and the tests that use them.</p><h3>How to write tests — example with explanations</h3><p>As part of our testing development process, we follow these practices:</p><ol><li><strong>User perspective:</strong> Make sure clicks and texts on the screen are called and displayed.</li><li><strong>Component composition: </strong>Mock the child component and check if it has been called and which props it has been called with.</li><li><strong>Isolate test files: </strong>Every component has its own test file (see the structure section).</li></ol><h3>1. <strong>User perspective</strong>:</h3><p>Let’s start with a simple example:</p><p>The task is to build a card with an Image on the left, and text and button on the right.<br>When clicking on the card or the button it should open a WebView and send an analytics report.</p><p>Should look like this -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*5PPnCiKYmy0oZCr7v9__-w.png" /></figure><p>The code:</p><p><strong>*Note — <br></strong>The new convention by the RNTL is to use ‘screen’ to get the queries. We don’t use this yet in our code.</p><p>ActionCards.tsx-</p><pre>import React, { ReactElement } from &#39;react&#39;;<br>import { Image, TouchableOpacity, View } from &#39;react-native&#39;;<br>import { sendAnalytics, Analytics } from &#39;./analytics&#39;;<br>import { ButtonWithStyle, TextWithStyle } from &#39;./components&#39;;<br>import { navigateTo } from &#39;./navigation&#39;;<br><br>interface ActionCardData {<br>  imageUrl: string;<br>  text: string;<br>  button: {<br>    text: string;<br>    link: string;<br>    analytics: Analytics;<br>  };<br>}<br><br>interface Props {<br>  data?: ActionCardData[];<br>}<br> <br>export const ActionCards = ({ data }: Props): ReactElement =&gt; {<br> <br>  const ActionCard = ({ imageUrl, text, button, index }: ActionCardData &amp; { index: number; key: number }) =&gt; {<br>    const onPress = () =&gt; {<br>      const { link, analytics } = button;<br>      navigateTo(link);<br>      analytics &amp;&amp; sendAnalytics(analytics);<br>    };<br> <br>    return (<br>      &lt;TouchableOpacity style={styles.card} testID={`ActionCard:${index}`} onPress={onPress}&gt;<br>        &lt;View style={styles.leftContainer}&gt;{&lt;Image testID=&#39;ActionCardImage&#39; style={styles.image} source={{ uri: imageUrl }} /&gt;}&lt;/View&gt;<br>        &lt;View style={styles.rightContainer}&gt;<br>          &lt;TextWithStyle text={text} styleName={&#39;headingBold&#39;} /&gt;<br>          &lt;ButtonWithStyle customStyle={styles.button} text={button?.text} onPress={onPress} variant=&#39;btnDefLink&#39; /&gt;<br>        &lt;/View&gt;<br>      &lt;/TouchableOpacity&gt;<br>    );<br>  };<br> <br>  return (<br>    &lt;View style={styles.container}&gt;<br>      {data?.map((card, index) =&gt; {<br>        return &lt;ActionCard key={index} index={index} imageUrl={card?.imageUrl} text={card?.text} button={card?.button} /&gt;;<br>      })}<br>    &lt;/View&gt;<br>  );<br>};<br> </pre><p><strong>Which tests should we write?</strong></p><p>1. Verify all the elements are present — 2 texts and an image.<br>2. Verify that the code can handle getting data as — undefined or null.<br>3. Verify that when we click on the <strong>button, </strong>the analytics and the webView are called.<br>4. Verify that when we click on the <strong>Card, </strong>the analytics and the webView are called.</p><p>The test file :</p><p>ActionCards.test.tsx -</p><pre>import React from &#39;react&#39;;<br>import { fireEvent, render } from &#39;@testing-library/react-native&#39;;<br>import * as sendAnalytics from &#39;../analytics&#39;;<br>import * as navigateTo  from &#39;../url-navigation&#39;;<br>import { actionCardsMock } from &#39;./__mocks__/actionCards.mock&#39;;<br>import { ActionCards } from &#39;./ActionCards&#39;;<br> <br>const mockProps = { data: actionCardsMock };<br> <br>const setup = (overrides = {}) =&gt; {<br>  const props = { ...mockProps, ...overrides };<br>  const utils = render(&lt;ActionCards {...props} /&gt;);<br>  return { ...utils, props };<br>};<br> <br>describe(&#39;&lt;ActionCards/&gt;&#39;, () =&gt; {<br>  it(&#39;Should render text and image&#39;, () =&gt; {<br>    const {<br>      getByText,<br>      getAllByTestId,<br>      props: { data },<br>    } = setup();<br> <br>    getByText(data[0].text);<br>    getByText(data[0].button?.text);<br>    getByText(data[1].text);<br>    getByText(data[1].button?.text);<br> <br>   expect(getAllByTestId(&#39;ActionCardImage&#39;)).toHaveLength(2);<br>  });<br> <br>  it(&#39;Should support undefined or null data&#39;, () =&gt; {<br>    const edgeCasesMock = [<br>      { imageUrl: null, text: null, button: { text: null, link: null, analytics: null } },<br>      { imageUrl: undefined, text: undefined, button: undefined },<br>    ] as any[];<br> <br>    expect(() =&gt; render(&lt;ActionCards data={edgeCasesMock} /&gt;)).not.toThrowError();<br>  });<br> <br>  describe(&#39;Check onPress&#39;, () =&gt; {<br>    const openWebViewSpy = jest.spyOn(navigateTo, &#39;navigateTo&#39;);<br>    const sendLinkClick = jest.spyOn(sendAnalytics, &#39;sendAnalytics&#39;);<br> <br>     afterEach(() =&gt; {<br>      jest.restoreAllMocks();<br>    });<br> <br>    it(&#39;Should click on Card will open a webView and send analytics&#39;, () =&gt; {<br>      const {<br>        getByTestId,<br>        props: { data },<br>      } = setup();<br> <br>      const card = getByTestId(&#39;ActionCard:0&#39;);<br> <br>      fireEvent.press(card);<br> <br>      expect(openWebViewSpy).toHaveBeenCalledWith(data[0].button.link);<br>      expect(sendLinkClick).toHaveBeenCalledWith(data[0].button.analytics);<br>    });<br> <br>    it(&#39;Should click on Button will open a webView and send analytics&#39;, () =&gt; {<br>      const {<br>        getByText,<br>        props: { data },<br>      } = setup();<br> <br>      const button = getByText(data[0].button.text);<br> <br>      fireEvent.press(button);<br> <br>      expect(openWebViewSpy).toHaveBeenCalledWith(data[0].button.link);<br>      expect(sendLinkClick).toHaveBeenCalledWith(data[0].button.analytics);<br>    });<br>  });<br>});</pre><h4>Break it down -</h4><p><strong>* mock the data:</strong></p><pre>import { actionCards } from &#39;./__mocks__/actionCards.mock&#39;; <br><br>const mockProps = { data: actionCardsMock };</pre><p>We take the mock data from our __mock__ file and use it during the test and the development.</p><p><strong>* Setup function -</strong></p><pre><br>const setup = (overrides = {}) =&gt; {<br>  const props = { ...mockProps, ...overrides };<br>  const utils = render(&lt;ActionCards {...props} /&gt;);<br>  return { ...utils, props };<br>};<br><br>// Also we can use this generic function to create the setup <br><br>// Should be in common utils file<br>const setupBuilder =<br>  (Comp, mockProps) =&gt;<br>  (overrides = {}) =&gt; {<br>    const props = { ...mockProps, ...overrides };<br>    const utils = render(&lt;Comp {...props} /&gt;);<br>    return { ...utils, props };<br>  };<br><br>// And then in the tests<br><br>const setUp = setupBuilder(ActionCards, mockProps);<br><br>it(&#39;Should render text and image&#39;, () =&gt; {<br>// If we want to override the props we will call them inside the setup like <br> setup({overridesProps});<br>})</pre><p>The setup function renders the component with the mock props and also gets props for overriding them from outside, which supports the ability to use queries like ‘getBy..’ .</p><p>The root ‘describe’ will always be called with the name of the component -</p><pre>describe(&#39;&lt;ActionCards/&gt;&#39;, () =&gt; {</pre><p>1. Verify all the elements are present — 2 texts and an image.</p><pre>it(&#39;Should render text and image&#39;, () =&gt; {<br>  const {<br>    getByText,<br>    getAllByTestId,<br>    props: { data },<br>  } = setup();<br> <br>  getByText(data[0].text);<br>  getByText(data[0].button?.text);<br>  getByText(data[1].text);<br>  getByText(data[1].button?.text);<br> <br>  expect(getAllByTestId(&#39;ActionCardImage&#39;)).toHaveLength(2);<br>});</pre><p>We will check if all the elements are rendered.<br>- for the text elements we will use getByText, and for the image ‘getAllByTestId’ to check if we have two images.</p><p>2. Verify that the code can handle getting data as — undefined or null.</p><pre>it(&#39;Should support undefined or null data&#39;, () =&gt; {<br>  const edgeCasesMock = [<br>    { imageUrl: null, text: null, button: { text: null, link: null, analytics: null } },<br>    { imageUrl: undefined, text: undefined, button: undefined },<br>  ] as any[];<br> <br>  expect(() =&gt; render(&lt;ActionCards data={edgeCasesMock} /&gt;)).not.toThrowError();<br>});</pre><p>For edge cases, we will check if our values can be null or undefined without causing the app to crash.</p><p>By mocking our data with incorrect values, we can compare them to check if the code will not throw an error. If it does, the test will fail.</p><p>3. Verify that when we click on the <strong>Button, </strong>the analytics and the webView are called.<br>4. Verify that when we click on the <strong>Card, </strong>the analytics and the webView are called.</p><pre>describe(&#39;Check onPress&#39;, () =&gt; {<br>  const openWebViewSpy = jest.spyOn(navigateTo, &#39;navigateTo&#39;);<br>    const sendLinkClick = jest.spyOn(sendAnalytics, &#39;sendAnalytics&#39;);<br> <br>     afterEach(() =&gt; {<br>      jest.restoreAllMocks();<br>    });<br> <br>    it(&#39;Should click on Card will open a webView and send analytics&#39;, () =&gt; {<br>      const {<br>        getByTestId,<br>        props: { data },<br>      } = setup();<br> <br>      const card = getByTestId(&#39;ActionCard:0&#39;);<br> <br>      fireEvent.press(card);<br> <br>      expect(openWebViewSpy).toHaveBeenCalledWith(data[0].button.link);<br>      expect(sendLinkClick).toHaveBeenCalledWith(data[0].button.analytics);<br>    });<br> <br>    it(&#39;Should click on Button will open a webView and send analytics&#39;, () =&gt; {<br>      const {<br>        getByText,<br>        props: { data },<br>      } = setup();<br> <br>      const button = getByText(data[0].button.text);<br> <br>      fireEvent.press(button);<br> <br>      expect(openWebViewSpy).toHaveBeenCalledWith(data[0].button.link);<br>      expect(sendLinkClick).toHaveBeenCalledWith(data[0].button.analytics);<br>    });<br>  });<br>});</pre><p>We use ‘jest.spyOn’ to mock the webView and the analytics, then we simulate clicking on the button/card and verifying that the mock has been called with the expected data.</p><p>As we can see, the two tests were created under one ‘describe’ block, ‘Check onPress’, because they are in the same scope.</p><p>If we want to check only specific properties we will use — ‘objectContaining’</p><pre>expect.objectContaining({<br>          text: &#39;title&#39;,<br>          subTitle: &#39;blabla&#39;,<br>        }</pre><h3><strong>2. Component composition</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/501/1*YqGmKYBSlgl5wscQEnmZVA.png" /></figure><p><strong>The Problem</strong><br>Most of our custom components render other custom components alongside React-Native native components (&lt;Text&gt; etc.), In order to follow the library approach, we test component B elements when testing component A.</p><p><strong>Overhead —</strong> component B elements are tested in tests of any component that contains B.<strong><br>Coupling — </strong>changes in component B elements may cause tests containing A components to fail.</p><p>As a result, it’s not practical — on multiple compositions (A -&gt; B -&gt; C …), the number of components to search for and test when testing A is huge.</p><p><strong>The solution — mock</strong><br>Instead of testing component B elements when testing component A, we spy/mock component B. Using the spy/mock functions, we assert that component B was used (rendered) by component A and that the correct props were passed by A to B. Component B must be (unit) tested separately with the same approach (for maximum coverage).</p><p><strong>No overhead</strong> — component B elements are tested once (in their own unit test).<br><strong>No coupling</strong> — changes in component B elements can’t cause tests containing component A to fail.</p><p>Practical — when testing A, we test the React-Native native elements (a few) using the react-testing-library approach, and just spy/mock other custom components.</p><p><strong>Spying/Mocking</strong></p><p>-Spying a dependency allows verifying the number of times it was called and with which parameters</p><ul><li>toHaveBeenCalledTimes()</li><li>toHaveBeenCalledWith()</li></ul><p>-Spying alone doesn’t change the dependency behavior</p><p>-In order to change the behavior, use mock APIs on the spied dependency, such as:</p><ul><li>mockImplementation()</li><li>mockReturnValue()</li><li>mockReturnValueOnce()</li></ul><p>-There are dependencies that cannot be spied and they must be fully mocked</p><ul><li>Jest.mock()</li></ul><p>Let’s show a code example :</p><p>A — Parent<br>B — Child<br>C — Child</p><p>Code — A.tsx</p><pre>import React, { ReactElement } from &#39;react&#39;;<br>import Analytics from &#39;./analytics&#39;;<br>import B from &#39;./B&#39;;<br>import C from &#39;./C&#39;;<br> <br> <br>interface Props {<br>title : { text: string, analytics: Analytics }<br>subTitle :{ text: string, analytics: Analytics }<br>}<br> <br>export const A = ({ title, subTitle }: Props): ReactElement =&gt; {<br> <br> <br>return (<br>&lt;View&gt;<br>&lt;B {...title}/&gt;<br>&lt;C {...subTitle}/&gt;<br>&lt;/View&gt;)<br>};</pre><p>Test — A.test.tsx</p><pre>import React from &#39;react&#39;;<br>import { fireEvent, render } from &#39;@testing-library/react-native&#39;;<br>import * as B from &#39;./B&#39;<br>import * as C from &#39;./C&#39;<br> <br> <br>const mockProps = { title: {text:&#39;blabla&#39;, analytics:{...someData}}, subTitle:{text:&#39;blabla2&#39;, analytics:{...someData}} };<br> <br>const setup = (overrides = {}) =&gt; {<br>  const props = { ...mockProps, ...overrides };<br>  const utils = render(&lt;A {...props} /&gt;);<br>  return { ...utils, props };<br>};<br> <br>describe(&#39;&lt;A/&gt;&#39;, () =&gt; {<br>  let bSpy: jest.SpyInstance;<br>  let cSpy: jest.SpyInstance;<br> <br>  beforeAll(() =&gt; {<br>    bSpy = jest.spyOn(B, &#39;B&#39;).mockImplementation(() =&gt; &lt;&gt;&lt;/&gt;);<br>    cSpy = jest.spyOn(C, &#39;C&#39;).mockImplementation(() =&gt; &lt;&gt;&lt;/&gt;);<br>  });<br> <br>  it(&#39;Should render Title and subTitle&#39;, () =&gt; {<br>    const {<br>      props: { title, subTitle },<br>    } = setup();<br> <br>    expect(bSpy).toHaveBeenCalledWith(title, {});<br>    expect(bSpy).toHaveBeenCalledTimes(1);<br> <br>    expect(cSpy).toHaveBeenCalledWith(subTitle, {});<br>    expect(cSpy).toHaveBeenCalledTimes(1);<br>  });<br>});</pre><p>We spied on components B and C and checked if they were called with the right parameters only once.</p><p>‘B’ and ‘C‘ will be unit tested separately with the same approach.</p><h3>To sum up:</h3><p>Unit testing is an important tool to protect our code, I encourage you to use our strategy of user perspective, component composition with mocking, and isolate test files in order to write tests. Our experience has shown that this approach is more efficient in terms of time, more consistent in results, and provides a higher level of confidence in our testing.</p><p>Nonetheless, I recommend that you try new strategies yourself and see what best suits your project.</p><p>Thanks for reading! What are your thoughts? Which topic in React Native would you like to read about next? Feel free to share in the comments below.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d8ad72fdb659" width="1" height="1" alt=""><hr><p><a href="https://medium.com/att-israel/practical-concepts-for-unit-testing-in-react-native-d8ad72fdb659">Practical concepts for unit testing in React Native</a> was originally published in <a href="https://medium.com/att-israel">AT&amp;T Israel Tech Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Concepts That Every React Native Developer Should Know]]></title>
            <link>https://medium.com/att-israel/concepts-that-every-react-native-developer-should-know-6e0b424d7b44?source=rss-91aaa63fe02------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e0b424d7b44</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[developer]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Barak Zadiki]]></dc:creator>
            <pubDate>Wed, 23 Mar 2022 13:08:06 GMT</pubDate>
            <atom:updated>2022-03-23T13:08:06.342Z</atom:updated>
            <content:encoded><![CDATA[<p>In 2015, Facebook launched React Native, a JavaScript library that enables the development of mobile apps that run on both iOS and Android using React.</p><p>Since then, the framework has seen an ongoing increase in adoption by the mobile development community used by tech giants such as Microsoft, Uber, Spotify, and Pinterest.</p><p>Developing large-scale projects can be challenging, and we should build a strong structure and concepts at the early stages to create a stable development environment.</p><p>After building a few React Native apps along the road, I’ve learned a few key principles that help me build cleaner and more readable code for my projects that have better performance.</p><p>In this article, I will share a few important points that, from my point of view, can help you develop better mobile apps. The article is geared towards developers at any level of React Native development.</p><p><strong>Which concepts are we going to cover?</strong></p><p>1.<strong> </strong><a href="#0a80">Feature folder</a><br>2. <a href="#6bf7">Keep the logic outside of the component</a><br>3. <a href="#8e64">Support accessibility with big/small font </a><br>4. <a href="#032d">Animations</a></p><h3>1. Feature folder</h3><p>As far as structure, React Native has no clear opinion.<br>It depends on both your personal taste and the project you’re working on.</p><p>There are two common folder structures that developers use:<br>1. PBL- Package by Layer<br>2. PBF- Package by Feature</p><p><strong>PBL- Package by Layer</strong><br> The highest-level packages reflect the various application “layers” such ascomponents, actions, reducers etc, and they are grouped by type.</p><pre>└── src<br>    ├── actions<br>    │   ├── devicesActions.ts<br>    │   ├── sharesActions.ts<br>    │   └── postsActions.ts<br>    ├── api<br>    │   └── mediumApi.ts<br>    ├── components<br>    │   ├── devices<br>    │   │   ├── Devices.tsx<br>    │   ├── shares<br>    │   │   ├── Shares.tsx<br>    │   └── posts<br>    │       ├── Posts.tsx<br>    ├── reducers<br>    │   ├── devicesReducer.ts<br>    │   ├── sharesReducer.ts<br>    │   └── postsReducer.ts<br>    └── utils<br>        ├── formatter.ts<br>        └── helper.ts</pre><p><strong>Disadvantages:</strong></p><ul><li>Lack of modularity (OOP concept) - You can’t extract the feature easily, it will be tightly coupled into our codebase.</li><li>Folders contain files that aren’t closely related to each other.</li><li>Bad code navigation - You need to jump around in the project to find the related files.</li><li>Can be hard to scale and can cause merge conflicts in big teams.</li></ul><p><strong>PBF - Package by Feature</strong></p><p>A more reasonable solution is to organize files by feature. Files related to a feature should be placed together. This increases the readability within the same module and diminishes the dependencies between the packages.</p><pre>└── src<br>    ├── api<br>    │   └── mediumApi.ts<br>    ├── features<br>    │   ├── devices<br>    │   │   ├── Devices.tsx<br>    │   │   ├── devicesActions.ts<br>    │   │   ├── devicesReducer.ts<br>    │   ├── shares<br>    │   │   ├── Shares.tsx<br>    │   │   ├── sharesActions.ts<br>    │   │   ├── sharesReducer.ts<br>    │   ├── shared<br>    │   │   └── utils<br>    │   │       └── formatter.ts<br>    │   └── posts<br>    │       ├── Posts.tsx<br>    │       ├── postsActions.ts<br>    │       └── postsReducer.ts<br>    └── shared<br>        └── utils<br>            └── helper.ts</pre><p><strong>PBF solves PBL problems</strong>:</p><ul><li>Has high modularity: Easy to remove or add features, StoryBook/Bit friendly, supports OOP concept<strong> </strong>modularity.</li><li>Easier code navigation when all related files are in the same location.</li><li>Easier to scale when each team can work on a specific feature, resulting in fewer code conflicts.</li></ul><h3><strong>2. </strong>Keep the logic outside of the component</h3><p>I have seen this behavior often when a developer writes a long component with a bunch of logic - when a new developer wants to make changes in the code, it could take days instead of hours.</p><p>React should be responsible for only rendering the UI, the logic should be managed by the redux or exported utils functions/classes, we will write it in a dedicated file, in that way your code will be more:</p><ul><li>Understandable and clean with a function name that describes the purpose of the logic.</li><li>The logic can be reused in other components too.</li><li>It’s easier to test it using a unit test when it’s an independent function.</li></ul><p>Bad 🔴</p><p>WelcomeCenter.tsx</p><pre>const WelcomeCenter = () =&gt; <br>{</pre><pre>const getPartOfTheDay = () =&gt;<br>{<br>.... return &quot;morning&quot;<br>}</pre><pre>return &lt;Text&gt;{getPartOfTheDay()}&lt;Text&gt;</pre><pre>}</pre><p>Good ✅</p><pre>// WelcomeCenter.tsx</pre><pre>import { getPartOfTheDay } from &#39;./utils/WelcomeCenterLogic&#39;;</pre><pre>const WelcomeCenter = () =&gt; <br>{</pre><pre>return &lt;Text&gt;{getPartOfTheDay()}&lt;Text&gt;</pre><pre>}</pre><pre>// WelcomeCenterLogic.tsx</pre><pre>export const getPartOfTheDay = () =&gt;<br>{<br>.... return &quot;morning&quot;<br>}</pre><h3>3. Support accessibility with big/small font</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jbhOTL8wNm0yoQX3.jpeg" /></figure><p>Handling different font sizes can be tricky if you don’t have special support. In a regular situation, when the user increases their default phone font size, the UI will look bad, the text could become distorted and run outside the container dimensions, etc.</p><p>The solution is to keep it simple using these three accessibility concepts:</p><ul><li><strong>Limit your font size</strong>, your app should have a max value for the font size and support it in this range. In our app, we support the values in the range of 0 to 2.</li></ul><pre>const MAX_FONT_SIZE_MULTIPLIER = 2</pre><pre>&lt;Text maxFontSizeMultiplier={MAX_FONT_SIZE_MULTIPLIER}&gt;Bla&lt;/Text&gt;</pre><ul><li><strong>Limit the number of lines</strong> for unimportant text (this will show as 3 dots ‘…’). If the text is important, <strong>wrap it using ScrollView</strong> and swipe up to see the hidden content.</li></ul><pre>&lt;Text numberOfLines={3}&gt;Bla&lt;/Text&gt;</pre><ul><li>Prevent creating a logic UI calculation between different font sizes. This makes the code unstable and can cause bugs or twitches of the element. The correct method is to use flex to handle spaces when you have a limited area or use margin if you want to keep the size of the spaces the same.</li></ul><h3>4. Animations</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*UdTS_DNDQXcjY159.gif" /></figure><p><em>In this article I’m not going to teach you how to use animations, I will provide you with tips on how to create your animation structure.</em></p><p>Animations have a major impact on user experience in terms of interaction and engagement. Animations can quickly become one of the key factors that make your mobile app attractive to users.</p><p>React Native comes with a built-in <a href="https://reactnative.dev/docs/animations">Animated</a> API to create animations and transitions; however, it has some limitations when enabling gesture-based interactions.</p><p><a href="https://github.com/software-mansion/react-native-reanimated"><strong>React-Native-Reanimated</strong></a></p><p>Instead of working with React Native built-in Animated API, I recommend using React-Native-Reanimated. It has full native support and runs animations directly on the <strong>native thread</strong> instead of the <strong>JavaScript thread</strong>.</p><p>The <strong>native thread</strong> helps us to perform animations on a dedicated UI thread as it sends everything about the animation to the native before starting the animation. So, we aren’t blocking the <strong>JavaScript thread</strong>, unlike using the <strong>JavaScript thread </strong>for animation. This helps in making the animations smoother.</p><p><a href="https://airbnb.io/lottie/#/"><strong>Lottie</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*6PiMMxSIpVjRRW9y" /></figure><p>Lottie is a mobile library for Android and iOS that parses <a href="http://www.adobe.com/products/aftereffects.html">Adobe After Effects</a> animations and exports it as JSON and renders them natively on mobile!</p><p>For the first time, designers can create and export beautiful animations without the help of engineers.</p><p>To use Lottie in React Native you need to install the<a href="http://lottie-react-native/"><strong>lottie-react-native</strong></a><strong> </strong>wrapper - a simple wrapper element that takes a JSON file as a <strong>source</strong>, as shown in the example below :</p><pre>import React from &#39;react&#39;;<br>import LottieView from &#39;lottie-react-native&#39;;<br><br>export default class BasicExample extends React.Component {<br>  render() {<br>    return (<br>      &lt;LottieView<br>        source={require(&#39;../path/to/animation.json&#39;)}<br>        autoPlay<br>        loop<br>      /&gt;<br>    );<br>  }<br>}</pre><p>It’s so easy!</p><p>I recommend using React-Native-Reanimated for simple gesture-based interactions on existing elements in the app such as swipe, drag, scale, scroll, etc, and using Lottie for the complicated interactions.</p><h3>To sum up:</h3><p>React Native is amazing. there is a large community, many solutions, components, structures, etc. It’s important to learn the best practices and the right design patterns, and, from there, even the sky is the limit.</p><p>I hope you’re now feeling more familiar with what goes on in a React Native app.</p><p>Thanks for reading! What are your thoughts? Which topic in React Native would you like to read about next? Feel free to share in the comments below.</p><p>Sources and credits:<br>* <a href="https://medium.com/react-native-training/lets-write-examples-with-package-by-feature-approach-on-react-native-7494b49e034c">https://medium.com/react-native-training/lets-write-examples-with-package-by-feature-approach-on-react-native-7494b49e034c</a><br>* <a href="https://hackernoon.com/package-by-features-not-layers-2d076df1964d">https://hackernoon.com/package-by-features-not-layers-2d076df1964d</a><br>* <a href="https://www.freecodecamp.org/news/how-to-structure-your-project-and-manage-static-resources-in-react-native-6f4cfc947d92">https://www.freecodecamp.org/news/how-to-structure-your-project-and-manage-static-resources-in-react-native-6f4cfc947d92</a><br>*<a href="https://www.tutorialspoint.com/object_oriented_analysis_design/ooad_object_oriented_principles.htm">https://www.tutorialspoint.com/object_oriented_analysis_design/ooad_object_oriented_principles.htm</a><br>* <a href="https://blog.bitsrc.io/top-5-animation-libraries-in-react-native-d00ec8ddfc8d">https://blog.bitsrc.io/top-5-animation-libraries-in-react-native-d00ec8ddfc8d</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e0b424d7b44" width="1" height="1" alt=""><hr><p><a href="https://medium.com/att-israel/concepts-that-every-react-native-developer-should-know-6e0b424d7b44">Concepts That Every React Native Developer Should Know</a> was originally published in <a href="https://medium.com/att-israel">AT&amp;T Israel Tech Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>