<?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 Bhavesh Daswani on Medium]]></title>
        <description><![CDATA[Stories by Bhavesh Daswani on Medium]]></description>
        <link>https://medium.com/@digitaldaswani?source=rss-fb8784950c62------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*i9O_RXvlFUxFzoxRLrke7w.jpeg</url>
            <title>Stories by Bhavesh Daswani on Medium</title>
            <link>https://medium.com/@digitaldaswani?source=rss-fb8784950c62------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 18:45:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@digitaldaswani/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[Closures in Javascript]]></title>
            <link>https://medium.com/@digitaldaswani/closures-in-javascript-8ab46b78fccc?source=rss-fb8784950c62------2</link>
            <guid isPermaLink="false">https://medium.com/p/8ab46b78fccc</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[closure]]></category>
            <category><![CDATA[closures-functions]]></category>
            <dc:creator><![CDATA[Bhavesh Daswani]]></dc:creator>
            <pubDate>Sun, 24 Nov 2019 10:59:20 GMT</pubDate>
            <atom:updated>2019-11-24T10:59:20.605Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*m6HxwtjtbzOaI7TJ.jpg" /></figure><p>There is two main pillar of javascript, which makes javascript much powerful and are closures and prototype inheritance. After understanding these two principles, you will unlock the real power of javascript.</p><p>Till now when I am writing this blog, javascript is the most used and popular language in the world but some of its concepts are still confusing like closures. I think after reading this blog one of the most confusing topic closures will be cleared to you.</p><p>Closures concept depends on two things</p><ul><li>Function in javascript is first-class citizen</li><li>Javascript has lexical scope</li></ul><p><strong>What do I mean by Function are first class citizen lets understand</strong></p><p>In javascript function can be assigned to a variable which is called function expression, Function in javascript can be passed as a parameter to other function or can be the return value of the function invocation</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c567322d6eb4edd9d595b3e04d93de9/href">https://medium.com/media/2c567322d6eb4edd9d595b3e04d93de9/href</a></iframe><p>Above is the example of a function expression</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c30780c1f5cdb5bede000d5dec61612c/href">https://medium.com/media/c30780c1f5cdb5bede000d5dec61612c/href</a></iframe><p>The above is the example of Function as parameter and function as a return value</p><p><strong>Lexical scope</strong></p><p>Lexical means where the code has been written and scope which variable are available to the function. I have return blog on scope in javascript, if you want to know in detail about scope then you can check for it</p><p><a href="https://medium.com/@bhaveshdaswani93/scopes-in-javascript-cd3ddd5a17d1">Scopes in Javascript</a></p><p><strong>What is closure</strong></p><p>A closure means the function has access to the variable which are defined in the parent function also if the parent function has been removed from the call stack</p><p>Let&#39;s understand with an example</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b66bf712d2ee093f0e76806e0d67df0/href">https://medium.com/media/3b66bf712d2ee093f0e76806e0d67df0/href</a></iframe><p>Function ‘a’ has variable grandFather, when function a is executed and removed from the stack the variable grandFather is not removed from the memory heap because it is referenced inside c function. In memory heap a closure is created where grandFather variable leaves until c function is not executed the variable grandFather will not be removed from the memory map</p><p>Similar happens with variable father defined inside function b, after function b is executed and removed from the call stack, the variable father is placed along with grandFather in the same closure</p><p>Now when function c is executed it tries to access variable grandFather, first it looks inside its variable envrionment and grandFather variable is not defined inside c function so it is not available in the variable environment of c function, then function c looks inside the closure to find the value of grandFather variable where it gets resolved and same happens with variable father, for variable son it is resolved from variable environment of function c, as it is defined inside function c</p><p>So this is how closure works in javascript.</p><p><strong>Benefits of Closure</strong></p><ul><li>Memory Efficient</li><li>Encapsulation</li></ul><p><strong>Memory Efficient:</strong></p><p>Closures are memory efficient. Let’s understand this with example</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c753baeb48e872892069779dd62acbe/href">https://medium.com/media/5c753baeb48e872892069779dd62acbe/href</a></iframe><p>In withOutClosure function, whenever it is called our data set(arr variable) is created and then when function execution finishes the function is removed from the stack as well as the data(arr variable) is removed as it is not referenced anywhere else so the garbage collector will remove the variable. It is not memory efficient because every time the function is called the heavy operation is executed to generate the data set is executed which is not memory efficient. Here Closure comes to the rescue</p><p>In withClosure example, the variable array is created once and on each execution of findFromIndex we are referencing arr variable from closure. So the creation of the variable again on each execution is avoided</p><p><strong>Encapsulation</strong></p><p>What is encapsulation. <strong>encapsulation </strong>means a protective shield that prevents the data from being accessed by the code outside that shield. Let’s understand this by example</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b2bfcff97ebd24cd72871a2212c821de/href">https://medium.com/media/b2bfcff97ebd24cd72871a2212c821de/href</a></iframe><p>In the above example we have variable protectData that has to be protected means we don&#39;t want to give direct access to manipulate it, we want to provide only readability, so we have created a function accessData which return the value of protected variable, This is encapsulation and is all possible because of closure as on execution of the function encapEg will be removed from the stack after its execution is done but the protectedData will not be removed because it is referenced by the accessTheData function and its value will be saved in the closure.</p><p>I hope this article would help you to understand closure and its benefit</p><p>For more updates follow me on <a href="https://twitter.com/Bhavesh_Dev93">Twitter</a> and <a href="https://github.com/bhaveshdaswani93">Github</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ab46b78fccc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scopes in Javascript]]></title>
            <link>https://medium.com/@digitaldaswani/scopes-in-javascript-cd3ddd5a17d1?source=rss-fb8784950c62------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd3ddd5a17d1</guid>
            <category><![CDATA[execution-context]]></category>
            <category><![CDATA[lexical-scope]]></category>
            <category><![CDATA[lexical-environment]]></category>
            <category><![CDATA[scopes]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Bhavesh Daswani]]></dc:creator>
            <pubDate>Sun, 17 Nov 2019 10:33:46 GMT</pubDate>
            <atom:updated>2019-11-17T10:33:46.495Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-QBZo6Pu4Aoa2OVg.jpg" /><figcaption>Scopes in Javascript</figcaption></figure><p>Are you having a difficult time understanding scopes in javascript then this blog is for you here I will explain to you all about scopes in javascript. After reading this whole post I am damn sure you will surely master scopes in javascript.</p><p>Before understanding scopes, let&#39;s understand few core concepts of javascript that are related to scopes.</p><p><strong>Execution Context</strong></p><p>Let&#39;s see the example to understand more clearly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FbU5j2luQLNsM08QC3OJsw.png" /><figcaption>Execution Context Example</figcaption></figure><p>This is a simple code which print return my name. But this is the best example to understand execution context. Each time a function is called an execution context is created Like below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b120gFYiPBtT4jDc2J1Zew.png" /></figure><p>Whenever javascript sees the these () bracket that is an invocation of the function it creates an execution context and adds it to the call stack. Here sayMyName is invoked, so javascript creates an execution context of sayMyName and add it to the call stack, the sayMyName function calls findName function and an execution context is being created for findName function which is added to call stack finally printName is called so its execution context is also added to the stack. When printName function execution is finished by returning the name it is popped off from the stack and then findName is popped off and then sayMyName is popped off.</p><p>Below is the Empty call stack when all the execution context are popped off the stack</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wYycS6TjooiIAA9kNERphg.png" /><figcaption>Empty call stack</figcaption></figure><p>The above scenario is almost right but one thing is missing is the global execution context</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bsvsXYEi7jJk6acuWW__OA.png" /><figcaption>With global Execution</figcaption></figure><p>Whenever javascript engine runs over code, It creates a global execution context under which our code gets executed. The global execution context is popped off from the stack when our last line of the code gets executed. For the global execution context, we can say that it is the first thing that is pushed to call stack by javascript engine and is the last execution context to be popped off from the stack.</p><blockquote>The most important line I want to say is that: whenever any code is executed it is executed inside an execution context.</blockquote><p>Let&#39;s summarize what we have learned whenever over code is run inside the javascript engine it provides a global execution context inside which over code is run and when we run a function a new execution context is created.</p><p><strong>Lexical Environment</strong></p><p>Lexical Environment means where you write your code not how it get executed. Let’s see an example</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*isasqR2xs8iVQ9Kw9wvCzg.png" /><figcaption>Lexical Environment Example</figcaption></figure><p>Each Execution context has its own lexical environment.</p><p>In the Global execution context, our lexical environment contains findName and sayMyName function.</p><p>findName execution context has its own lexical environment which contains printName function</p><p>printName execution context has its own lexical environment which contains logMyName function.</p><p>let me repeat myself that lexical environment defines where your code is written and each execution context has its own lexical environment.</p><p><strong>Variable Environment</strong></p><p>Which variables reside inside the execution context is defined by the variable environment. All variable defined in your code has memory allocation in-memory heap but which variables are attached to execution context is defined by variable environment, Let’s see an example</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R_RfcIbzGYuNPjDf_UjIeQ.png" /><figcaption>Execution context with a variable environment</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3xchEBuc7r087Cs_mXVJDg.png" /><figcaption>Variable environment example</figcaption></figure><p>In this example, we have isValid variable in three different variable environment</p><p>On line 12 we have variable isValid with value false in global variable environment. On line 7 we have variable isValid with the value true in ‘one’ function variable environment.On Line 2 we have variable isValid with the value undefined in ‘two’ function variable environment.</p><p>By seeing the value of console.log in the output you can understand how important is to understand the variable environment.</p><p>So by now, we have understood what is execution context, lexical environment, variable environment. These are the core concept after clearing these terms we can easily understand scopes. Lets now discuss scopes.</p><p><strong>Scope in Javascript</strong></p><p><strong>In javascript, our lexical scope ( available data + variable depends on where the function was defined) determines our available variables. Not where the function is called (dynamic scope)</strong></p><p>Let&#39;s see an example to better understand it</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RpQd2LIT_DkUBK0LnbkLOw.png" /><figcaption>Example to understand scope</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*acSG0Vq0wvpBzNWdMa6xww.png" /><figcaption>scope chain</figcaption></figure><p>In the above example, we will try to understand the scope and scope chain</p><p>As we know that each function has its own execution context and variable environment, Let&#39;s distinguish this in the example</p><p>sayMyName: It variable environment has ‘a’ variable</p><p>findName: It variable environment has ‘b’ variable</p><p>printName: It variable environment has ‘c’ variable</p><p>In javascript whenever we want to access variable first it sees in its own variable environment if it&#39;s not there then it looks iteratively in the parent’s variable enviroment until it finds the desired variable</p><p>For example, if we want to access variable ‘a’ inside printName function, how execution will flow let see:</p><p>Firstly javascript engine will look into printName variable environment in which its not there, then it will look into the printName parent variable enviroment that is findName variable environment, in which it is not defined, then it will look into findName parent variable environment that is sayMyName variable environment which has ‘a’ variable defined in its variable environment, from here the value is resolved. This scope chain lookup goes upto global variable environment.</p><p>Now we have understood the scope and scope chain, lets discuss one more important topic.</p><p><strong>Function scope vs block scope</strong></p><p>Remember scope means what variable we have access to and javascript has function scope, every time we create a function we create an execution context that has its own variable environment, from es6, javascript included block scope. Block scope is created when it sees curly braces ({}) for example if block, for block creates a block scope. Let see an example of this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ItTrJvm-SOg7hMOQsJ6L1g.png" /><figcaption>Example of function scope vs block scope</figcaption></figure><p>Here we have created two function loop and loopBlockScope, The loop function uses var in for loop and we are accessing its value outside the for loop as ‘var’ respect function scope and we are inside the function so its value will be printed. In loopBlockScope, we are having the same logic as in loop function except we are using let which respect block scope, so accessing a variable outside the block scope as we are doing on line 14 will generate an error.</p><p>The key thing to remember is that var respect function scope while let and const respect block scope</p><p>I hope by know you have understood scope in javascript.</p><blockquote>You can follow me on <a href="https://twitter.com/Bhavesh_Dev93">Twitter </a>and <a href="https://github.com/bhaveshdaswani93">Github</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd3ddd5a17d1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is Service Container in Laravel]]></title>
            <link>https://medium.com/@digitaldaswani/what-is-service-container-in-laravel-d0dff465f4e0?source=rss-fb8784950c62------2</link>
            <guid isPermaLink="false">https://medium.com/p/d0dff465f4e0</guid>
            <category><![CDATA[laravel]]></category>
            <dc:creator><![CDATA[Bhavesh Daswani]]></dc:creator>
            <pubDate>Sun, 10 Nov 2019 05:54:19 GMT</pubDate>
            <atom:updated>2019-11-10T05:54:19.056Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/900/0*q4nIwetql9UVtRBS.jpg" /><figcaption>Service container in laravel</figcaption></figure><p>Let me introduce you to the core component of the laravel that is service container.</p><p><strong>What is Service container</strong></p><p>Service container is a powerful tool for managing class dependencies and performing dependency injection. Its sole responsibility is to manage the dependencies in your laravel project.</p><p><strong>What is dependency injection</strong></p><p>Dependency Injection means that class dependencies are “injected” into the class via the constructor or, in some cases, “setter” methods. Mostly constructor is used for injecting the dependency.</p><p>Service container is like a container where we define how the dependency should be resolved. We have to register the dependencies into the service container during the initialization of the framework and the best place to do it is the service provider.</p><p><strong>Number of Ways of Registering the Dependency</strong></p><p>Now as we have understood what is service container, what is dependency injection, lets now focus on the number of ways to register the dependency in service container.</p><blockquote>We have to register the dependency in register method of service provider</blockquote><pre>&lt;?php<br><br>namespace App\Providers;<br><br>use Illuminate\Support\ServiceProvider;<br>use App\Services\Contracts\ExampleContract;<br>use App\Services\NormalDependency;<br>use App\Services\ViaInterfaceDependency;<br>use App\Services\SingleTonDependency;<br><br><br><br>class AppServiceProvider extends ServiceProvider<br>{<br>    <em>/**<br>     * Register any application services.<br>     *<br>     * </em><strong><em>@return </em></strong><em>void<br>     */<br>    </em>public function register()<br>    {<br>        $this-&gt;app-&gt;bind(NormalDependency::class,function($app){<br>            return new NormalDependency();<br>        });<br><br>        $this-&gt;app-&gt;singleton(SingleTonDependency::class,function($app){<br>           return new SingleTonDependency();<br>        });<br><br>        $this-&gt;app-&gt;bind(ExampleContract::class,function($app){<br>           return new ViaInterfaceDependency();<br>        });<br><br><br>    }<br><br>    <em>/**<br>     * Bootstrap any application services.<br>     *<br>     * </em><strong><em>@return </em></strong><em>void<br>     */<br>    </em>public function boot()<br>    {<br>        //<br>    }<br>}</pre><p>For Demo purpose I have created NormalDependency, SingleTonDependency, ViaInterfaceDependency class and ExampleContract interface (Name of the class or interface are up to you)</p><pre>$this-&gt;app-&gt;bind(NormalDependency::class,function($app){<br>            return new NormalDependency();<br>        });</pre><p>The above bind method registers the NormalDependency class into the service container. Whenever this dependency is resolved from the container, each time a new instance of this class will be provided.</p><pre>$this-&gt;app-&gt;singleton(SingleTonDependency::class,function($app){<br>           return new SingleTonDependency();<br>        });</pre><p>The above singleton method register SingleTonDependency class into the service container. Here Whenever this dependency is resolved from the container, each time the same instance of this class will be provided. A new instance is provided only for the first time resolution of this class, every next resolution will provide the same instance of the class.</p><pre>$this-&gt;app-&gt;bind(ExampleContract::class,function($app){<br>           return new ViaInterfaceDependency();<br>        });</pre><p>The above is not different from the earlier discussed bind method, it&#39;s just the different technique to resolve the class using interface. This comes to help while resolving the dependency when we tell to service container that give me such class that implements a particular interface. Above same example would also work with singleton method.</p><p><strong>Number of Ways of Resolving the Dependency</strong></p><p>Now we have understood how to register the dependencies to the container, now let’s understand how to resolve them</p><p>Laravel supports automatic dependency injection in the constructor of the class limited to controllers, event listeners, middleware and to the handle method of the queued jobs. For automatic dependency injection, the dependencies have to be type hinted</p><p>If you have a scenario where you want to resolve dependency other then automatic injection then you have to do them manually using the resolve helper method, the resolve method expects dependency name as a parameter.</p><p>Let&#39;s see example</p><pre>&lt;?php<br><br>namespace App\Http\Controllers;<br><br>use App\Services\NormalDependency;<br>use App\Services\SingleTonDependency;<br>use Illuminate\Http\Request;<br><br>class ResolveDependenciesController extends Controller<br>{<br>    private $normalDependency;<br>    private $manualDependency;<br><br>    public function __construct(NormalDependency $normalDependency)<br>    {<br>        $this-&gt;normalDependency = $normalDependency;<br>    }<br><br><br>    public function resolveDependencyManually()<br>    {<br>        $this-&gt;manualDependency = resolve(SingleTonDependency::class);<br>    }<br>}</pre><p>Above is the demo example of the controller to understand the resolving of dependency from the service container. In the constructor for automatic injection, I have type hinted NormalDependency and in the resolveDependencyManually method I have used resolve helper method for resolving SingleTonDependency class manually from the service container.</p><p>One Important note for when to bind dependency manually in service container</p><ul><li>When your dependency depends on interface that is give me such class that implements this interface</li><li>Or your dependency class need extra configuration to be instanciated like passing additional parameters to the constructor of the dependency class</li></ul><p>If your dependency is simple like we have in this example like NormalDependency such type of dependency does not required to be binded manually in service provider because they are resolved automatically by service container using reflection.</p><p>Useful links</p><p>Github link for example used in this Blog: <a href="https://github.com/bhaveshdaswani93/Laravel-Service-Container">https://github.com/bhaveshdaswani93/Laravel-Service-Container</a></p><p>Service Container: <a href="https://laravel.com/docs/6.x/container">https://laravel.com/docs/6.x/container</a></p><blockquote>For regular updates follow me on <a href="https://twitter.com/Bhavesh_Dev93">Twitter </a>and <a href="https://github.com/bhaveshdaswani93">Github</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d0dff465f4e0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Handling forms with React Native using Formik and Yup]]></title>
            <link>https://medium.com/@digitaldaswani/handling-forms-with-react-native-using-formik-and-yup-d452ef06fe0f?source=rss-fb8784950c62------2</link>
            <guid isPermaLink="false">https://medium.com/p/d452ef06fe0f</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[formik]]></category>
            <category><![CDATA[yup]]></category>
            <dc:creator><![CDATA[Bhavesh Daswani]]></dc:creator>
            <pubDate>Sun, 03 Nov 2019 19:46:02 GMT</pubDate>
            <atom:updated>2019-11-04T15:14:52.119Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*UCIdhBXLeQ2_syY6.png" /><figcaption>React Native Loves Formik</figcaption></figure><p>In this blog, I will explain to you how to handle form in react native with formik and yup library.</p><p><strong>Formik</strong></p><p>Formik is an awesome library that makes handling form in react or react native much much easier. Formik helps you in handling 3 most annoying parts in forms:</p><ul><li>Getting values in and out of form state (Two way data binding)</li><li>Validation and error messages ( For this will be using Yup)</li><li>Handling form submission</li></ul><p><strong>Yup</strong></p><p>Yup is a JavaScript object schema validator and object parser. Yup provide powerful validation in object-oriented way.</p><p>Lot About theory Let see Form validation in action:</p><p>It will work on your new or old react native project:</p><p><strong>Step 1: Installing the package</strong></p><p>Add Formik and Yup package to your project by running following command</p><p>For Formik:</p><blockquote>npm install formik -S</blockquote><p>or</p><blockquote>yarn add formik</blockquote><p>For Yup</p><blockquote>npm install yup -S</blockquote><p>or</p><blockquote>yarn add yup</blockquote><p><strong>Step-2: Let&#39;s create the form</strong></p><pre>import React from &quot;react&quot;;</pre><pre>import { StyleSheet, Text, View, TextInput, Button } from &quot;react-native&quot;;</pre><pre>import { Formik } from &quot;formik&quot;;</pre><pre>import * as Yup from &quot;yup&quot;;</pre><pre>export default function App() {</pre><pre>return (</pre><pre>&lt;View style={styles.container}&gt;</pre><pre>&lt;View style={styles.inputStyle}&gt;</pre><pre>&lt;TextInput placeholder=&quot;Email&quot; /&gt;</pre><pre>&lt;/View&gt;</pre><pre>&lt;Text style={{ color: &quot;red&quot; }}&gt;&lt;/Text&gt;</pre><pre>&lt;View style={styles.inputStyle}&gt;</pre><pre>&lt;TextInput placeholder=&quot;Password&quot; /&gt;</pre><pre>&lt;/View&gt;</pre><pre>&lt;Text style={{ color: &quot;red&quot; }}&gt;&lt;/Text&gt;</pre><pre>&lt;View style={styles.inputStyle}&gt;</pre><pre>&lt;TextInput placeholder=&quot;Confrim Password&quot; /&gt;</pre><pre>&lt;/View&gt;</pre><pre>&lt;Text style={{ color: &quot;red&quot; }}&gt;&lt;/Text&gt;</pre><pre>&lt;Button title=&quot;Submit&quot; /&gt;</pre><pre>&lt;/View&gt;</pre><pre>);</pre><pre>}</pre><pre>const styles = StyleSheet.create({</pre><pre>container: {</pre><pre>flex: 1,</pre><pre>backgroundColor: &quot;#fff&quot;,</pre><pre>alignItems: &quot;center&quot;,</pre><pre>justifyContent: &quot;center&quot;</pre><pre>},</pre><pre>inputStyle: {</pre><pre>padding: 10,</pre><pre>width: &quot;60%&quot;,</pre><pre>borderWidth: 1,</pre><pre>borderColor: &quot;black&quot;</pre><pre>}</pre><pre>});</pre><p>The above is the simple form which contains email, password, confirm password, it will look like below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/216/1*6QW1YjW1DJjjZD6uZU9xbQ.png" /><figcaption>Simple form without formik</figcaption></figure><p><strong>Step-3 : let&#39;s add Formik to the form</strong></p><p><strong>Let first add two-way data binding using formik</strong></p><pre>import React from &quot;react&quot;;</pre><pre>import { StyleSheet, Text, View, TextInput, Button } from &quot;react-native&quot;;</pre><pre>import { Formik } from &quot;formik&quot;;</pre><pre>import * as Yup from &quot;yup&quot;;</pre><pre>export default function App() {</pre><pre>return (</pre><pre>&lt;Formik initialValues={{ email: &quot;&quot;, password: &quot;&quot;, confirmPassword: &quot;&quot; }}&gt;</pre><pre>{props =&gt; {</pre><pre>return (</pre><pre>&lt;View style={styles.container}&gt;</pre><pre>&lt;View style={styles.inputStyle}&gt;</pre><pre>&lt;TextInput</pre><pre>placeholder=&quot;Email&quot;</pre><pre>value={props.values.email}</pre><pre>onChangeText={props.handleChange(&quot;email&quot;)}</pre><pre>/&gt;</pre><pre>&lt;/View&gt;</pre><pre>&lt;Text style={{ color: &quot;red&quot; }}&gt;&lt;/Text&gt;</pre><pre>&lt;View style={styles.inputStyle}&gt;</pre><pre>&lt;TextInput</pre><pre>placeholder=&quot;Password&quot;</pre><pre>value={props.values.password}</pre><pre>onChangeText={props.handleChange(&quot;password&quot;)}</pre><pre>/&gt;</pre><pre>&lt;/View&gt;</pre><pre>&lt;Text style={{ color: &quot;red&quot; }}&gt;&lt;/Text&gt;</pre><pre>&lt;View style={styles.inputStyle}&gt;</pre><pre>&lt;TextInput placeholder=&quot;Confrim Password&quot;</pre><pre>value={props.values.confirmPassword}</pre><pre>onChangeText={props.handleChange(&quot;confirmPassword&quot;)}</pre><pre>/&gt;</pre><pre>&lt;/View&gt;</pre><pre>&lt;Text style={{ color: &quot;red&quot; }}&gt;&lt;/Text&gt;</pre><pre>&lt;Button title=&quot;Submit&quot; /&gt;</pre><pre>&lt;/View&gt;</pre><pre>);</pre><pre>}}</pre><pre>&lt;/Formik&gt;</pre><pre>);</pre><pre>}</pre><pre>const styles = StyleSheet.create({</pre><pre>container: {</pre><pre>flex: 1,</pre><pre>backgroundColor: &quot;#fff&quot;,</pre><pre>alignItems: &quot;center&quot;,</pre><pre>justifyContent: &quot;center&quot;,</pre><pre>backgroundColor: &quot;green&quot;</pre><pre>},</pre><pre>inputStyle: {</pre><pre>padding: 10,</pre><pre>width: &quot;60%&quot;,</pre><pre>borderWidth: 1,</pre><pre>borderColor: &quot;black&quot;</pre><pre>}</pre><pre>});</pre><p>There are a lot of ways to add formik to our form, but I like wrapping our form inside &lt;Formik&gt;&lt;/Fomik&gt; component which makes it easier to understand.</p><p>Let understand the initialValues attribute and props we used to achieve two way databinding</p><pre>&lt;Formik initialValues={{ email: &quot;&quot;, password: &quot;&quot;, confirmPassword: &quot;&quot; }}&gt;</pre><p>initialValues set the initial values for email, password, confirm password fields but how these values are assigned to the respective input field, for this props comes into play let understand</p><pre>&lt;TextInput</pre><pre>placeholder=&quot;Email&quot;</pre><pre>value={props.values.email}</pre><pre>onChangeText={props.handleChange(&quot;email&quot;)}</pre><pre>/&gt;</pre><p>The values of the intialValues attributes are accessed by props.values and are updated by props.handleChange. So the value attribute of the TextInput component is bind to props.values.email, and to update email you have to bind to onChangeText event to props.handleChange method.</p><p><strong>Step-4: Adding Validation using Yup</strong></p><p>Let&#39;s Add Validation to our form using yup package</p><pre>import React from &quot;react&quot;;</pre><pre>import {</pre><pre>StyleSheet,</pre><pre>Text,</pre><pre>View,</pre><pre>TextInput,</pre><pre>Button,</pre><pre>KeyboardAvoidingView</pre><pre>} from &quot;react-native&quot;;</pre><pre>import { Formik } from &quot;formik&quot;;</pre><pre>import * as Yup from &quot;yup&quot;;</pre><pre>const FormValidationSchema = Yup.object().shape({</pre><pre>email: Yup.string()</pre><pre>.email(&quot;Invalid Email.&quot;)</pre><pre>.required(&quot;Required&quot;),</pre><pre>password: Yup.string()</pre><pre>.min(6, &quot;Too Short!&quot;)</pre><pre>.required(&quot;Required&quot;),</pre><pre>confirmPassword: Yup.string()</pre><pre>.required(&quot;Required&quot;)</pre><pre>.test(</pre><pre>&quot;confirm-password-test&quot;,</pre><pre>&quot;Password and confirm password should match&quot;,</pre><pre>function(value) {</pre><pre>return value === this.parent.password;</pre><pre>}</pre><pre>)</pre><pre>});</pre><pre>export default function App() {</pre><pre>return (</pre><pre>&lt;KeyboardAvoidingView style={{flex:1}} behavior=&#39;padding&#39;&gt;</pre><pre>&lt;Formik</pre><pre>initialValues={{ email: &quot;&quot;, password: &quot;&quot;, confirmPassword: &quot;&quot; }}</pre><pre>validationSchema={FormValidationSchema}</pre><pre>&gt;</pre><pre>{props =&gt; {</pre><pre>return (</pre><pre>&lt;View style={styles.container}&gt;</pre><pre>&lt;View style={styles.inputStyle}&gt;</pre><pre>&lt;TextInput</pre><pre>placeholder=&quot;Email&quot;</pre><pre>value={props.values.email}</pre><pre>onChangeText={props.handleChange(&quot;email&quot;)}</pre><pre>onBlur={props.handleBlur(&quot;email&quot;)}</pre><pre>/&gt;</pre><pre>&lt;/View&gt;</pre><pre>&lt;Text style={{ color: &quot;red&quot; }}&gt;</pre><pre>{props.touched.email &amp;&amp; props.errors.email}</pre><pre>&lt;/Text&gt;</pre><pre>&lt;View style={styles.inputStyle}&gt;</pre><pre>&lt;TextInput</pre><pre>placeholder=&quot;Password&quot;</pre><pre>value={props.values.password}</pre><pre>onChangeText={props.handleChange(&quot;password&quot;)}</pre><pre>onBlur={props.handleBlur(&quot;password&quot;)}</pre><pre>/&gt;</pre><pre>&lt;/View&gt;</pre><pre>&lt;Text style={{ color: &quot;red&quot; }}&gt;</pre><pre>{props.touched.password &amp;&amp; props.errors.password}</pre><pre>&lt;/Text&gt;</pre><pre>&lt;View style={styles.inputStyle}&gt;</pre><pre>&lt;TextInput</pre><pre>placeholder=&quot;Confrim Password&quot;</pre><pre>value={props.values.confirmPassword}</pre><pre>onChangeText={props.handleChange(&quot;confirmPassword&quot;)}</pre><pre>onBlur={props.handleBlur(&quot;confirmPassword&quot;)}</pre><pre>/&gt;</pre><pre>&lt;/View&gt;</pre><pre>&lt;Text style={{ color: &quot;red&quot; }}&gt;</pre><pre>{props.touched.confirmPassword &amp;&amp; props.errors.confirmPassword}</pre><pre>&lt;/Text&gt;</pre><pre>&lt;Button  title=&quot;Submit&quot; /&gt;</pre><pre>&lt;/View&gt;</pre><pre>);</pre><pre>}}</pre><pre>&lt;/Formik&gt;</pre><pre>&lt;/KeyboardAvoidingView&gt;</pre><pre>);</pre><pre>}</pre><pre>const styles = StyleSheet.create({</pre><pre>container: {</pre><pre>flex: 1,</pre><pre>backgroundColor: &quot;#fff&quot;,</pre><pre>alignItems: &quot;center&quot;,</pre><pre>justifyContent: &quot;center&quot;,</pre><pre>backgroundColor: &quot;green&quot;</pre><pre>},</pre><pre>inputStyle: {</pre><pre>padding: 10,</pre><pre>width: &quot;60%&quot;,</pre><pre>borderWidth: 1,</pre><pre>borderColor: &quot;black&quot;</pre><pre>}</pre><pre>});</pre><p>First, let&#39;s see the validation form output</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/216/1*gXszvokkUer06j7fqy0Bfw.png" /><figcaption>Form with formik and validation</figcaption></figure><p>Let&#39;s understand the validation</p><pre>const FormValidationSchema = Yup.object().shape({</pre><pre>email: Yup.string()</pre><pre>.email(&quot;Invalid Email.&quot;)</pre><pre>.required(&quot;Email is Required&quot;),</pre><pre>password: Yup.string()</pre><pre>.min(6, &quot;Too Short!&quot;)</pre><pre>.required(&quot;Password is Required&quot;),</pre><pre>confirmPassword: Yup.string()</pre><pre>.required(&quot;Confirm Password is Required&quot;)</pre><pre>.test(</pre><pre>&quot;confirm-password-test&quot;,</pre><pre>&quot;Password and confirm password should match&quot;,</pre><pre>function(value) {</pre><pre>return value === this.parent.password;</pre><pre>}</pre><pre>)</pre><pre>});</pre><p>Here for email, I have put the validation as required and it should be of type email. One nice thing to notice is that you provide your validation message in validation rule as I have done .required(‘Email is Required’). For password, I have set a minimum 6 characters and required. For confirm password I have set that it should match with the password field and is required.</p><p>Two things you should notice. First is the key I used for validation like email, password,confirmPassword should match intialValues attribute value. Second, I have placed Yup validation schema (FormValidationSchema) outside the props component it is not necessary but if you are not using props for validation schema it is a good choice to place it out the component as it is not recreated every time component rerender.</p><pre>&lt;Formik</pre><pre>initialValues={{ email: &quot;&quot;, password: &quot;&quot;, confirmPassword: &quot;&quot; }}</pre><pre>validationSchema={FormValidationSchema}</pre><pre>&gt;</pre><p>To bind Yup validation schema with formik use validationSchema attribute and value would be yup validation schema object.</p><pre>&lt;TextInput</pre><pre>placeholder=&quot;Email&quot;</pre><pre>value={props.values.email}</pre><pre>onChangeText={props.handleChange(&quot;email&quot;)}</pre><pre>onBlur={props.handleBlur(&quot;email&quot;)}</pre><pre>/&gt;<br>&lt;Text style={{ color: &quot;red&quot; }}&gt;</pre><pre>{props.touched.email &amp;&amp; props.errors.email}</pre><pre>&lt;/Text&gt;</pre><p>In the input element, I have added onBlur element which triggers props.handeBlur method. This is useful to decide when to show the required validation message because by default form is empty so at that point, we do not want to show the required validation message, it should only be shown when the user selected the input element but does not fill it.</p><p>props.touched is an object which provide information about whether the user touched the input element or not. props.errors is an object which contains validation messages for the different fields we have in the form for example props.errors.email will contain validation message for the email field.</p><p><strong>Step-5: Submitting the form and handing Server Side Error</strong></p><p>After successful client-side validation using Yup and formik, we will submit the form detail to the server and handle any server-side error generated like duplicate email validation error etc. I will not go into sever side logic as this is out of scope for this article.</p><pre>import React from &quot;react&quot;;</pre><pre>import {</pre><pre>StyleSheet,</pre><pre>Text,</pre><pre>View,</pre><pre>TextInput,</pre><pre>Button,</pre><pre>KeyboardAvoidingView,</pre><pre>ActivityIndicator,</pre><pre>Alert</pre><pre>} from &quot;react-native&quot;;</pre><pre>import { Formik } from &quot;formik&quot;;</pre><pre>import * as Yup from &quot;yup&quot;;</pre><pre>const FormValidationSchema = Yup.object().shape({</pre><pre>email: Yup.string()</pre><pre>.email(&quot;Invalid Email.&quot;)</pre><pre>.required(&quot;Required&quot;),</pre><pre>password: Yup.string()</pre><pre>.min(6, &quot;Too Short!&quot;)</pre><pre>.required(&quot;Required&quot;),</pre><pre>confirmPassword: Yup.string()</pre><pre>.required(&quot;Required&quot;)</pre><pre>.test(</pre><pre>&quot;confirm-password-test&quot;,</pre><pre>&quot;Password and confirm password should match&quot;,</pre><pre>function(value) {</pre><pre>return value === this.parent.password;</pre><pre>}</pre><pre>)</pre><pre>});</pre><pre>const signUp = ({ email,password }) =&gt;</pre><pre>new Promise((resolve, reject) =&gt; {</pre><pre>setTimeout(() =&gt; {</pre><pre>if (email === &#39;test@test.com&#39;) {</pre><pre>reject(new Error(&quot;Duplicate email,please try again.&quot;));</pre><pre>}</pre><pre>resolve(true);</pre><pre>}, 5000);</pre><pre>});</pre><pre>export default function App() {</pre><pre>return (</pre><pre>&lt;KeyboardAvoidingView style={{flex:1}} behavior=&#39;padding&#39;&gt;</pre><pre>&lt;Formik</pre><pre>initialValues={{ email: &quot;&quot;, password: &quot;&quot;, confirmPassword: &quot;&quot; }}</pre><pre>validationSchema={FormValidationSchema}</pre><pre>onSubmit={(values,actions)=&gt;signUp({ email: values.email,password:values.password })</pre><pre>.then(() =&gt; {</pre><pre>Alert.alert(&#39;User Registered Succesfully.&#39;);</pre><pre>})</pre><pre>.catch(error =&gt; {</pre><pre>actions.setFieldError(&#39;general&#39;, error.message);</pre><pre>})</pre><pre>.finally(() =&gt; {</pre><pre>actions.setSubmitting(false);</pre><pre>})}</pre><pre>&gt;</pre><pre>{props =&gt; {</pre><pre>return (</pre><pre>&lt;View style={styles.container}&gt;</pre><pre>&lt;View style={styles.inputStyle}&gt;</pre><pre>&lt;TextInput</pre><pre>placeholder=&quot;Email&quot;</pre><pre>value={props.values.email}</pre><pre>onChangeText={props.handleChange(&quot;email&quot;)}</pre><pre>onBlur={props.handleBlur(&quot;email&quot;)}</pre><pre>/&gt;</pre><pre>&lt;/View&gt;</pre><pre>&lt;Text style={{ color: &quot;red&quot; }}&gt;</pre><pre>{props.touched.email &amp;&amp; props.errors.email}</pre><pre>&lt;/Text&gt;</pre><pre>&lt;View style={styles.inputStyle}&gt;</pre><pre>&lt;TextInput</pre><pre>placeholder=&quot;Password&quot;</pre><pre>value={props.values.password}</pre><pre>onChangeText={props.handleChange(&quot;password&quot;)}</pre><pre>onBlur={props.handleBlur(&quot;password&quot;)}</pre><pre>/&gt;</pre><pre>&lt;/View&gt;</pre><pre>&lt;Text style={{ color: &quot;red&quot; }}&gt;</pre><pre>{props.touched.password &amp;&amp; props.errors.password}</pre><pre>&lt;/Text&gt;</pre><pre>&lt;View style={styles.inputStyle}&gt;</pre><pre>&lt;TextInput</pre><pre>placeholder=&quot;Confrim Password&quot;</pre><pre>value={props.values.confirmPassword}</pre><pre>onChangeText={props.handleChange(&quot;confirmPassword&quot;)}</pre><pre>onBlur={props.handleBlur(&quot;confirmPassword&quot;)}</pre><pre>/&gt;</pre><pre>&lt;/View&gt;</pre><pre>&lt;Text style={{ color: &quot;red&quot; }}&gt;</pre><pre>{props.touched.confirmPassword &amp;&amp; props.errors.confirmPassword}</pre><pre>&lt;/Text&gt;</pre><pre>{props.isSubmitting?&lt;ActivityIndicator/&gt;:&lt;Button onPress={props.handleSubmit}  title=&quot;Submit&quot; /&gt;}</pre><pre>{&lt;Text style={{ color: &#39;red&#39; }}&gt;{props.errors.general}&lt;/Text&gt;}</pre><pre>&lt;/View&gt;</pre><pre>);</pre><pre>}}</pre><pre>&lt;/Formik&gt;</pre><pre>&lt;/KeyboardAvoidingView&gt;</pre><pre>);</pre><pre>}</pre><pre>const styles = StyleSheet.create({</pre><pre>container: {</pre><pre>flex: 1,</pre><pre>backgroundColor: &quot;#fff&quot;,</pre><pre>alignItems: &quot;center&quot;,</pre><pre>justifyContent: &quot;center&quot;,</pre><pre>backgroundColor: &quot;green&quot;</pre><pre>},</pre><pre>inputStyle: {</pre><pre>padding: 10,</pre><pre>width: &quot;60%&quot;,</pre><pre>borderWidth: 1,</pre><pre>borderColor: &quot;black&quot;</pre><pre>}</pre><pre>});</pre><p>First, let&#39;s see the code in action and then we will understand it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/216/1*PkqrP9OS8sFMlVipKS5nbw.png" /><figcaption>Submitting the form. When the form is being submitted the submit button is replaced by ActivityIndicator</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/216/1*KNpaOaZzKdG33cDhvIxYAA.png" /><figcaption>Rendering Server Side Error</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/216/1*ZDy_XkzMxenYreySwyKOnA.png" /><figcaption>Success Response From Server</figcaption></figure><p>Let&#39;s Understand The final step that is form submission</p><pre>{props.isSubmitting?&lt;ActivityIndicator/&gt;:&lt;Button onPress={props.handleSubmit}  title=&quot;Submit&quot; /&gt;}<br>{&lt;Text style={{ color: &#39;red&#39; }}&gt;{props.errors.general}&lt;/Text&gt;}</pre><p>Form submission is handled by props.handleSubmit method. One interesting prop is props.isSubmitting which use full to handle whether the form is being submitted or not. Here i have use that prop to handle submit button. If isSubmitting is false then i will show the submit button else i will show a spinner which indicate the user that form submission is in progress.</p><p>props.errors.general is use to show server side error message. More on this is in below section.</p><pre>const signUp = ({ email,password }) =&gt;</pre><pre>new Promise((resolve, reject) =&gt; {</pre><pre>setTimeout(() =&gt; {</pre><pre>if (email === &#39;test@test.com&#39;) {</pre><pre>reject(new Error(&quot;Duplicate email,please try again.&quot;));</pre><pre>}</pre><pre>resolve(true);</pre><pre>}, 5000);</pre><pre>});</pre><p>The above is the dummy function I created for mocking server process because our main goal is to understand form handling in react native so I don&#39;t want to make it complex by also including server-side code or logic.</p><pre>&lt;Formik</pre><pre>initialValues={{ email: &quot;&quot;, password: &quot;&quot;, confirmPassword: &quot;&quot; }}</pre><pre>validationSchema={FormValidationSchema}</pre><pre>onSubmit={(values,actions)=&gt;signUp({ email: values.email,password:values.password })</pre><pre>.then(() =&gt; {</pre><pre>Alert.alert(&#39;User Registered Succesfully.&#39;);</pre><pre>})</pre><pre>.catch(error =&gt; {</pre><pre>actions.setFieldError(&#39;general&#39;, error.message);</pre><pre>})</pre><pre>.finally(() =&gt; {</pre><pre>actions.setSubmitting(false);</pre><pre>})}</pre><pre>&gt;</pre><p>In Fomik component I have added onSubmit method which is only called after all the client-side validation is successful. Here we make api request and handle server response. onSubmit method provide two argument <strong>values </strong>and <strong>actions</strong>. <strong>values </strong>argument contain all form fields values (value of email, password, confirmPassword) and <strong>actions </strong>argument is an object which helps to change the state of the form for example when the form submission is completed, we can change the state of the form using actions.setSubmitting(false) method.</p><p>Also, we can handle error response from the server by using actions.setFieldError(‘general’, error.message) method here ‘general’ key name is up to you. I use this key to show server-side error.</p><blockquote>Note: I have not used secureTextEntry={true} for password and confirm password for making you understand clearly what i am typing in screenshot which is necessary for understanding of validation. I higly recommend to use secureTextEntry={true} for password and other sensitive text input</blockquote><p>The Above article was an intro to form handling with react native using Formik and Yup. I just want to make clear the entire workflow of form submission like two-way data binding, validation and form submission, there is much more on this. Below Links help you in that matter.</p><p><strong>Useful links:</strong></p><blockquote>Source Code for this demo can be found at github: <a href="https://github.com/bhaveshdaswani93/formik-with-react-native">https://github.com/bhaveshdaswani93/formik-with-react-native</a></blockquote><p>Formik Github: <a href="https://github.com/jaredpalmer/formik">https://github.com/jaredpalmer/formik</a></p><p>Formik Docs: <a href="https://jaredpalmer.com/formik/docs/overview">https://jaredpalmer.com/formik/docs/overview</a></p><p>Formik Tutorial (That I, where i learned from): <a href="https://www.reactnativeschool.com/build-and-validate-forms-with-formik-and-yup/">https://www.reactnativeschool.com/build-and-validate-forms-with-formik-and-yup/</a></p><p>Yup github: <a href="https://github.com/jquense/yup">https://github.com/jquense/yup</a></p><blockquote>Follow me on <a href="https://github.com/bhaveshdaswani93">Github </a>and <a href="https://twitter.com/Bhavesh_Dev93">Twitter</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d452ef06fe0f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Native Expo boilerplate]]></title>
            <link>https://medium.com/@digitaldaswani/react-native-expo-boilerplate-3bf42b2eaa50?source=rss-fb8784950c62------2</link>
            <guid isPermaLink="false">https://medium.com/p/3bf42b2eaa50</guid>
            <category><![CDATA[boilerplate]]></category>
            <category><![CDATA[expo]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Bhavesh Daswani]]></dc:creator>
            <pubDate>Sun, 03 Nov 2019 08:31:41 GMT</pubDate>
            <atom:updated>2019-11-03T08:31:41.745Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*pQ6uPYRyt1NZzrTI.png" /><figcaption>React Native Expo Boilerplate</figcaption></figure><p>Hello, My self Bhavesh Daswani I am a full stack developer having 3 years of experience and I am working on tech stack like React Native, React, Laravel, Nodejs.</p><p>Today I will talk about React native Expo Boilerplate.</p><p><strong>What is Boilerplate.</strong></p><p>Boilerplate means the initial setup you need to start your project. let me give you an example to better understand it. we create a new project from expo init project_name. It gives us a folder structure with a bare minimum setup with packages like react,react-native and expo but for our project to kick start, we need extra libraries like react-navigation, react-native-safe-area, etc. These types of libraries are required for each project. Then another thing is required is project structure, In each new project we have to setup a folder structure like each new screen will go in screen folder, reusable component will go in the component folder and much more. These setups are required for each new project, so generally, these requirements are termed as boilerplate.</p><p><strong>Why we need boilerplate</strong></p><p>For now, we have understand what is boilerplate. Let&#39;s understand its importance. Boilerplate give you a pre define architecture, so you don&#39;t have to reinvent the wheel again and again for each new project. It make developer life easy. Everyone has heard about create-react-app what it does, it provide a boiler plate for new react app, it setup all for us like babel, webpack everything, we as a developer just need to worry about the project and we are free from initial setup burden.</p><p><strong>React Native Expo boilerplate</strong></p><p>Let me introduce to mine react native expo boilerplate which I use in each new react-native expo project.</p><p>This includes common use to packages like:</p><ul><li>react-navigation</li><li>react-navigation-stack</li><li>react-navigation-drawer</li><li>react-native-gesture-handler</li><li>react-native-safe-area-view</li><li>react-native-safe-area-context</li><li>react-native-reanimated</li></ul><p>The above are the additional libraries I added along provided by expo init</p><p>It contains folder structure as below</p><ul><li>Screen folder: It will contain files related to screen like LoginScreen.js,Homescreen.js etc</li><li>Component Folder: where I put the reusable component. The component that is being used in multiple screens.</li><li>UI folder: It belongs inside the component folder, it contains UI(User Interface) components like font-related component. I put my font, button level component in this folder</li><li>Constants folder: It will contain constant files like colors.js (this file contain app main color, secondary color etc.) etc. Those things which are constants are put in this folder</li><li>assets/fonts folder: It contains app fonts. The .ttf,.otf like fonts are placed in this folder</li><li>assets/images: it contains static images that will be used inside app. One point to note the image used as static images should be 1x, 2x,3x format, react-native automatically handle it for both Android and ios. We do not have to worry xdpi, mdpi,hdpi like things react native will manage for us</li></ul><p>This boilerplate contain two other main things the RootComponent, Custom Navigation header</p><ul><li>RootComponent: This component would be root component of each screen. This component provide safe area view to screen in both android and ios</li><li>Custom Navigation header: Navigation header provided by react-navigation library is cool but customizing it is difficult for me, so I made my custom header which is compatible with both ios and android.</li></ul><p><strong>How to use React Native Boiler Plate</strong></p><p>This boilerplate targets lates expo sdk version that is expo sdk 35.</p><p>When you start new project you do not need to do expo init just clone my repo from GitHub and then do npm install / yarn install and your boilerplate is ready, enjoy coding react native expo project. No hassle for initial setup.</p><p>Github Link: <a href="https://github.com/bhaveshdaswani93/react-native-expo-boilerplate">https://github.com/bhaveshdaswani93/react-native-expo-boilerplate</a></p><blockquote>Follow me on <a href="https://github.com/bhaveshdaswani93"><strong>Github</strong></a><strong> </strong>&amp; <a href="https://twitter.com/Bhavesh_Dev93"><strong>Twitter</strong></a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3bf42b2eaa50" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Web Scraping with Laravel]]></title>
            <link>https://medium.com/@digitaldaswani/web-scraping-with-laravel-ab5f1c5f00a5?source=rss-fb8784950c62------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab5f1c5f00a5</guid>
            <category><![CDATA[web-scraping]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Bhavesh Daswani]]></dc:creator>
            <pubDate>Sun, 27 Oct 2019 13:52:33 GMT</pubDate>
            <atom:updated>2019-10-27T13:52:33.821Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*A7q6F4NQDEdReU1u.png" /><figcaption>Web Scraping with Laravel</figcaption></figure><p>What is Web Scraping?</p><p>Web scraping is a process where the data is present in the Html format and we filter out the required data and persist it.</p><p>The above is a general definition. The more precise one that I think is that the data is present over the website, we have to target what exact information we need to extract from the Html page, which is called dataset. Once we have identified the dataset then over logic comes into play. We know that the Html page contains a root HTML tag, HEAD tag, BODY tag, over dataset is present inside the BODY tag. we have other Html tags like p, div and other attributes like id, class. Over web scraping logic depends on these elements. For example, suppose we have below like Html page for simplicity I will only include directly body tag</p><p>&lt;body&gt;</p><p>&lt;p&gt; Hello world &lt;/p&gt;</p><p>&lt;/body&gt;</p><p>In the above example, my dataset is “Hello world” and I want to scrap it out from the Html page. so my logic would be to get the text inside the p tag.</p><p>Now we understand what is web scraping, how we will filter the data. Now the question is how can we do web scraping in laravel for that we will use a package called <a href="https://github.com/dweidner/laravel-goutte">laravel-goutte</a>. This repository implements a simple <a href="https://laravel.com/docs/master/providers">ServiceProvider</a> that makes a singleton instance of the Goutte client easily accessible via a <a href="https://laravel.com/docs/master/facades">Facade</a> in <a href="http://laravel.com/">Laravel</a>.</p><p>Let me introduce you to Goutte (Pronounced goot). Goutte is a library that helps us to implement our web scraping logic. It is a PHP Web Scraper. It is a thin wrapper around Symfony Components like <a href="https://symfony.com/components/BrowserKit">BrowserKit</a>, <a href="https://symfony.com/doc/current/components/css_selector.html">CssSelector</a>, and <a href="https://symfony.com/doc/current/components/dom_crawler.html">DomCrawler</a>. For Http request this library uses <a href="http://docs.guzzlephp.org/">Guzzle</a> HTTP Component.</p><p>Now we know the tool that will help us with web scraping. Now the question is how to integrate the <a href="https://github.com/dweidner/laravel-goutte">laravel-goutte</a> package with laravel.</p><p>First, install the package in your existing or new Laravel package using the following command</p><pre>composer require weidner/goutte</pre><p>Second Register it in config/app.php and provide an alias for its Facade</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KRjAgVvG_hz7rQxwcUnz7A.png" /><figcaption>Contain Registration of the service provider and providing an alias to the facade</figcaption></figure><p>For now, we have registered laravel-goutte package. Now let’s use it to scrap it</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3IqRhT6DMzkfALMs09JL0Q.png" /><figcaption>How to use laravel-goutte package for web scraping.</figcaption></figure><p>I have Created a controller called GoutteController( Controller name is up to you ), Created goutte client, guzzle client with timeout 60 ( guzzle config is depended on you. All guzzle configurations are supported). $goutteClient-&gt;request method calls the desired html page and returns DomCrawler Component where you can apply any filtering logic you have decided to get your dataset.</p><p>I have made a simple logic. My script will dump the header name of the search result. This is just a simple demo and it is much more powerful. Below is the image of the page which I will scrap for</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7tW-R5oz4IsHACCbNVt9zg.png" /></figure><p>The Below is the output of the script</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qGvbOT6JkFPCclP05ydwCw.png" /></figure><p><strong>Below I will provide links where you can go and check in-depth detail related to the package and library</strong></p><p><strong>The above example source code: </strong><a href="https://github.com/bhaveshdaswani93/Web-Scraping-with-laravel"><strong>https://github.com/bhaveshdaswani93/Web-Scraping-with-laravel</strong></a></p><p><strong>laravel-goutte: </strong><a href="https://github.com/dweidner/laravel-goutte"><strong>https://github.com/dweidner/laravel-goutte</strong></a></p><p><strong>Goutte: </strong><a href="https://github.com/FriendsOfPHP/Goutte"><strong>https://github.com/FriendsOfPHP/Goutte</strong></a></p><p><strong>Symfony DomCrawler Component </strong><a href="https://symfony.com/doc/current/components/dom_crawler.html#component-dom-crawler-dumping"><strong>: https://symfony.com/doc/current/components/dom_crawler.html#component-dom-crawler-dumping</strong></a></p><p><strong>TL;DR</strong></p><p><strong>This article is all about what is web scraping, which package you should use to integrate your logic of web scraping in laravel.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab5f1c5f00a5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>