<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Daryl Aranha</title>
    <description>The latest articles on DEV Community by Daryl Aranha (@darylaranha).</description>
    <link>https://dev.to/darylaranha</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F666143%2Fc6a3ecbf-2f08-4240-a247-1dc8b867db8e.jpeg</url>
      <title>DEV Community: Daryl Aranha</title>
      <link>https://dev.to/darylaranha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/darylaranha"/>
    <language>en</language>
    <item>
      <title>Import Node packages in React and React Native.</title>
      <dc:creator>Daryl Aranha</dc:creator>
      <pubDate>Wed, 23 Mar 2022 12:46:25 +0000</pubDate>
      <link>https://dev.to/darylaranha/import-node-packages-in-react-and-react-native-2nmm</link>
      <guid>https://dev.to/darylaranha/import-node-packages-in-react-and-react-native-2nmm</guid>
      <description>&lt;p&gt;Do you wish to use packages that are typically used on the backend in your frontend app? Well, you can do it with the help of Browserify.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Browserify?
&lt;/h2&gt;

&lt;p&gt;It’s a JavaScript bundler that allows us to use node modules to be compiled to use in the browser. It can also be used to keep track of your own and third-party code.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I use it?
&lt;/h2&gt;

&lt;p&gt;In a nutshell, you pass browserify your custom code or an external library, and it takes care of the rest.&lt;/p&gt;

&lt;p&gt;Now, for instance, there is a need for a third-party package xml-js in your web app, then you need to follow these sets.&lt;/p&gt;

&lt;p&gt;Create a file, say xmlConverter.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;convert&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;xml&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;convert&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, pass this file to browserify with -o option. This -o is used to tell which file will contain the complied code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;browserify xmlConverter.js &lt;span class="nt"&gt;-o&lt;/span&gt; xmlComplied.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This file can now be utilized in your web application. This, however, will not function with native programs. &lt;a href="https://hackernoon.com/using-core-node-js-modules-in-react-native-apps-64acd4d07140"&gt;Aakash N S&lt;/a&gt; gives detailed instructions on how to do so, or you can continue reading if you don't want to delve into depth. I've built a script that will take care of everything for you, and it will work for both web and mobile apps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Docker Run:&lt;/strong&gt;   &lt;a href="https://hub.docker.com/r/darylaranha/node-to-app-compiler"&gt; (link) &lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; &amp;lt;project_directory&amp;gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;docker run &lt;span class="nt"&gt;-it&lt;/span&gt; &lt;span class="nt"&gt;--rm&lt;/span&gt; &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;PWD&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;:/app node-to-app-compiler:v0.0.1 &amp;lt;option&amp;gt; &amp;lt;package_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Example:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker run &lt;span class="nt"&gt;-it&lt;/span&gt; &lt;span class="nt"&gt;--rm&lt;/span&gt; &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;PWD&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;:/app node-to-app-compiler:v0.0.1 xml-js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Using Terminal:&lt;/strong&gt;   &lt;a href="https://github.com/DarylAranha/node-to-app-compiler"&gt; (link) &lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Clone this &lt;a href="https://github.com/DarylAranha/node-to-app-compiler"&gt;repo&lt;/a&gt; and run the shell script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;chmod&lt;/span&gt; +x compiler.sh
&lt;span class="nv"&gt;$ &lt;/span&gt;./complier.sh &amp;lt;options&amp;gt; &amp;lt;package_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Example:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;./complier.sh xml-js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Options:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;–local: This parameter tells the script whether you're looking for a node package that comes pre-installed. This script tries to install the package using npm by default.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://browserify.org/"&gt;Browserify&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/browserify/browserify-handbook"&gt;Browserify Handbook&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://hackernoon.com/using-core-node-js-modules-in-react-native-apps-64acd4d07140"&gt;Using Core Nodejs Modules In React Native Apps&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactnative.dev/"&gt;React Native&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/"&gt;ReactJs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>react</category>
      <category>docker</category>
    </item>
    <item>
      <title>Get Infinite Scrolling in just few lines of code!</title>
      <dc:creator>Daryl Aranha</dc:creator>
      <pubDate>Thu, 09 Dec 2021 15:43:50 +0000</pubDate>
      <link>https://dev.to/darylaranha/get-infinite-scrolling-in-just-few-lines-of-code-368l</link>
      <guid>https://dev.to/darylaranha/get-infinite-scrolling-in-just-few-lines-of-code-368l</guid>
      <description>&lt;p&gt;Do you want to remove bulky packages that perform Infinite scrolling or Lazy loading with just a few lines of code? Then I have something interesting to show you. Introducing &lt;strong&gt;Intersection Observer!!!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8bmpxj7vjhlalnwavjkb.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8bmpxj7vjhlalnwavjkb.gif" alt="Infinite Scroll Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Intersection Observer work?
&lt;/h2&gt;

&lt;p&gt;To put it in simple terms it works like magic 🎊 That's it! Let's move on...&lt;/p&gt;

&lt;p&gt;If you want to know more about it then fine!!! It's a web API that takes a callback. This callback is triggered when a target element intersects with either the viewport or a specified element &lt;strong&gt;&lt;em&gt;(Lets call it Root Element or Root)&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I use it?
&lt;/h2&gt;

&lt;p&gt;Create an IntersectionObserver object by passing the callback function and configuration object.&lt;/p&gt;

&lt;p&gt;The configuration &lt;em&gt;(also called options)&lt;/em&gt; take 3 values. &lt;strong&gt;root&lt;/strong&gt;, &lt;strong&gt;rootMargin&lt;/strong&gt; and &lt;strong&gt;threshold&lt;/strong&gt; and it looks something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Defining config
let config = {
    root: document.querySelector('#scrollArea'),
    rootMargin: '0px',
    threshold: 1.0
}

// What action needs to be taken
let callback = () =&amp;gt; {
    // Here you specify what needs to be done
    // More on this later
}

// Creating an observer object
let observer = new IntersectionObserver(callback, config)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before we move towards an example let me briefly explain what each value in config does.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Root:&lt;/strong&gt; It is used as a viewport to check the visibility of the target. By default it takes browser viewport if &lt;code&gt;null&lt;/code&gt; is passed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RootMargin:&lt;/strong&gt; It's the margin around the root.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Threshold:&lt;/strong&gt; It is called intersection ratio and has a range from 0.0 to 1.0 with 1.0 being 100% visible of the target within the root.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let's talk about the callback. The callback takes one parameter of type array. The reason behind it being an array is because you can add multiple target &lt;em&gt;(entities having same id name)&lt;/em&gt; to a single observer. We use the &lt;strong&gt;isIntersecting&lt;/strong&gt; property to decided whether the observable element is visible or not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;Enough explanation and let's move towards an example. You can check out the entire &lt;a href="https://github.com/DarylAranha/IntersectionOberserverExample" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...

const App = () =&amp;gt; {
    // Create an observer object
    const observer = useRef(null);
    observer.current = new IntersectionObserver(
    (entries) =&amp;gt; {
        // entries is of type array
        entries.forEach((entry) =&amp;gt; {
        // this will tell if the element is visible or not
        if (!entry.isIntersecting) {
            return;
        }
        // Do something
    })
    }, {
        root: document.querySelector("#App"),
        rootMargin: "0px",
        threshold: 0.3,
    });

    ...

    // ComponentDidMount, Make sure to observe the element after render
    useEffect(() =&amp;gt; {
        observer.current.observe(document.querySelector("#observe-this"));
    }, []);

    ...

   return (
        &amp;lt;div className="App"&amp;gt;

            ...

            {/* When this div is visible, fetch new data */}
            &amp;lt;div id="observe-this"&amp;gt;&amp;lt;/div&amp;gt;

            ...

        &amp;lt;/div&amp;gt;
    );
};

...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The example above must be self explanatory. If not then let me summarise.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create an observer object, if its a class based component then add the object creation snippet in the constructor.&lt;/li&gt;
&lt;li&gt;While creating an observer you need to pass a callback and an option object. This will tell the Observer the following:

&lt;ul&gt;
&lt;li&gt;What needs to be done when an callback is triggered.&lt;/li&gt;
&lt;li&gt;When should the callback be called.&lt;/li&gt;
&lt;li&gt;What is the visible are?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;When the component renders, point the element to be observed.&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Reference
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API" rel="noopener noreferrer"&gt;Learn more about Intersection Observer&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/" rel="noopener noreferrer"&gt;Getting started with ReactJs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.spacexdata.com/#5fc4c846-c373-43df-a10a-e9faf80a8b0a" rel="noopener noreferrer"&gt;SpaceX API&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>reactnative</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>PropTypes!!! Trust me you need it.</title>
      <dc:creator>Daryl Aranha</dc:creator>
      <pubDate>Fri, 15 Oct 2021 09:21:17 +0000</pubDate>
      <link>https://dev.to/darylaranha/proptypes-trust-me-you-need-it-2a1l</link>
      <guid>https://dev.to/darylaranha/proptypes-trust-me-you-need-it-2a1l</guid>
      <description>&lt;p&gt;As a developer working with React Components, I noticed developers usually don't think of adding validation to their components, which is fine for a simple application. As the app grows multiple developers start working towards a single goal; there are instances of the applications' stability to get compromised.&lt;/p&gt;

&lt;p&gt;One of the reasons for this to happen is using incorrect type or not sending the required values which may result in unexpected behaviour.&lt;/p&gt;

&lt;p&gt;Here PropTypes come into picture.&lt;/p&gt;

&lt;h2&gt;
  
  
  So... What is PropTypes?
&lt;/h2&gt;

&lt;p&gt;To put it in simple terms, it's an added layer of checks to a component’s prop object. In other words, it makes sure we send all the necessary data in the correct type as props.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I set it up?
&lt;/h2&gt;

&lt;p&gt;Setup is pretty straight forward. Here’s what you need to do. Open your favourite terminal and run the following commands.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd &amp;lt;project_directory&amp;gt;
npm install prop-types --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What’s next? An Example? Sure....
&lt;/h2&gt;

&lt;p&gt;Let me walk you through a simple example which will make things clear. Let's say there is a List component and it does what it says, displays a list. Of what? Hmmm… let's say a list of the first 5 Marvel movies released. (Sorry DC fans.)&lt;/p&gt;

&lt;p&gt;This list contains the names of movies and the year it was released.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const movieList = [
  { id: 1, name: "Iron Man", releaseYear: "2008" },
  { id: 2, name: "The Incredible Hulk", releaseYear: "2008" },
  { id: 3, name: "Iron Man 2", releaseYear: "2010" },
  { id: 4, name: "Thor", releaseYear: "2011" },
  { id: 5, name: "Captain America", releaseYear: "2011" }
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is a code snippet that contains an array of objects with the name and release year of the first 5 marvel movies and a functional component that displays those values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import PropTypes from "prop-types";

...

const movieList = [
  { id: 1, name: "Iron Man", releaseYear: "2008" },
  { id: 2, name: "The Incredible Hulk", releaseYear: "2008" },
  { id: 3, name: "Iron Man 2", releaseYear: "2010" },
  { id: 4, name: "Thor", releaseYear: "2011" },
  { id: 5, name: "Captain America", releaseYear: "2011" }
];

function Movie({ name, year = "Not specified" }) {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;
        &amp;lt;b&amp;gt;Name:&amp;lt;/b&amp;gt; {name}
      &amp;lt;/p&amp;gt;
      &amp;lt;p&amp;gt;
        &amp;lt;b&amp;gt;Year:&amp;lt;/b&amp;gt; {year}
      &amp;lt;/p&amp;gt;
      &amp;lt;hr /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

Movie.propTypes = {
  name: PropTypes.string.isRequired,
  year: PropTypes.string
};

...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have noticed I have removed all CSS. Why do you ask??? Because I am Batman.&lt;/p&gt;

&lt;p&gt;Now, It looks pretty similar to how you would traditionally write but with one small addition at the end. &lt;strong&gt;Movie.propTypes&lt;/strong&gt; allows us to add type checking and forces us to pass the required props.&lt;/p&gt;

&lt;p&gt;You can also add &lt;strong&gt;Movie.defaultProps&lt;/strong&gt; which will set the default value, but I didn’t add as JavaScript allows us to add a default value during object destructuring.&lt;/p&gt;

&lt;p&gt;Here is how the error would look in the console.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario 1:&lt;/strong&gt; When the required prop is not passed.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0ja8m1ue8qe92tibix5m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0ja8m1ue8qe92tibix5m.png" alt="missing required parameter"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario 2:&lt;/strong&gt; When the type of the prop do not match.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fum865vs1dif7orqfp9lx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fum865vs1dif7orqfp9lx.png" alt="prop type mismatch"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Don't worry, these errors wont show up in production. It’s meant to be caught during development.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What else can I do with it?
&lt;/h2&gt;

&lt;p&gt;I have shown you the basics on how to get started and with this we have covered the basics and a little bit of intermediate stuffs. There is more to this and you can go through the &lt;a href="https://github.com/facebook/prop-types" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>reactnative</category>
    </item>
  </channel>
</rss>
