<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Agustin Fernandez, Author at asap developers</title>
	<atom:link href="https://www.asapdevelopers.com/author/agustin-fernandez/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.asapdevelopers.com/author/agustin-fernandez/</link>
	<description>San Francisco Mobile App Developers</description>
	<lastBuildDate>Mon, 13 Sep 2021 19:50:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.1</generator>

<image>
	<url>https://www.asapdevelopers.com/wp-content/uploads/2017/04/favicon-asap-1.png</url>
	<title>Agustin Fernandez, Author at asap developers</title>
	<link>https://www.asapdevelopers.com/author/agustin-fernandez/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Build Widgets for a React Native App</title>
		<link>https://www.asapdevelopers.com/how-to-build-widgets-for-a-react-native-app/</link>
		
		<dc:creator><![CDATA[Agustin Fernandez]]></dc:creator>
		<pubDate>Mon, 13 Sep 2021 19:50:40 +0000</pubDate>
				<category><![CDATA[React Native]]></category>
		<guid isPermaLink="false">https://www.asapdevelopers.com/?p=14871</guid>

					<description><![CDATA[<p>Widgets are small applications that add aesthetic appeal to your home screen while also displaying data at a glance and providing useful features. In this article, we will help you...</p>
<p>The post <a rel="nofollow" href="https://www.asapdevelopers.com/how-to-build-widgets-for-a-react-native-app/">How to Build Widgets for a React Native App</a> appeared first on <a rel="nofollow" href="https://www.asapdevelopers.com">asap developers</a>.</p>
]]></description>
										<content:encoded><![CDATA[


<p>Widgets are small applications that add aesthetic appeal to your home screen while also displaying data at a glance and providing useful features. In this article, we will help you create widgets for Android and iOS and integrate them into a React Native application.</p>



<h2><strong>Why should you develop a widget?</strong></h2>



<p>Most widgets are developed not only so users can stay up to date with relevant information and use their applications from their home screen, but also because they help those apps stand out among competitors and retain user engagement.</p>



<p>Unfortunately, widget development is not feasible using React Native alone. An alternative is to get a native widget to communicate with your React Native application, and that is what we are going to do in this guide.</p>



<h2><strong>Widgets with React Native</strong></h2>



<p>First of all, you’ll need to create your React Native app. This will be a bare React Native app; it does not use Expo. The command you will be using to set it up is</p>



<p><code>npx react-native init widgetAsap</code></p>



<p>In our case, the project has been created with <strong>react-native</strong> version 0.64.2.</p>



<p>Then, you will need just one additional dependency that will be creating the bridge between the widget and the app. This will be different for iOS and Android as we&#8217;ll show you below. </p>



<p>Let&#8217;s start with iOS. Behind the scenes, the package is just taking data from the React Native app and putting it into <em>UserDefaults</em> for Swift. According to the official <a href="https://developer.apple.com/documentation/foundation/userdefaults">docs</a>, <em>UserDefaults</em> is a persistent key-value storage. </p>



<p>Install the dependency by running the following command on your terminal:</p>



<p><code>yarn add react-native-shared-group-preferences</code></p>



<p>Let’s import it and try it out. To keep it simple, the application will just ask for a text value to be rendered into the widget. The content of the App.js file will be the following:</p>



<p><script src="https://gist.github.com/agustinfece/1510ac26321edfbdd2692c7d8eed06ec.js"></script> <b>Link:</b> <a href="https://gist.github.com/agustinfece/1510ac26321edfbdd2692c7d8eed06ec"><span style="font-weight: 400;">script</span></a></p>





<p>As you can see, <strong>SharedGroupPreferences</strong> is being imported from the library. The way to use it is to store an item using <strong>setItem</strong> with the given key, value and group. In this example, the key will be <strong>widgetKey</strong>, the value will be <strong>widgetData</strong>—a JavaScript object that will contain the user input—and, finally, <strong>group</strong> will be the name of the group that will share the information between the app and the widget. We will see them again when working on the Swift code.</p>



<p>On the other hand, for Android, we&#8217;ll use <em>SharedStorage</em>. This is imported from<strong>NativeModules</strong>, which belongs to the React Native package so we don&#8217;t need to install any additional libraries. In this case, the stored value is a serialized JavaScript object that has been converted to a string and saved using the <strong>set</strong> <em>SharedStorage</em> method.</p>



<p>That’s all you need to know about the React Native application. Now it’s time to look at some native code. Let’s start with iOS.</p>



<h2><strong>iOS widgets</strong></h2>



<p>If you are new to iOS widgets, we recommend you take a look at the official Apple <a href="https://support.apple.com/en-us/HT207122">documentation</a>. To develop one, you will need Xcode version 12 or above so it can run iOS14 and have access to <a href="https://developer.apple.com/widgets/">WidgetKit</a>. This is a framework that allows you to build widgets easily and make them compatible across the different Apple operating systems. In this case, we will be using version 12.4 of Xcode. You will also need an Apple Developer account.</p>



<p>The first step will be to create the widget extension for the app. Open the application you have just created in Xcode, go to File &gt; New &gt; Target and select <em>Widget Extension</em>. Choose your widget name and a folder will be created.</p>



<p>Now it’s time to set up your App Group. This will enable your React Native application to communicate with the widget extension. Go to the file with the “.xcodeproj” extension.</p>



<p>Select the<em> Build Settings</em> tab and search for <em>Library Search Paths</em>. Look at the value and replace <strong>swift-5.0</strong> with <strong>swift-5.2</strong>. This will prevent you from getting an Xcode error later on. </p>



<figure class="wp-block-image"><img decoding="async" loading="lazy" class="" src="https://lh4.googleusercontent.com/sEi1nEkyt-Br_Q8DT8KZrUtqRZ3RdQMkZm-GmjRPllVcDeBazY3cr7Xyt3sYkc3tTb-Cs3Pq_PZ6oLc4ml1k_Ti0k2etcdDzjobrBoF-1PpeQ0n13Fimm6d5EphKw6b9p8Sf4HTi=s0" alt="" width="767" height="128" /></figure>



<figure class="wp-block-image"><img decoding="async" loading="lazy" class="" src="https://lh6.googleusercontent.com/mFtZvpEmGgXb6rUQfNhz-nsRFnGaQlZTBYVzDcha-56vlFrzukXsPI4sSofL3GdgDROtI3o2fQXUSVmzm7nqPsFMYENPALbbJH-F2IDid2tvTHbiKpSmf7EQSwsER1UU-t1oIuYR=s0" alt="" width="767" height="374" /></figure>



<p>Also, right-click on <em>widgetApp</em> in the Project Navigator on the left and click “New File…”. Create an empty Swift file in the project and, when Xcode prompts you, press Create Bridging Header. This file won’t be used later, it&#8217;s just to avoid getting an Xcode error. </p>



<figure class="wp-block-image"><img decoding="async" loading="lazy" class="" src="https://lh4.googleusercontent.com/3qTtB9gljyHjZ3tTvF0zgRZzHmJktdEzWPy_QneI7ObAoQlryhS7ddOS7LoEBx61RnyTLkU5lUqE8ijsLSJDxjwmtd1uQ5IOqqCcxb0Z_cR4GBngTM4a3M_RqpYFCk6bhLJZk0TX=s0" alt="" width="521" height="361" /></figure>



<p>Then, select your project file and look at the targets; you should be seeing something like this: </p>



<figure class="wp-block-image"><img decoding="async" src="https://lh5.googleusercontent.com/WG73HRmxMk5dtBDD0Y4kRSLnDWqTxzeY8PSBmkDRl4PgKMQQ0ypFz2VtMnretZtQLgnn6OTfu0eRRjdXUCuLif6wkiDgz4mezDo6EOxeEYHzEiT_vfCTJUK7QSiAoHlS9fHdyHYd=s0" alt="" /></figure>



<p>You will need to add the <strong>widgetAsap</strong> and <strong>MyWidgetExtension</strong> targets so they become part of the App Group we just created. Select <strong>widgetAsap</strong> and, inside the <em>Signing &amp; Capabilities</em> section, click on <img decoding="async" loading="lazy" src="https://lh5.googleusercontent.com/eb5BdmcO8221YTfYdPnOaNaaMkWAe-wvZEOEP5sKGfKjrJcHRud_72hWN_pNFAe5B8OWBDWN6D53z-Hws6Oj1rLOizLKFJLgu4uormF-ysdEzmZm7zDd2aeolxgOejJ68AhVsLYC=s0" width="32" height="20" /> in the upper right. Select <em>App Groups</em> and type a name of your choice. We will be using group.asap. Repeat this step with <strong>MyWidgetExtension</strong>.</p>



<p>We are almost there. You now need to modify the <em>MyWidget.swift</em> file. There, you have to retrieve the data that is being stored by your React Native code. Remember you are using <em>UserDefaults</em> for this. Take a look at what the content of the file will be, as explained below.</p>



<p><script src="https://gist.github.com/agustinfece/08a6ecbfca397110442369bc9f5fd6a4.js"></script> <b>Link:</b> <a href="https://gist.github.com/agustinfece/08a6ecbfca397110442369bc9f5fd6a4"><span style="font-weight: 400;">script</span></a></p>





<p>We will delve just into what is important so you can create your widget as quickly as possible.</p>



<p>First of all, take a look at the <strong>WidgetData</strong> struct. Its type is <strong>Decodable</strong> because it is the retrieved data we will parse and display on the widget. In this example, it contains only a <strong>text</strong> property.</p>



<p>Then, we have the <strong>SimpleEntry</strong> struct. It has the <strong>TimelineEntry</strong> type, which specifies the date to display a widget (used to refresh the widget content) and can also include the information to display. In this case, we added a <strong>text</strong> value to match the one we are storing in the <em>UserDefaults</em>. </p>



<p>Now everytime that <strong>SimpleEntry</strong> is instantiated, it has our <strong>text</strong> string. This occurs in <strong>MyWidget_Previews</strong>, <strong>placeholder</strong>, <strong>getSnapshot</strong> and, the vital one, <strong>getTimeline</strong>. This function is the one that does the work here, so we will explain it step by step below. </p>



<p>It starts by initializing <em>UserDefaults</em> using the App Group we created:</p>



<p><code>let userDefaults = UserDefaults.init(suiteName: "group.asap")</code></p>



<p>Then, it retrieves the value from the <strong>widgetKey</strong> we defined in the React Native code:</p>



<p><code>let savedData = userDefaults!.value(forKey: "widgetKey")</code></p>

<p>After that, it decodes the data received and defines how often our widget will refresh:</p>



<p><code>let parsedData = try? decoder.decode(WidgetData.self, from: data!)</code></p>



<p><code>let nextRefresh = Calendar.current.date(byAdding: .minute, value: 5, to: entryDate)!</code></p>



<p>In this case, it will be five minutes, the lowest value available.</p>



<p>With that information, it creates a <strong>SimpleEntry</strong> and passes it to set the Timeline. This will provide the widget&#8217;s new content and indicate when to carry out the update. </p>



<p>The last step is to style the widget. This is up to you. In this case, we decided to keep it simple. The MyWidgetEntryView struct is the one that takes care of styling.</p>



<p>You can test your widget by running your application using <strong>npm start</strong> and using a virtual or real device. Once the application has been installed, select the widget from the widget list and place it on the home screen.</p>



<figure class="wp-block-image"></figure>



<figure class="wp-block-image"></figure>



<p><img decoding="async" loading="lazy" class="alignnone wp-image-14887" src="https://www.asapdevelopers.com/wp-content/uploads/2021/09/phone1-283x300.png" alt="Widgets - React Native - ios" width="423" height="448" srcset="https://www.asapdevelopers.com/wp-content/uploads/2021/09/phone1-283x300.png 283w, https://www.asapdevelopers.com/wp-content/uploads/2021/09/phone1.png 514w" sizes="(max-width: 423px) 100vw, 423px" /></p>
<p>Open the app or the widget itself and you will see something like this: </p>



<figure class="wp-block-image"></figure>



<figure class="wp-block-image"></figure>



<p><img decoding="async" loading="lazy" class="alignnone size-medium wp-image-14888" src="https://www.asapdevelopers.com/wp-content/uploads/2021/09/phone2-258x300.png" alt="" width="258" height="300" srcset="https://www.asapdevelopers.com/wp-content/uploads/2021/09/phone2-258x300.png 258w, https://www.asapdevelopers.com/wp-content/uploads/2021/09/phone2.png 471w" sizes="(max-width: 258px) 100vw, 258px" /></p>
<p>Enter something in the input field, hit the <em>enter</em> key and return to the home screen. Remember we set the refresh time to five minutes so it will take a while to display the text you have just entered. Be patient! </p>





<p><img decoding="async" loading="lazy" class="alignnone wp-image-14889" src="https://www.asapdevelopers.com/wp-content/uploads/2021/09/phone3-141x300.png" alt="Widgets - React Native - ios2" width="193" height="411" srcset="https://www.asapdevelopers.com/wp-content/uploads/2021/09/phone3-141x300.png 141w, https://www.asapdevelopers.com/wp-content/uploads/2021/09/phone3.png 246w" sizes="(max-width: 193px) 100vw, 193px" /></p>
<p>And that’s it for iOS. Let’s see how to achieve the same results on the Android platform. </p>



<h2><strong>Android widgets</strong></h2>



<p>If you are new to Android widgets, we recommend you take a look at the official Android <a href="https://developer.android.com/guide/topics/appwidgets/overview">documentation</a>.</p>



<p>Luckily, This process will be easier than in iOS. The approach will be the same, storing the data we want to display from React Native into the widget and, then, retrieving it using native code (Java, in this case).</p>



<p>Begin by opening the project in Android Studio. We will be using version 4.2.</p>



<figure class="wp-block-image"><img decoding="async" loading="lazy" class="alignnone" src="https://lh6.googleusercontent.com/01vOgd7Liq7MMrF7YTVO3krrmklJd2WERY5cI-NWpfwOKP6aLEJJedX8F64mpB1XqR6Wf8SXQ32u1zxKUbhILzI91ubgK3_8QXrZaR9D49K54_qNRYUApdNsvIEBDYsJ_ET0IBK9=s0" alt="Widgets - React Native - build in android" width="558" height="479" /></figure>



<p>Then, right click on the <em>res</em> folder and select the option shown above. Name your widget class however you want and click on <em>Finish</em>. We will name it “MyWidget”, like we did for iOS.</p>



<p>A new <em>MyWidget.java</em> file will open. Just as we used <em>UserDefaults</em> for iOS, we will be using the <em>SharedPreferences</em> Android native module.</p>



<p>To do this, you will first need to implement the bridge between the React Native app and the widget. Add the new <em>SharedStorage.java</em> and <em>SharedStoragePackager.java</em> files to the same directory as your <em>MainApplication.java</em>.</p>



<p>SharedStorage.java</p>



<p><script src="https://gist.github.com/agustinfece/18f453ac98aeb377d2cb72c92d3b9e3a.js"></script> <b>Link:</b> <a href="https://gist.github.com/agustinfece/18f453ac98aeb377d2cb72c92d3b9e3a"><span style="font-weight: 400;">script</span></a></p>





<p>SharedStoragePackager.java</p>



<p><script src="https://gist.github.com/agustinfece/df045da6725449cba5cfc5558f9476ba.js"></script> <b>Link:</b> <a href="https://gist.github.com/agustinfece/df045da6725449cba5cfc5558f9476ba"><span style="font-weight: 400;">script</span></a></p>





<p>Note that the application package is being imported in the first line of both files. Change the name to your app’s; you can find it in your <em>AndroidManifest.xml</em> file inside android &gt; app &gt; src &gt; main. </p>



<p>Once you have placed those files and made the relevant changes, add this to your <em>MainApplication.java</em> file inside the <strong>getPackages</strong> method:</p>



<p><code>packages.add(new SharedStoragePackager());</code></p>



<p>This will autolink the package we have just created.</p>



<p>Now that we have established the bridge, it’s time to receive that data in <em>MyWidget.java</em>. We need to tell the widget to update its content using <em>SharedPreferences</em>. This is managed by <strong>updateAppWidget</strong>. Replace your code with this one:</p>



<p><script src="https://gist.github.com/agustinfece/08bea07bcc021aae778668acbd3c6e42.js"></script> <b>Link:</b> <a href="https://gist.github.com/agustinfece/08bea07bcc021aae778668acbd3c6e42"><span style="font-weight: 400;">script</span></a></p>



<p>The last step is optional and has to do with the widget styling. We will keep the same design as in the iOS example. Using Android Studio, go to your app &gt; res &gt; layout &gt; my_widget.xml file, click on the “Example” text and you will see the following:</p>



<figure class="wp-block-image"><img decoding="async" loading="lazy" class="alignnone" src="https://lh4.googleusercontent.com/-ch6aEgDgI7j5w4aObq7TxcEr_b0E-vR3XT_2d9GdCFGYSPF3lRlWEVaUH76XXURUfhxnscbmKMSsrEUFqDAcvBG1JgQv8rZ2dyw8avl2ehmxqxqooiwhHCiPsUCWYFf3kDwY0wC=s0" alt="Widgets - React Native - android code" width="563" height="324" /></figure>



<p>To the right, you will see the attributes being used. In this example, changing <strong>textStyle</strong>, <strong>textColor</strong> and <strong>background</strong> is enough. </p>



<p>And that’s it. Try testing it using an Android device, either virtual or real, to confirm you have followed this guide properly. </p>



<p>Find the widget of the application:</p>





<p><img decoding="async" loading="lazy" class="alignnone wp-image-14890" src="https://www.asapdevelopers.com/wp-content/uploads/2021/09/android1-173x300.png" alt="Widgets - React Native - android" width="262" height="454" srcset="https://www.asapdevelopers.com/wp-content/uploads/2021/09/android1-173x300.png 173w, https://www.asapdevelopers.com/wp-content/uploads/2021/09/android1.png 328w" sizes="(max-width: 262px) 100vw, 262px" /></p>
<p>Place it where you want and open the application. Write something in the input field, hit the <em>enter</em> key and return to the home screen. </p>



<figure class="wp-block-image"></figure>



<figure class="wp-block-image"></figure>



<p><img decoding="async" loading="lazy" class="alignnone  wp-image-14891" src="https://www.asapdevelopers.com/wp-content/uploads/2021/09/android2-300x267.png" alt="" width="422" height="376" srcset="https://www.asapdevelopers.com/wp-content/uploads/2021/09/android2-300x267.png 300w, https://www.asapdevelopers.com/wp-content/uploads/2021/09/android2.png 615w" sizes="(max-width: 422px) 100vw, 422px" /></p>
<p>In this case, the widget has refreshed the content immediately:</p>





<p><img decoding="async" loading="lazy" class="alignnone wp-image-14892" src="https://www.asapdevelopers.com/wp-content/uploads/2021/09/android3-169x300.png" alt="Widgets - React Native - android" width="223" height="396" srcset="https://www.asapdevelopers.com/wp-content/uploads/2021/09/android3-169x300.png 169w, https://www.asapdevelopers.com/wp-content/uploads/2021/09/android3.png 320w" sizes="(max-width: 223px) 100vw, 223px" /></p>
<p>Congratulations! You&#8217;ve reached the end of this guide for widgets using React Native. You have successfully built a widgets application for the iOS and Android platforms that communicates with a React Native application. We hope this can help you take full advantage of this functionality.</p>



<p><strong>Final thoughts on Widgets for React Native </strong></p>



<p>This simplified application allows you to elevate key content and features from your app so users can see them at a glance. In spite of being combined with a native approach, it wasn’t that difficult to integrate it into a <a href="https://www.asapdevelopers.com/cross-platform-frameworks-vs-native-code-development/">React Native</a> application.</p>



<p>We hope this guide has left you with teachings that you can put into practice for your applications in order to take advantage of the benefits we&#8217;ve mentioned, as it could be particularly valuable if your app is part of a business.</p>

		<div id="fws_69397b29cd12a"  data-column-margin="default" data-midnight="dark"  data-bg-mobile-hidden="" class="wpb_row vc_row-fluid vc_row top-level standard_section "  style="padding-top: 0px; padding-bottom: 0px; "><div class="row-bg-wrap" data-bg-animation="none" data-bg-overlay="false"><div class="inner-wrap"><div class="row-bg"  style=""></div></div><div class="row-bg-overlay" ></div></div><div class="col span_12 dark left">
	<div  class="vc_col-sm-12 wpb_column column_container vc_column_container col no-extra-padding"  data-t-w-inherits="default" data-border-radius="none" data-shadow="none" data-border-animation="" data-border-animation-delay="" data-border-width="none" data-border-style="solid" data-border-color="" data-bg-cover="" data-padding-pos="all" data-has-bg-color="false" data-bg-color="" data-bg-opacity="1" data-hover-bg="" data-hover-bg-opacity="1" data-animation="" data-delay="0" >
		<div class="vc_column-inner" ><div class="column-bg-overlay-wrap" data-bg-animation="none"><div class="column-bg-overlay"></div></div>
			<div class="wpb_wrapper">
				<a class="nectar-button large regular accent-color has-icon  wpb_animate_when_almost_visible wpb_bounce bounce regular-button"  style="margin-top: 40px; color: #ffffff; "  href="https://www.asapdevelopers.com/react-native-san-francisco/" data-color-override="false" data-hover-color-override="false" data-hover-text-color-override="#fff"><span>React Native Development Company</span><i style="color: #ffffff;"  class="icon-button-arrow"></i></a>
			</div> 
		</div>
	</div> 
</div></div>
<p>The post <a rel="nofollow" href="https://www.asapdevelopers.com/how-to-build-widgets-for-a-react-native-app/">How to Build Widgets for a React Native App</a> appeared first on <a rel="nofollow" href="https://www.asapdevelopers.com">asap developers</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>React Native AsyncStorage</title>
		<link>https://www.asapdevelopers.com/react-native-asyncstorage/</link>
		
		<dc:creator><![CDATA[Agustin Fernandez]]></dc:creator>
		<pubDate>Mon, 05 Jul 2021 18:31:41 +0000</pubDate>
				<category><![CDATA[React Native]]></category>
		<guid isPermaLink="false">https://www.asapdevelopers.com/?p=14667</guid>

					<description><![CDATA[<p>According to the official React Native docs, their AsyncStorage module has been deprecated. Instead, they recommend using one of the community packages. This is why, in this guide, we have...</p>
<p>The post <a rel="nofollow" href="https://www.asapdevelopers.com/react-native-asyncstorage/">React Native AsyncStorage</a> appeared first on <a rel="nofollow" href="https://www.asapdevelopers.com">asap developers</a>.</p>
]]></description>
										<content:encoded><![CDATA[


<p>According to the official React Native <a href="https://reactnative.dev/docs/asyncstorage">docs</a>, their AsyncStorage module has been deprecated. Instead, they recommend using one of the <a href="https://reactnative.directory/?search=storage">community packages</a>. This is why, in this guide, we have chosen to cover one of these:</p>



<p><code>@react-native-async-storage/async-storage</code></p>



<h2><strong>What does this package do? </strong></h2>



<p>Its formal definition states that this library is an asynchronous, unencrypted, persistent, key-value storage API. </p>



<p>Well, those are a lot of adjectives for a storage API. Let&#8217;s break this terminology down. <strong>Asynchronous</strong> means that each of its methods returns an object that could be a <em>Promise</em> or an <em>Error</em>. It’s <strong>unencrypted</strong>, so beware of using it to store sensitive data. Being <strong>persistent</strong> means it saves the data, even when the user has closed the API or even the device. Finally, <strong>key-value</strong> pairs are the way data is stored.</p>



<p>Now that we know what it is, let&#8217;s see how useful it is.</p>



<h2><strong>Why should you use it?</strong></h2>



<p>First of all, it&#8217;s a lightweight solution that doesn’t involve a database, server or even an internet connection. It sounds like a good choice, but remember it is just one more alternative for storing information and sharing data in your application; it may not always be the best one to use. </p>



<p>So the really important question would be when to use this library, rather than why you should use it. Based on the characteristics we mentioned, you can deduce when it is appropriate to use AsyncStorage and when it is not such a good idea.</p>



<p>It is usually replaced by <a href="https://reactjs.org/docs/context.html">React Context</a> or <a href="https://redux.js.org/">Redux</a>, but despite being used for the same purpose, they are not the same. Mainly because React Context and Redux store data in memory that is erased when the app closes and AsyncStorage saves data persistently in the device. It resembles the local storage used for web applications.</p>



<h2><strong>How to use it </strong></h2>



<p>There are three main actions you can take with AsyncStorage. <strong>Set</strong> a key-value pair, <strong>get</strong> a value of a given key and <strong>remove</strong> the pair using its key. The API methods used for this are <em>setItem</em>, <em>getItem</em> and <em>removeItem</em>, respectively. You can delve into the <a href="https://react-native-async-storage.github.io/async-storage/docs/api">API</a> if you are interested, but we will only use these main ones in the following example. </p>



<h2><strong>Example of React Native AsyncStorage</strong></h2>



<p>Let’s do a proof of concept. Our application will store the user’s theme preference, whether light or dark. For this purpose, we are not compromising data scalability, sensitive information or anything else we may be worried about. </p>



<p>Start by installing <em>create-react-native-app</em> globally with <em>yarn</em> by running this command:</p>



<p><code>yarn global add create-react-native-app</code></p>



<p>Now, you can run <em>create-react-app asyncstorapp</em> to create your <a href="https://www.asapdevelopers.com/?s=react+native">React Native</a> application. We decided to use “asyncstorapp”, but you can name it however you want. Follow the instructions and you are done. In our case, the project was created with <em>react-native</em> version 0.63.4.</p>



<p>Proceed to install the package by running:</p>



<p><code>yarn add @react-native-async-storage/async-storage</code></p>



<p>We will be using the 1.15.5 version. According to the official <a href="https://react-native-async-storage.github.io/async-storage/docs/install/">installation guide</a>, running <em>npx pod-install</em> is needed to link this dependency.</p>



<p>You can now enter <em>npm start</em> in your terminal and use a real or virtual device to start testing what we are about to develop.</p>



<p>Let’s import it and try it out. As a first approach, we will store a hardcoded key-value pair, then close the app and open it again to prove it’s being saved. </p>



<p>To start, add the following to your App.js file and then look at the console.</p>

<p><script src="https://gist.github.com/agustinfece/11aa7650a9d64c3f8a01446b16cd71bd.js"></script> <b>Link:</b> <a href="https://gist.github.com/agustinfece/11aa7650a9d64c3f8a01446b16cd71bd"><span style="font-weight: 400;">script</span></a></p>

<p>Remember it is asynchronous, so that’s why try/catch and async/await are used to handle the Promise in <em>getItem</em>.</p>



<p>As you can see, <em>value null</em> has been outputted. That’s because we haven’t set a value for that key yet. So let’s create an asynchronous <em>setItem</em> function that does it.</p>



<p>Also, let’s change our code so instead of getting the <em>theme</em> value in the <em>useEffect</em>, we store it. </p>



<p>Keep in mind that AsyncStorage can only store string values. In order to store object data you need to serialize it first. You can do this by using <em>JSON.stringify()</em> to store it and <em>JSON.parse() </em>to retrieve it.</p>



<p>In this case, we will be using just a string, so it should look like this:</p>

<p><script src="https://gist.github.com/agustinfece/d2af11af4a1de3a78b89421309af22f7.js"></script> <b>Link:</b> <a href="https://gist.github.com/agustinfece/d2af11af4a1de3a78b89421309af22f7"><span style="font-weight: 400;">script</span></a></p>

<p>To test if the <em>dark</em> value has been stored correctly, change the <em>useEffect</em> again to its previous value. Save your file and you will see <em>value dark</em> outputted in your terminal.</p>



<p>Let’s close and reopen the application to test if that value has been saved to our device. You will see the same value in your terminal. This proves it’s really working.</p>



<p>Now that we know it works, it’s time to store and use our application theme properly. Replace your code with the following:</p>

<p><script src="https://gist.github.com/agustinfece/110af48536dae7cf43659d250b4fa2d2.js"></script> <b>Link:</b> <a href="https://gist.github.com/agustinfece/110af48536dae7cf43659d250b4fa2d2"><span style="font-weight: 400;">script</span></a></p>
<p>&nbsp;</p>

<p>You will see something like this, working like a light switch, lighting up when turned on and darkening when turned off:</p>





<figure class="wp-block-image"><img decoding="async" loading="lazy" class=" wp-image-14677 aligncenter" src="https://www.asapdevelopers.com/wp-content/uploads/2021/07/Screenshot-278x300.png" alt="react native asyncstorage" width="405" height="437" srcset="https://www.asapdevelopers.com/wp-content/uploads/2021/07/Screenshot-278x300.png 278w, https://www.asapdevelopers.com/wp-content/uploads/2021/07/Screenshot.png 573w" sizes="(max-width: 405px) 100vw, 405px" /></figure>



<p>And that’s it! Close the app keeping in mind the previous state and reopen it so you can confirm that it remembers it.</p>



<h2><strong>Final thoughts on React Native AsyncStorage</strong></h2>



<p>This package fulfills a need when you wish to recover data without depending on anything other than your device. However, it also has some drawbacks. </p>



<p>Since it is a storage option that is on your device until it is manually removed, we find it very useful as long as the data you are storing or sharing in your application has been carefully selected. When it comes to saving sensitive data or large amounts of it, we would recommend choosing another alternative.</p>

		<div id="fws_69397b29ce854"  data-column-margin="default" data-midnight="dark"  data-bg-mobile-hidden="" class="wpb_row vc_row-fluid vc_row standard_section "  style="padding-top: 0px; padding-bottom: 0px; "><div class="row-bg-wrap" data-bg-animation="none" data-bg-overlay="false"><div class="inner-wrap"><div class="row-bg"  style=""></div></div><div class="row-bg-overlay" ></div></div><div class="col span_12 dark left">
	<div  class="vc_col-sm-12 wpb_column column_container vc_column_container col no-extra-padding"  data-t-w-inherits="default" data-border-radius="none" data-shadow="none" data-border-animation="" data-border-animation-delay="" data-border-width="none" data-border-style="solid" data-border-color="" data-bg-cover="" data-padding-pos="all" data-has-bg-color="false" data-bg-color="" data-bg-opacity="1" data-hover-bg="" data-hover-bg-opacity="1" data-animation="" data-delay="0" >
		<div class="vc_column-inner" ><div class="column-bg-overlay-wrap" data-bg-animation="none"><div class="column-bg-overlay"></div></div>
			<div class="wpb_wrapper">
				<a class="nectar-button large regular accent-color has-icon  wpb_animate_when_almost_visible wpb_bounce bounce regular-button"  style="margin-top: 40px; color: #ffffff; "  href="https://www.asapdevelopers.com/react-native-san-francisco/" data-color-override="false" data-hover-color-override="false" data-hover-text-color-override="#fff"><span>React Native Development Company</span><i style="color: #ffffff;"  class="icon-button-arrow"></i></a>
			</div> 
		</div>
	</div> 
</div></div>
<p>The post <a rel="nofollow" href="https://www.asapdevelopers.com/react-native-asyncstorage/">React Native AsyncStorage</a> appeared first on <a rel="nofollow" href="https://www.asapdevelopers.com">asap developers</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Build a React Native Login app with Node.js Backend</title>
		<link>https://www.asapdevelopers.com/build-a-react-native-login-app-with-node-js-backend/</link>
		
		<dc:creator><![CDATA[Agustin Fernandez]]></dc:creator>
		<pubDate>Mon, 28 Jun 2021 19:46:40 +0000</pubDate>
				<category><![CDATA[Node.js]]></category>
		<category><![CDATA[React Native]]></category>
		<guid isPermaLink="false">https://www.asapdevelopers.com/?p=14636</guid>

					<description><![CDATA[<p>&#160; In this guide, we will cover how to connect React Native and Node.js to develop a login application. Once logged in, users will be able to access a private...</p>
<p>The post <a rel="nofollow" href="https://www.asapdevelopers.com/build-a-react-native-login-app-with-node-js-backend/">Build a React Native Login app with Node.js Backend</a> appeared first on <a rel="nofollow" href="https://www.asapdevelopers.com">asap developers</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>&nbsp;</p>



<p>In this guide, we will cover how to connect React Native and Node.js to develop a login application. Once logged in, users will be able to access a private resource. We won&#8217;t go into too much detail, so you can get started as soon as possible.</p>



<p>Before we start, let’s take a look at why we chose to use these technologies. You should know that prior knowledge of both tools is required to complete this guide successfully.</p>



<h2><strong>Why React Native? </strong></h2>



<p>According to the official <a href="https://reactnative.dev/">docs</a>, “<em>React Native combines the best parts of native development with React, a best-in-class JavaScript library for building user interfaces.</em>”</p>



<p>To put it more simply, React Native is the adaptation of React for native development. But let’s analyze what that really means.</p>



<p>React Native is a framework based on React that uses JavaScript to build mobile applications that render natively, for Android and iOS at the same time. </p>



<p>Therefore, this tool allows web developers to take advantage of React’s outstanding benefits and apply their prior JavaScript knowledge but, in this case, to target mobile platforms.</p>



<h2><strong>Why Node.js?</strong> </h2>



<p>The official <a href="https://nodejs.org/es/">documentation</a> claims that “<em>Node.js is a JavaScript runtime built with Chrome&#8217;s V8 JavaScript engine.</em>” </p>



<p>Being a runtime means it acts as an environment that allows you to execute, in this case, a JavaScript program on your machine. Moreover, it uses a non-blocking I/O model to respond to events from a user or from another application, which makes it lightweight and efficient.</p>



<p>Moreover, its package ecosystem, npm, has the greatest number of open-source libraries in the world.</p>



<p>Now that we know which technologies we are going to use, let&#8217;s get started.</p>



<h3><strong>Create the Node.js project</strong></h3>



<p>Let&#8217;s talk about our backend and what its role will be. It will receive requests from the frontend and, in turn, give a response according to the result of the data flow. Behind the scenes, it will be interacting with the database, creating the users when they sign up and verifying their credentials when they attempt to log in.</p>



<h3><strong>Requirements</strong></h3>



<p>To be able to follow this part of the guide, you must have Node installed. We will be using <em>node</em> v14.16.1 and the <em>npm</em> 6.14.12 version.</p>



<p>To store our users in the database, we will be using MySQL Community Server as well as MySQL Workbench, so you will need them both as well. You can use another database if you want. If you do, you can skip the next step. </p>



<h3><strong>How to setup MySQL </strong><img decoding="async" loading="lazy" src="https://lh3.googleusercontent.com/4iqxMheIU0bKjitZ085HqYrxdiOvwxFh_fexrF-jvxuipnkni4dENvlWpOvmmWSAk69c2X9VnilOHU45KV0L0VF731ttnxppHZ8qlkBJRb7l0ohYaZOL0aIzuSpX4k3dTCPUtCD0" width="29" height="29" /></h3>



<p>Start by downloading MySQL Community Server from their <a href="https://dev.mysql.com/downloads/">website</a>. Follow the installation instructions and that’s it. Then, MySQL Workbench will be needed to manage our database. Download it from the same site and install it. We will use versions 8.0.25 and 8.0.12, respectively.</p>



<p>When you have finished, you will see a MySQL local instance running. To connect to the database, click on that instance and you will be asked to enter a password. Remember the password, as well as the username and the port your local instance is running on. You will need that later. </p>



<p>Now, it’s time to create our database. Right click below “Schemas&#8221; and choose “Create schema”. We will name it “loginDB”, but you can choose whichever name you prefer. Click “Apply” and the database will be created. </p>



<p>In the left panel, you will see the database we have just created. Inside it, there is a “Tables” section. Right click it and select “Create table”. Select the configuration below and click “Apply” to finish the setup.</p>



<figure class="wp-block-image"><img decoding="async" src="https://lh4.googleusercontent.com/AKXBdC_nEsaDoELNT4VoEvYRoY8psEzsrTjItZRG_mlpPSeYOCFEhBaCe2HuWAcB_P-PfgdCP_Bei-mLXxzWRCnNvKWg3aLr9qA1NBuA9PJkRtvP1BTgQVTunFgacPu-OjJfbxV9" alt="" /></figure>



<p>Once you are done, you can start by creating the backend project and installing the necessary dependencies. </p>



<p>First, we will create a “loginApp” folder and, inside it, another one called “server” where our backend will be placed. Folder names are up to you, you can name them however you want. Then, run the <em>npm init </em>command to initialize <em>npm</em> and proceed to install the required dependencies listed below. To install them, we will be using <em>yarn</em> version 1.22.10 and running the <em>yarn add &lt;package_name&gt;</em> command. we will summarize what each dependency does below, in case you are not familiar with all of them.</p>



<figure class="wp-block-table">
<table>
<tbody>
<tr>
<td>Package</td>
<td>Version</td>
<td>Description</td>
</tr>
<tr>
<td>bcryptjs</td>
<td>2.4.3</td>
<td>Hashes and compares user passwords </td>
</tr>
<tr>
<td>express</td>
<td>4.17.1</td>
<td>Minimal and flexible framework to be used</td>
</tr>
<tr>
<td>jsonwebtoken</td>
<td>8.5.1</td>
<td>Generates and verifies request tokens</td>
</tr>
<tr>
<td>mysql2</td>
<td>2.2.5</td>
<td>Allows you to interact with the MySQL database</td>
</tr>
<tr>
<td>nodemon</td>
<td>2.0.7</td>
<td>Automatically restarts the application when file changes in the directory are detected</td>
</tr>
<tr>
<td>sequelize</td>
<td>6.6.2</td>
<td>ORM to be used</td>
</tr>
</tbody>
</table>
</figure>



<p>After installing them, go to your package.json file and, in the <em>scripts</em> section, change <em>start</em> from <em>node app.js</em> to <em>nodemon app.js</em>. You can now run <em>npm start</em> and <em>nodemon</em> will be activated.</p>



<p>Enough with the preparations, let’s code. In the “server” folder, create an app.js file that will initialize the application and create and hold the environment configuration. Place this in your file:</p>



<h4>app.js</h4>

<p><script src="https://gist.github.com/agustinfece/849b79e6a43fdbbdf0d74301c07b4326.js"></script> <b>Link:</b> <a href="https://gist.github.com/agustinfece/849b79e6a43fdbbdf0d74301c07b4326"><span style="font-weight: 400;">script</span></a></p>

<p>From now on, pay close attention to the imports in each file to understand how they are connected. </p>



<p>As you may see, in the first line of our app.js, we are importing <em>express</em> and, at the bottom, we are specifying the port where our server will be listening for requests. Also, two files we haven’t talked about yet are being imported. app.js will connect what they provide to our application.</p>



<p>The first one is database.js and it is inside a “utils” folder. Here, we will create an instance of <em>sequelize</em> that will let us connect to our MySQL database. Copy the following content and replace your password where it says <em>YOUR_PASSWORD</em>.</p>



<h4>database.js</h4>

<p><script src="https://gist.github.com/agustinfece/fc9891e62aa048531ce3ab55fd607e45.js"></script> <b>Link:</b> <a href="https://gist.github.com/agustinfece/fc9891e62aa048531ce3ab55fd607e45"><span style="font-weight: 400;">script</span></a></p>

<p>The second imported script is inside a “routes” folder and is named routes.js. This is the one that defines our API endpoints using <em>express</em>, as follows: </p>



<p>routes.js</p>

<p><script src="https://gist.github.com/agustinfece/8059754189c582a0d991149f3a2ed27c.js"></script> <b>Link:</b> <a href="https://gist.github.com/agustinfece/8059754189c582a0d991149f3a2ed27c"><span style="font-weight: 400;">script</span></a></p>

<p>We are only going to use the first three endpoints, the other two are examples you may need to define a public route and a default one.</p>



<p>You may note that, in this script, another file came into play in the imports. Its name is auth.js and it is placed inside the “controllers” folder. This file contains the actions that get executed when those requests are received.</p>



<p>So let&#8217;s see what auth.js should include:</p>



<h4>auth.js</h4>

<p><script src="https://gist.github.com/agustinfece/1dfc5d8bbd3c2f0d42bd0bc82bebbbfa.js"></script> <b>Link:</b> <a href="https://gist.github.com/agustinfece/1dfc5d8bbd3c2f0d42bd0bc82bebbbfa"><span style="font-weight: 400;">script</span></a></p>

<p>Again, looking at the imports, we can see that there are two new packages: <em>bcryptjs</em> and <em>jsonwebtoken</em>. </p>
<p>&nbsp;</p>



<p>There is also another file we haven’t seen yet. Let’s look at what it should include before continuing with auth.js. </p>



<p>The file name is user.js and it is inside a “models” folder. It should import the <em>sequelize </em>instance from the database.js file we already saw and, then, define the <em>users </em>table we are going to store. Copying this into your file will be enough:s</p>
<p>

</p>
<h4>user.js</h4>

<p><script src="https://gist.github.com/agustinfece/a5d6f50870058df7ff5e27aa8c25ba9d.js"></script> <b>Link:</b> <a href="https://gist.github.com/agustinfece/a5d6f50870058df7ff5e27aa8c25ba9d"><span style="font-weight: 400;">script</span></a></p>
<p>&nbsp;</p>

<p>Going back to the auth.js file, we can see three functions are being exported: <em>signup</em>, <em>login</em> and <em>isAuth</em>. These will be in charge of resolving the requests defined in routes.js.</p>



<p><em>signup</em> registers users in the database. First, it checks if the email provided has already been registered. Then, if the email and password have been received, it hashes the password and stores the user in the database. </p>



<p><em>login</em> handles login requests. It starts by checking if the email corresponds to a database user. If that’s the case, it hashes the password and compares it to the user password in the database. If they match, it responds with a temporary secret token. </p>



<p><em>isAuth</em> asks for the secret token and, if it is provided, proceeds to verify it. If everything goes well, it will finally respond with the private resource. In this case, that resource will be just a message containing “here is your resource”. This is enough for our proof of concept.</p>



<p>That’s it. We are done for now. We have our server ready to respond to authentication requests in port 5000 after running <em>npm start</em>. You can test the endpoints defined using Postman or another tool of your choice.</p>



<p><strong>Create the React Native project</strong></p>



<p>It&#8217;s time to talk about the frontend and what its role in this project should be. It will receive the user input and send a request to the backend, to sign up or log in, depending on what the user has selected. Then, based on the backend response, it will show an error or success message to the user.</p>



<p>So let’s start coding. The <em>create-react-native-app</em> package will set up the project for you. First, run the following command to install it globally: <em>yarn global add create-react-native-app</em></p>



<p>Then, go to your “loginApp” folder and execute the command we’ve just installed by running <em>create-react-native-app mobile</em>. Again, we decided to use “mobile” as the name of the frontend app, but it’s up to you. </p>



<p>This step is optional since your app’s style is up to you. Inside your new “mobile” folder, create a new folder called “public” and another one named “images” inside it. That last folder will contain the background image for the app. We downloaded the one we will be using from <a href="https://www.freepik.com/free-vector/white-abstract-background_11852424.htm">here</a> and we had to rotate it to achieve a portrait orientation.</p>



<p>Then, create another folder inside “mobile” called “screens”. Inside it, create two files, “index.js” and “AuthScreen.js”. The first one will be the one that exports the screen like this:</p>



<h4>index.js</h4>

<p><script src="https://gist.github.com/agustinfece/56083e1b477a6c77921c438407d68dab.js"></script> <b>Link:</b> <a href="https://gist.github.com/agustinfece/56083e1b477a6c77921c438407d68dab"><span style="font-weight: 400;">script</span></a></p>

<p>The second one will contain our unique screen, where the user can get authenticated. It will use <em>fetch</em> to send the user requests to the backend and return a message according to what it receives. There will be one request sending user input and creating the signup or login, and another one requesting the private resource. Once the user logs in, the resource request will automatically be triggered.</p>



<p>We will be using a single screen for the entire app to make it simpler, but you can try a different approach if you want. Copy its content to your AuthScreen.js file:</p>



<h4>AuthScreen.js</h4>

<p><script src="https://gist.github.com/agustinfece/16c420233703f3e622829ba24d527aa9.js"></script> <b>Link:</b> <a href="https://gist.github.com/agustinfece/16c420233703f3e622829ba24d527aa9"><span style="font-weight: 400;">script</span></a></p>

<p>Finally, we need to import that screen to our App.js file and render it as follows:</p>



<h4>App.js</h4>

<p><script src="https://gist.github.com/agustinfece/b62d065d24ad099edcf8d7ab18f657fb.js"></script> <b>Link:</b> <a href="https://gist.github.com/agustinfece/b62d065d24ad099edcf8d7ab18f657fb"><span style="font-weight: 400;">script</span></a></p>
<p>&nbsp;</p>

<p>And that’s all. Just run <em>npm start</em> on the mobile directory and the frontend app will start running on port 3000. Then, proceed to use a virtual or real device to test how it communicates with the backend. You should see something like this:</p>





<figure class="wp-block-image"><img decoding="async" loading="lazy" class="alignnone wp-image-14649" src="https://www.asapdevelopers.com/wp-content/uploads/2021/06/test1-139x300.png" alt="react native sign up" width="313" height="676" srcset="https://www.asapdevelopers.com/wp-content/uploads/2021/06/test1-139x300.png 139w, https://www.asapdevelopers.com/wp-content/uploads/2021/06/test1.png 302w" sizes="(max-width: 313px) 100vw, 313px" /> <img decoding="async" loading="lazy" class="alignnone wp-image-14650" src="https://www.asapdevelopers.com/wp-content/uploads/2021/06/test2-137x300.png" alt="node js login" width="308" height="674" srcset="https://www.asapdevelopers.com/wp-content/uploads/2021/06/test2-137x300.png 137w, https://www.asapdevelopers.com/wp-content/uploads/2021/06/test2.png 299w" sizes="(max-width: 308px) 100vw, 308px" /></figure>



<p>Let’s test if it works:</p>





<figure class="wp-block-image"><img decoding="async" loading="lazy" class="alignnone wp-image-14655" src="https://www.asapdevelopers.com/wp-content/uploads/2021/06/test3-138x300.png" alt="react native sign up verification" width="311" height="676" srcset="https://www.asapdevelopers.com/wp-content/uploads/2021/06/test3-138x300.png 138w, https://www.asapdevelopers.com/wp-content/uploads/2021/06/test3.png 300w" sizes="(max-width: 311px) 100vw, 311px" /><img decoding="async" loading="lazy" class="alignnone wp-image-14656" src="https://www.asapdevelopers.com/wp-content/uploads/2021/06/test4-136x300.png" alt="node js login verification" width="306" height="676" srcset="https://www.asapdevelopers.com/wp-content/uploads/2021/06/test4-136x300.png 136w, https://www.asapdevelopers.com/wp-content/uploads/2021/06/test4.png 294w" sizes="(max-width: 306px) 100vw, 306px" /></figure>



<p>Let’s test error messages by creating a user with the same email and trying to log in with the wrong credentials:</p>





<figure class="wp-block-image"><img decoding="async" loading="lazy" class="alignnone  wp-image-14657" src="https://www.asapdevelopers.com/wp-content/uploads/2021/06/test5-139x300.png" alt="" width="305" height="658" srcset="https://www.asapdevelopers.com/wp-content/uploads/2021/06/test5-139x300.png 139w, https://www.asapdevelopers.com/wp-content/uploads/2021/06/test5.png 302w" sizes="(max-width: 305px) 100vw, 305px" /><img decoding="async" loading="lazy" class="alignnone wp-image-14658" src="https://www.asapdevelopers.com/wp-content/uploads/2021/06/test6-140x300.png" alt="node js login error" width="307" height="658" srcset="https://www.asapdevelopers.com/wp-content/uploads/2021/06/test6-140x300.png 140w, https://www.asapdevelopers.com/wp-content/uploads/2021/06/test6.png 302w" sizes="(max-width: 307px) 100vw, 307px" /></figure>



<p>It works perfectly! After you finish this guide, your directory should look like this:</p>



<figure class="wp-block-image"><img decoding="async" loading="lazy" class="alignnone" src="https://lh6.googleusercontent.com/rycan7rVcPS6OY0icgW32A1DARqFYLljOEeQKtGtbJUmmtD3t8cebgi4xbbp5Or28yvaY5WOD5fbcuJwm4A1m8JnRPqC_O1uBU8qOmsTIfbZMsvMep2MMhhCJMSEbOikrzntXDXw" alt="react native" width="295" height="1092" /></figure>



<p>You have reached the end of this guide. Now you have a React Native frontend login application working with a Node.js backend. Congratulations!</p>



<h2><strong>Challenge: Develop tests for your React Native App </strong></h2>



<p>We encourage you to test your application on both sides to ensure it is working as expected. Testing is an essential part of any development process and you should start incorporating it into your projects from the beginning. </p>



<p>You can start with the frontend side, testing components in the way a user would use them. We recommend using <a href="https://testing-library.com/">React Testing Library</a>. If you haven’t heard about it, you can read the <a href="https://www.asapdevelopers.com/react-testing-library/">article</a> we have written about it.</p>



<h2><strong>Final thoughts on React Native with Node.js backend</strong></h2>



<p>To sum up, both React Native and Node.js are powerful tools that share a single programming language: JavaScript. With their separate advantages combined, you have built an incredibly strong mobile application. We have proven how straightforward it is to get started with any of them, even if you have not seen it before. We hope this guide gives you a starting point to start creating mobile apps with more functionalities.</p>



<p><a href="https://github.com/agustinfece/login"><strong>GitHub</strong></a></p>

		<div id="fws_69397b29d07c4"  data-column-margin="default" data-midnight="dark"  data-bg-mobile-hidden="" class="wpb_row vc_row-fluid vc_row standard_section "  style="padding-top: 0px; padding-bottom: 0px; "><div class="row-bg-wrap" data-bg-animation="none" data-bg-overlay="false"><div class="inner-wrap"><div class="row-bg"  style=""></div></div><div class="row-bg-overlay" ></div></div><div class="col span_12 dark left">
	<div  class="vc_col-sm-12 wpb_column column_container vc_column_container col no-extra-padding"  data-t-w-inherits="default" data-border-radius="none" data-shadow="none" data-border-animation="" data-border-animation-delay="" data-border-width="none" data-border-style="solid" data-border-color="" data-bg-cover="" data-padding-pos="all" data-has-bg-color="false" data-bg-color="" data-bg-opacity="1" data-hover-bg="" data-hover-bg-opacity="1" data-animation="" data-delay="0" >
		<div class="vc_column-inner" ><div class="column-bg-overlay-wrap" data-bg-animation="none"><div class="column-bg-overlay"></div></div>
			<div class="wpb_wrapper">
				<a class="nectar-button large regular accent-color has-icon  wpb_animate_when_almost_visible wpb_bounce bounce regular-button"  style="margin-top: 40px; color: #ffffff; "  href="https://www.asapdevelopers.com/react-native-san-francisco/" data-color-override="false" data-hover-color-override="false" data-hover-text-color-override="#fff"><span>React Native Development Company</span><i style="color: #ffffff;"  class="icon-button-arrow"></i></a>
			</div> 
		</div>
	</div> 
</div></div>
<p>The post <a rel="nofollow" href="https://www.asapdevelopers.com/build-a-react-native-login-app-with-node-js-backend/">Build a React Native Login app with Node.js Backend</a> appeared first on <a rel="nofollow" href="https://www.asapdevelopers.com">asap developers</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>React Testing Library</title>
		<link>https://www.asapdevelopers.com/react-testing-library/</link>
		
		<dc:creator><![CDATA[Agustin Fernandez]]></dc:creator>
		<pubDate>Mon, 11 Jan 2021 18:31:02 +0000</pubDate>
				<category><![CDATA[React]]></category>
		<guid isPermaLink="false">https://www.asapdevelopers.com/?p=14117</guid>

					<description><![CDATA[<p>React Testing Library Testing is an essential part of any development process and there are countless tools available depending on what you are looking to test. In this article, we’ll...</p>
<p>The post <a rel="nofollow" href="https://www.asapdevelopers.com/react-testing-library/">React Testing Library</a> appeared first on <a rel="nofollow" href="https://www.asapdevelopers.com">asap developers</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><b>React Testing Library</b></h1>
<p><span style="font-weight: 400;">Testing is an essential part of any development process and there are countless tools available depending on what you are looking to test. In this article, we’ll focus on the React Testing Library and cover what it is, as well as why and how to use it.</span></p>
<h2><b>What is React Testing Library? </b></h2>
<p><span style="font-weight: 400;">The React Testing Library is a library built as part of an open-source project named </span><a href="https://testing-library.com/"><span style="font-weight: 400;">Testing Library</span></a><span style="font-weight: 400;">. It is part of a family of packages named </span><a href="https://www.npmjs.com/org/testing-library"><span style="font-weight: 400;">@testing-library</span></a><span style="font-weight: 400;"> that provides guidance to test UI components in a user-centric way.</span></p>
<p><span style="font-weight: 400;">Just as React Testing Library has its own package, there are specific ones for other frameworks, including </span><a href="https://testing-library.com/docs/react-native-testing-library/intro"><span style="font-weight: 400;">React Native</span></a><span style="font-weight: 400;">, </span><a href="https://testing-library.com/docs/angular-testing-library/intro"><span style="font-weight: 400;">Angular</span></a><span style="font-weight: 400;">, and </span><a href="https://testing-library.com/docs/vue-testing-library/intro"><span style="font-weight: 400;">Vue</span></a><span style="font-weight: 400;">. All of these have been built on top of </span><span style="font-weight: 400;">DOM Testing Library</span><span style="font-weight: 400;">, the so-called “core library”, by adding a specific ergonomic API for each framework. </span></p>
<p><span style="font-weight: 400;">Here, we will focus on the React package, since it is the most popular among its </span><a href="https://github.com/testing-library"><span style="font-weight: 400;">GitHub</span></a><span style="font-weight: 400;"> pinned repositories.</span><span style="font-weight: 400;"> </span></p>
<h2><b>Why should you use it?  </b></h2>
<p><span style="font-weight: 400;">Testing Library relies on the belief that </span><i><span style="font-weight: 400;">“The more your tests resemble the way your software is used, the more confidence they can give you.”</span></i><span style="font-weight: 400;"> </span></p>
<p><span style="font-weight: 400;">In other words, this light-weight library produces tests that use components closer to the way a user would, giving the developer more confidence that their application will work when it goes out to real users.</span></p>
<p><span style="font-weight: 400;">End users don&#8217;t care what happens behind the scenes, they just see and interact with the output. So, instead of getting hung up writing code that tests a component’s internal API, </span><span style="font-weight: 400;"><strong>props</strong> </span><span style="font-weight: 400;">or </span><strong>state</strong><span style="font-weight: 400;">, you should be writing tests that verify it works properly from a user’s perspective, regardless of how it’s implemented.</span></p>
<p><span style="font-weight: 400;">Last but not least, this way of testing makes tests more maintainable, since code refactors won’t break them. </span></p>
<h2><b>How to use React Testing Library  </b></h2>
<p><span style="font-weight: 400;">We’ll be taking a high-level view at this library, just to get you started from scratch. It is far from being a full library explanation, so </span><a href="https://www.robinwieruch.de/react-testing-library"><span style="font-weight: 400;">here</span></a><span style="font-weight: 400;"> is a complete and detailed guide recommended by the official docs in case you want to take a deeper look.</span></p>
<p><span style="font-weight: 400;">Note: this example was developed using </span><span style="font-weight: 400;"><strong>create-react-app</strong><span style="font-weight: 400;">, functional components, </span><strong>react</strong><span style="font-weight: 400;"> version 17.0.1 and version 11.2.2 of <strong>@testing-library/react</strong><span style="font-weight: 400;">.</span></span></span></p>
<p><span style="font-weight: 400;">The latest </span><strong>create-react-app</strong><span style="font-weight: 400;"> setups already come with </span><span style="font-weight: 400;"><strong>@testing-library</strong>/react</span><span style="font-weight: 400;"> installed. If this is not the case for the version you’re working with, start by adding the following package to the project:</span></p>
<p>&nbsp;</p>
<p><span style="font-weight: 400;"><code>npm install --save-dev @testing-library/react</code></span></p>
<p>&nbsp;</p>
<p><span style="font-weight: 400;">React Testing Library works along with Jest. When running </span><strong>npm test</strong><span style="font-weight: 400;">, all files with a .</span><i><span style="font-weight: 400;">test.js</span></i><span style="font-weight: 400;"> suffix will be recognized by the Jest test runner. So, an example for our test file would be </span><i><span style="font-weight: 400;">App.test.js</span></i><span style="font-weight: 400;">. Create an </span><i><span style="font-weight: 400;">App.test.js </span></i><span style="font-weight: 400;">file if it has not been included yet with </span><strong>create-react-app</strong><span style="font-weight: 400;">. </span><span style="font-weight: 400;">In this example, the files’ content looked like this:</span><span style="font-weight: 400;"> </span></p>
<p>&nbsp;</p>
<h3><b>App.js</b></h3>
<p><script src="https://gist.github.com/agustinfece/50f57ac347916b43c1b85deec7322b85.js"></script><br />
<b>Link:</b> <a href="https://gist.github.com/agustinfece/50f57ac347916b43c1b85deec7322b85"><span style="font-weight: 400;">script</span></a></p>
<h3></h3>
<h3><b>App.test.js</b></h3>
<p><script src="https://gist.github.com/agustinfece/cc4095cfa8c064dd9d607bd8dcceca46.js"></script><br />
<b>Link:</b> <a href="https://gist.github.com/agustinfece/cc4095cfa8c064dd9d607bd8dcceca46"><span style="font-weight: 400;">script</span></a></p>
<p>&nbsp;</p>
<p><span style="font-weight: 400;">This example has a single test suite, </span><span style="font-weight: 400;"><strong>renders learn react link</strong>,</span><span style="font-weight: 400;"> with just one assertion: </span><strong>expect</strong><span style="font-weight: 400;">. It uses the </span><span style="font-weight: 400;">render </span><span style="font-weight: 400;">function to <strong>render</strong> a React component and then queries the UI to verify if a text that matches the regular expression has been rendered.</span></p>
<p><span style="font-weight: 400;">After running all tests with </span><strong>npm test</strong><span style="font-weight: 400;">, it passes successfully:</span></p>
<p><span style="font-weight: 400;"><img decoding="async" loading="lazy" class="alignnone wp-image-14133" src="https://www.asapdevelopers.com/wp-content/uploads/2021/01/npm-36-300x116.png" alt="react testing library 36" width="569" height="220" srcset="https://www.asapdevelopers.com/wp-content/uploads/2021/01/npm-36-300x116.png 300w, https://www.asapdevelopers.com/wp-content/uploads/2021/01/npm-36-1024x395.png 1024w, https://www.asapdevelopers.com/wp-content/uploads/2021/01/npm-36-768x296.png 768w, https://www.asapdevelopers.com/wp-content/uploads/2021/01/npm-36.png 1068w" sizes="(max-width: 569px) 100vw, 569px" /></span></p>
<p><span style="font-weight: 400;">Let’s modify the </span><i><span style="font-weight: 400;">App.js </span></i><span style="font-weight: 400;">file to add a button and then fire an event on the </span><i><span style="font-weight: 400;">App.test.js </span></i><span style="font-weight: 400;">in a different test suite. To do so, the </span><strong>fireEvent</strong><span style="font-weight: 400;"> function will be used like this:</span></p>
<p>&nbsp;</p>
<h3><b>App.js</b></h3>
<p><script src="https://gist.github.com/agustinfece/f7e22eb7c90b546ce2124a647a5d8d6b.js"></script><br />
<b>Link:</b> <a href="https://gist.github.com/agustinfece/f7e22eb7c90b546ce2124a647a5d8d6b"><span style="font-weight: 400;">script</span></a></p>
<h3><b>App.test.js</b></h3>
<p><script src="https://gist.github.com/agustinfece/e8d994f9362299cd8a538ddb3eda204e.js"></script><br />
<b>Link:</b> <a href="https://gist.github.com/agustinfece/e8d994f9362299cd8a538ddb3eda204e"><span style="font-weight: 400;">script</span></a></p>
<p>&nbsp;</p>
<p><span style="font-weight: 400;">What we’ve done here is simulate a user’s behavior, checking what happened in the application UI after that event. After executing </span><strong>npm test</strong><span style="font-weight: 400;">, it passes again.</span></p>
<p><span style="font-weight: 400;"><img decoding="async" loading="lazy" class="alignnone wp-image-14132" src="https://www.asapdevelopers.com/wp-content/uploads/2021/01/npm-300x115.png" alt=" npm 60" width="556" height="213" srcset="https://www.asapdevelopers.com/wp-content/uploads/2021/01/npm-300x115.png 300w, https://www.asapdevelopers.com/wp-content/uploads/2021/01/npm-1024x392.png 1024w, https://www.asapdevelopers.com/wp-content/uploads/2021/01/npm-768x294.png 768w, https://www.asapdevelopers.com/wp-content/uploads/2021/01/npm.png 1098w" sizes="(max-width: 556px) 100vw, 556px" /></span></p>
<p><span style="font-weight: 400;">There are more ways to test your application using this library; only the basics were shown in this example. Because of how this library works, rather than dealing with instances of rendered React components, tests will work with actual DOM nodes. Ultimately, you are using JavaScript but with a React output.</span></p>
<p>&nbsp;</p>
<h2><b>Bonus: Why not use Enzyme? </b></h2>
<p><span style="font-weight: 400;">According to the official </span><a href="https://testing-library.com/docs/react-testing-library/migrate-from-enzyme/"><span style="font-weight: 400;">docs</span></a><span style="font-weight: 400;">, the React Testing Library is suggested as a replacement for another well-known testing library: </span><a href="http://airbnb.io/enzyme/"><span style="font-weight: 400;">Enzyme</span></a><span style="font-weight: 400;">.  This is because <a href="https://www.asapdevelopers.com/testing-a-react-web-app-using-jest-and-enzyme/">Enzyme</a> fosters some bad testing practices, encouraging developers to test </span><span style="font-weight: 400;">implementation like we mentioned above.</span> <a href="https://kentcdodds.com/blog/testing-implementation-details"><span style="font-weight: 400;">Here</span></a><span style="font-weight: 400;"> is an article written by the React Testing Library founder delving into why you should not test implementation details, and </span><a href="https://testing-library.com/docs/react-testing-library/migrate-from-enzyme#how-to-migrate-from-enzyme-to-react-testing-library"><span style="font-weight: 400;">here</span></a><span style="font-weight: 400;"> is an official guide explaining how to migrate from Enzyme to the React Testing Library.</span></p>
<p>&nbsp;</p>
<h2><b>Final thoughts on React Testing Library</b></h2>
<p><span style="font-weight: 400;">Without a doubt, we fully recommend this library because of its primary purpose: giving the developer confidence by testing components in the way a user would use them. </span><span style="font-weight: 400;">Give this library a try if you have the chance. If you prefer a different framework, you can use one of the other libraries offered by the Testing Library project. You won’t regret it!</span></p>
<div id="fws_69397b29d1b66"  data-column-margin="default" data-midnight="dark"  data-bg-mobile-hidden="" class="wpb_row vc_row-fluid vc_row standard_section "  style="padding-top: 0px; padding-bottom: 0px; "><div class="row-bg-wrap" data-bg-animation="none" data-bg-overlay="false"><div class="inner-wrap"><div class="row-bg"  style=""></div></div><div class="row-bg-overlay" ></div></div><div class="col span_12 dark left">
	<div  class="vc_col-sm-12 wpb_column column_container vc_column_container col no-extra-padding"  data-t-w-inherits="default" data-border-radius="none" data-shadow="none" data-border-animation="" data-border-animation-delay="" data-border-width="none" data-border-style="solid" data-border-color="" data-bg-cover="" data-padding-pos="all" data-has-bg-color="false" data-bg-color="" data-bg-opacity="1" data-hover-bg="" data-hover-bg-opacity="1" data-animation="" data-delay="0" >
		<div class="vc_column-inner" ><div class="column-bg-overlay-wrap" data-bg-animation="none"><div class="column-bg-overlay"></div></div>
			<div class="wpb_wrapper">
				<a class="nectar-button large regular accent-color has-icon  wpb_animate_when_almost_visible wpb_bounce bounce regular-button"  style="margin-top: 40px; color: #ffffff; "  href="https://www.asapdevelopers.com/web-mobile-app-san-francisco/" data-color-override="false" data-hover-color-override="false" data-hover-text-color-override="#fff"><span>Mobile App Developers</span><i style="color: #ffffff;"  class="icon-button-arrow"></i></a>
			</div> 
		</div>
	</div> 
</div></div>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://www.asapdevelopers.com/react-testing-library/">React Testing Library</a> appeared first on <a rel="nofollow" href="https://www.asapdevelopers.com">asap developers</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
