<?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: Ridhik Govind</title>
    <description>The latest articles on DEV Community by Ridhik Govind (@ridhikgovind).</description>
    <link>https://dev.to/ridhikgovind</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%2F399884%2Fda2cf288-a8ad-442c-81d5-008fcb3dd964.jpeg</url>
      <title>DEV Community: Ridhik Govind</title>
      <link>https://dev.to/ridhikgovind</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ridhikgovind"/>
    <language>en</language>
    <item>
      <title>How to make React like components in HTML with 7 lines of JavaScript</title>
      <dc:creator>Ridhik Govind</dc:creator>
      <pubDate>Fri, 30 Jul 2021 13:10:19 +0000</pubDate>
      <link>https://dev.to/ridhikgovind/how-to-make-react-like-components-in-html-with-7-lines-of-javascript-2f0d</link>
      <guid>https://dev.to/ridhikgovind/how-to-make-react-like-components-in-html-with-7-lines-of-javascript-2f0d</guid>
      <description>&lt;p&gt;If we all know what one thing React is most famous for - that would be making Components right ? (I mean one among the multitude of amazing features). You make a component and you can use it anywhere you want throughout your codebase. &lt;em&gt;getting butterflies in my stomach right now&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Making a component in React is as easy as this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Welcome() {
  return &amp;lt;h1&amp;gt;Hello there !!&amp;lt;/h1&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we render this JavaScript function Welcome() - also a component into the HTML DOM which will display as "Hello there !!". As simple as that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const element = &amp;lt;Welcome/&amp;gt;;
ReactDOM.render(
  element,
  document.getElementById('root')
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now for a long time, I really thought only React could do this, UNTIL I found out that I could make components like these in HTML too. Only thing is we won't be having all the capabilities that React has - duh ! At first I thought it would be something that was done in the older HTML 1.0 or something where people used to manipulate HTML to get results but NO, this thing is legit.&lt;/p&gt;

&lt;h2&gt;
  
  
  So how does it really work ?
&lt;/h2&gt;

&lt;p&gt;Imagine you have 3 HTML pages. In these 3 pages you have a header and a footer. What you normally do is you create a sample template consisting of the header and footer in 1st page then when you are creating the 2nd page you just copy paste the entire HTML page and make some changes to the body. I don't know about you but that's how I usually did.   &lt;/p&gt;

&lt;p&gt;So for starters, let's create a header.&lt;br&gt;
We can solve this by creating a component for the header and then including it in every other page just by using HTML and a few lines of JavaScript - yeah you heard me right. Without further ado :  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; You create an HTML tag in your index.html page. e.g&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
&amp;lt;body&amp;gt;
    &amp;lt;header&amp;gt;&amp;lt;/header&amp;gt;
    Some sample body content
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Create a new "header.html" page. and insert some sample content e.g&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h1&amp;gt;Super important Header content&amp;lt;/h2&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Using the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API"&gt;fetch API&lt;/a&gt; you grab the HTML file (which is a resource here), then you take the response which is the "header.html" file, then you convert it into a text form.&lt;br&gt;&lt;br&gt;
Now the &lt;code&gt;.text()&lt;/code&gt; method is a string containing all the combined text of all the elements inside "header.html".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch("./header.html")
  .then(response =&amp;gt; {
    return response.text()
  })
  .then(data =&amp;gt; {
    document.querySelector("header").innerHTML = data;
 });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it ! Now if you look at your HTML page you can see the text being displayed. Now let me tell you some tricks.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Creating your own custom named components in HTML
&lt;/h2&gt;

&lt;p&gt;How we name components in React is usually like this e.g "Nav.js" or "Login.js". Do notice that the first letter is always in CAPS. We can do the same for our HTML pages too. How ?  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a folder named "components" in the root of the folder and create a new HTML page with names starting with CAPS like we mentioned above. So for e.g if we need to create a new component called "MobileNav". So we do:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;step 1: Adding the tag
&amp;lt;MobileNav&amp;gt;&amp;lt;MobileNav/&amp;gt;

step 2: Adding some content inside MobileNav.html

step 3: Adding the required JavaScript. In this case its something like this: 

fetch("./components/MobileNav.html")
  .then(response =&amp;gt; {
    return response.text()
  })
  .then(data =&amp;gt; {
    document.querySelector("MobileNav").innerHTML = data;
 });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The advantages of this is that we know which one is a component. But this is just another way to do things, but don't do these for tags like "header", "footer", "nav" etc as it might be bad for SEO purposes.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You could also do it this way by use of the "class" attribute so that we way we can keep all the SEO benefits intact.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. &amp;lt;nav class="MobileNav"&amp;gt;&amp;lt;/nav&amp;gt;

2. add some content

3. fetch("./components/MobileNav.html")
  .then(response =&amp;gt; {
    return response.text()
  })
  .then(data =&amp;gt; {
        //do notice the use of class name ".MobileNav" below
    document.querySelector(".MobileNav").innerHTML = data;
 });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;So I hope you learnt something new today. This method will save you loads of your time especially if you are working with more than 10 pages and there is a chance for the content to change all the time.   &lt;/p&gt;

&lt;p&gt;Here is the codesandbox containing all the code that we did today:&lt;br&gt;
&lt;a href="https://codesandbox.io/s/making-components-in-html-eebyx"&gt;https://codesandbox.io/s/making-components-in-html-eebyx&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;Reference: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Response/text"&gt;https://developer.mozilla.org/en-US/docs/Web/API/Response/text&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Some people reading the title of this article be like: &lt;br&gt;
&lt;a href="https://i.giphy.com/media/MZocLC5dJprPTcrm65/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/MZocLC5dJprPTcrm65/giphy.gif" alt="Its happening"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>todayilearned</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to create a simple testimonial carousel with Splide.js</title>
      <dc:creator>Ridhik Govind</dc:creator>
      <pubDate>Sun, 18 Jul 2021 10:41:29 +0000</pubDate>
      <link>https://dev.to/ridhikgovind/how-to-create-a-simple-testimonial-carousel-with-splide-js-5cac</link>
      <guid>https://dev.to/ridhikgovind/how-to-create-a-simple-testimonial-carousel-with-splide-js-5cac</guid>
      <description>&lt;h2&gt;
  
  
  Appetizer
&lt;/h2&gt;

&lt;p&gt;Today we will learn how to create a testimonial carousel with Splide&lt;/p&gt;

&lt;p&gt;Now why Splide and not others such as Swiperjs and Owl-carousel is that it's totally dependency free - hence less size. &lt;br&gt;
So let's get cracking.  &lt;/p&gt;
&lt;h2&gt;
  
  
  Main Course
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Get the latest Splide CDN &lt;/p&gt;

&lt;p&gt;Let's grab the CDN from JSDeliver and import the Splide package into our code. Paste the code right after where the &lt;code&gt;&amp;lt;/body&amp;gt;&lt;/code&gt; tag ends. Reason for placing it at the bottom like we are doing now is we don't slow down the initial render of HTML.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;script src="https://cdn.jsdelivr.net/npm/@splidejs/splide@2.4.21/dist/js/splide.min.js"&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt;  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: CDN link for styling  &lt;/p&gt;

&lt;p&gt;Now Splide comes with its own default as well as two thematic styling which are sea-green and sky-blue. If you are one of those people who loves custom styling things then just the default one which comes with basic styling such as arrows, pagination can be a great choice.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;link
rel="stylesheet"  
href="https://cdn.jsdelivr.net/npm/@splidejs/splide@2.4.21/dist/css/splide.min.css"
/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Fundamental structure &lt;br&gt;
    Learning the very basic structure is important if you are building a custom site for somebody or yourself and want to apply your own styling. Here go ahead and type that in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="splide"&amp;gt;
    &amp;lt;div class="splide__track"&amp;gt;
        &amp;lt;ul class="splide__list"&amp;gt;
            &amp;lt;li class="splide__slide"&amp;gt;&amp;lt;/li&amp;gt;
            &amp;lt;li class="splide__slide"&amp;gt;&amp;lt;/li&amp;gt;
            &amp;lt;li class="splide__slide"&amp;gt;&amp;lt;/li&amp;gt;
        &amp;lt;/ul&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;: Adding the slide content - which in our case is the testimonial content inside &lt;strong&gt;each&lt;/strong&gt; of the &lt;code&gt;&amp;lt;li class="splide__slide"&amp;gt;&amp;lt;/li&amp;gt;&lt;/code&gt; tags.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="splide__slide__container"&amp;gt;
    &amp;lt;div class="slide__content"&amp;gt;"I'm flabbergasted after using this product. No Regrets. Ever."
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside &lt;code&gt;splide__slide__container&lt;/code&gt; is where we do all the adjustments such are setting the &lt;code&gt;max-width&lt;/code&gt; for the content.&lt;br&gt;&lt;br&gt;
Tip:  Having &lt;code&gt;splide__slide__container&lt;/code&gt; is also pretty useful if you are having an image and you want to set a specific height for your image and go full blown cover mode but more on that later.&lt;/p&gt;

&lt;p&gt;Now let us turn this plain text into something pretty.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;: Styling your slide&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;add padding&lt;/li&gt;
&lt;li&gt;centering the text content inside the container&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;General Tip 1&lt;/em&gt;: When in doubt, add a &lt;code&gt;border-radius: 1px solid red&lt;/code&gt; around the element to easily identify where you are actually applying your styles.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;General Tip 2&lt;/em&gt;: Always, I mean always just have your Dev tools open at all times while you are checking where you are applying certain styling like your padding. 90% of the problems can be solved by simply inspecting the element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;style&amp;gt;
      *{
        box-sizing: border-box;
        font-family: 'Inter', sans-serif;
      }

      body{
        margin: 4rem 0;

      }
      .splide__slide{
        padding: 6rem;
        display: flex;
        justify-content: center;
        background: #ECF0F1 ;

      }

      .splide__slide__container{
        width: 600px;
        text-align: center;
      }

      .slide__content{
      margin: 1rem 0;
      font-size: 1.3rem;
      }

      .slide-img__wrapper{
        display: flex;
        align-items: center;

      }

      .slide-img{
        width: 60px;
        height: 60px;
        border-radius: 50%;
        max-width: 100%;
        max-height: 100%;
      }

      .slide-img__caption{
        margin-left: 1rem;
      }

      .slide-img__caption span{
        color: #2E86C1 ;
        font-weight: 500;
      }
    &amp;lt;/style&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 6&lt;/strong&gt;: Adding JavaScript&lt;br&gt;&lt;br&gt;
Activate the carousel by creating a new Splide object to the HTML on page load.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
    document.addEventListener( 'DOMContentLoaded', function () {
        new Splide( '.splide' ).mount();
    } );
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and with this the carousel is done. Now if you want to further modify it, here's the trick. Go to the HTML page. Inspect the whole page and you can notice that there are elements like &lt;code&gt;splide__arrows&lt;/code&gt; and &lt;code&gt;splide__pagination&lt;/code&gt;. What do these mean ?&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%2F20c80i5xw96kmwddz2v3.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%2F20c80i5xw96kmwddz2v3.png" alt="Splide pagination custom styling"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7&lt;/strong&gt;: Applying custom styles to Carousel elements (arrows, pagination)  &lt;/p&gt;

&lt;p&gt;If you are building a website which has a primary color and you would like to apply these to the arrows and the pagination at the bottom of the slide, you can just apply styles by grabbing the element class names from Dev tools and applying styles to them.&lt;/p&gt;

&lt;p&gt;As an example,let's change the default style of the pagination circles.  &lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br&gt;
.splide__pagination__page.is-active {&lt;br&gt;
        transform: scale(1.5); //here I have changed the scale from 1.4 to 1.5&lt;br&gt;
    background: #0b72e7; //changed from default bg-color to blue&lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
This means that when the the current page is active, the color will be activated.  &lt;/p&gt;

&lt;p&gt;Same method can be used for the splide__arrows as well !&lt;/p&gt;

&lt;h2&gt;
  
  
  Dessert
&lt;/h2&gt;

&lt;p&gt;There are sure a few more amazing way to modify your slider and for that do go check out the &lt;a href="https://splidejs.com/getting-started/" rel="noopener noreferrer"&gt;Splide Docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Find the whole code for this article on my &lt;a href="https://codesandbox.io/s/splidejs-carousel-4hgvl?file=/index.html" rel="noopener noreferrer"&gt;Codesandbox&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope you learned something new. Till then keep sliding !&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/xjZtu4qi1biIo/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/xjZtu4qi1biIo/giphy.gif" alt="dessert gif"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>html</category>
      <category>todayilearned</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Solving Event Handling Errors in React</title>
      <dc:creator>Ridhik Govind</dc:creator>
      <pubDate>Sun, 25 Apr 2021 19:49:15 +0000</pubDate>
      <link>https://dev.to/ridhikgovind/solving-event-handling-errors-in-react-4l7d</link>
      <guid>https://dev.to/ridhikgovind/solving-event-handling-errors-in-react-4l7d</guid>
      <description>&lt;p&gt;Event handling in React is one of the basic things you learn when you start learning React. Some example are: 'onClick', 'onSubmit', 'onChange' etc, and if you are coming from a Vanilla JavaScript path, handling events in React will feel a lot easier. But it does come with some basic understanding of the JSX syntax and using React state.&lt;/p&gt;

&lt;p&gt;So let's get started ? As always starting with our end goal and how we would be reaching that goal.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GOAL:&lt;/strong&gt; To change the word from 'heyy' to 'byee' on the click of the button&lt;br&gt;&lt;br&gt;
&lt;strong&gt;METHOD:&lt;/strong&gt; Using an event handler and React state &lt;/p&gt;

&lt;p&gt;&lt;em&gt;below is a basic layout of how the app is structured&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function App() {

  const[word,setWord] = useState('heyy')
  return(
    &amp;lt;div&amp;gt;
      {word}
      &amp;lt;button&amp;gt;Click here&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  CASE - 1: Doing it the wrong way.
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function App() {

  const[word,setWord] = useState('heyy')
  return(
    &amp;lt;div&amp;gt;
      {word}
      &amp;lt;button onClick="byee"&amp;gt;Click here&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now this isn't what people do in real life but I just wanted to show you the error which we will get if we do this: Here's the error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error
Expected `onClick` listener to be a function, instead got a value of `string` type.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why ? Because event handlers must always be a &lt;strong&gt;&lt;em&gt;function&lt;/em&gt;&lt;/strong&gt; or a &lt;strong&gt;&lt;em&gt;reference to a function&lt;/em&gt;&lt;/strong&gt;. Here the button will not work because the event handler here i.e &lt;code&gt;onClick&lt;/code&gt; is set to a string.&lt;/p&gt;

&lt;h2&gt;
  
  
  CASE - 2: Doing it the confusingly wrong way
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button onClick={setWord('bye')}&amp;gt;Click here&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now look at the above code. Logically speaking when one looks at this code it is simple - you have an event handler which is set to &lt;code&gt;setWord&lt;/code&gt; function, inside which we have &lt;strong&gt;'bye'&lt;/strong&gt; argument, hence its a &lt;strong&gt;&lt;em&gt;function call&lt;/em&gt;&lt;/strong&gt;. So if a user clicks this button, the state will be updated. Right ? &lt;/p&gt;

&lt;p&gt;Simple answer is Yes, the state will be updated BUT with a BIG error that goes 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;Error
Too many re-renders. React limits the number of renders to prevent an infinite loop.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So why do we get this 'too many re-renders' error ? Well, for that let's  kick back some React basics.&lt;/p&gt;

&lt;p&gt;Now in React curly braces like this one &lt;code&gt;{}&lt;/code&gt; are a special JSX syntax which are used to evaluate some JavaScript variable, function or any code that has a value. &lt;/p&gt;

&lt;p&gt;So if we look at the code above in this Case, we can see that we have written &lt;code&gt;onClick={setWord('bye')}&lt;/code&gt;. The problem with this is that at the very first time the app runs (i.e first render), this curly braces gets executed regardless of whether it is attached to an event listener or not. That is one feature of this special curly braces. Don't believe me ? try the below code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function App() {

  const[word,setWord] = useState('heyy')
  return(
    &amp;lt;div&amp;gt;
      {word}
      &amp;lt;button onClick={console.log('bleh! I just ran just like that')}&amp;gt;Click here&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Did you see that the output got logged at the first render itself without even clicking the button ? You bet !&lt;/p&gt;

&lt;p&gt;Note:  But if we click the button, nothing happens. This is because like we said curly braces is executing the console.log function, and we know that a &lt;strong&gt;&lt;em&gt;function should return something&lt;/em&gt;&lt;/strong&gt;. But since, here no value is being returned, &lt;code&gt;undefined&lt;/code&gt; is being returned, thus nothing is logged out.&lt;/p&gt;

&lt;p&gt;Now, I said that the &lt;strong&gt;'STATE WILL BE UPDATED'&lt;/strong&gt; in this case. But you wont be able to see because the error is being displayed on top of everything. But here's one way to debug you code and see what really happens under the hood. Try this code instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="p"&gt;export default function App() {
&lt;/span&gt;
    const[word,setWord] = useState('heyy')
&lt;span class="gi"&gt;+   console.log(word);
&lt;/span&gt;     return(
        &amp;lt;div&amp;gt;
        {word}
        &amp;lt;button onClick={setWord('byee')}&amp;gt;Click here&amp;lt;/button&amp;gt;
       &amp;lt;/div&amp;gt;
  )
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now check your console and you will see that the error has happened because the state has been updated too many times. Now combining the basics of curly braces, we can understand that this is what actually happens: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1st RENDER - curly braces runs the code inside it whether if it's attached to an event listener or not, state gets updated to 'byee', starts the 2nd RENDER&lt;/li&gt;
&lt;li&gt;2nd RENDER - again same thing happens: curly braces are executed, state gets updated to 'byee' again, starts 3rd RENDER and it goes on..and on.. till React is like "Okay dude, enough is enough,this is too much, lemme show you some errors".
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  CASE - 3 : Some better ways to use an event handler
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Better way - 1: Defining event handlers directly in the button's attribute
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button onClick={() =&amp;gt; setWord('bye')}&amp;gt;Click here&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basically, by setting the event handler to an arrow function which points to a reference, no function gets called until and only when the button is clicked.&lt;/p&gt;

&lt;p&gt;When we do it this way, we are like "Hey onClick, I have given you one function, which contains some activity to be done. ONLY DO IT when I click something on screen.&lt;/p&gt;

&lt;h3&gt;
  
  
  Better way - 2: Cleaner way to do it.
&lt;/h3&gt;

&lt;p&gt;Not many people prefer to do it the first way because sometimes, we have to add more than one activity to this function. So we can do it this way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function App() {
  const [word, setWord] = useState("heyy");

  const handleClick = () =&amp;gt; {
    setWord("Byee");
  };

  return (
    &amp;lt;div&amp;gt;
      {word}
      &amp;lt;button onClick={handleClick}&amp;gt;Click here&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we have created a separate function that does something i.e change the state, and have attached this function to the event handler.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion:
&lt;/h1&gt;

&lt;p&gt;Making sure you understand the bare basics of React is really important.You can get away with these tiny errors by making some adjustments, but in the long term its good to know what actually causes this error. Thanks for reading and hope you have learned something new today 😊. Byee !&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/QN6NnhbgfOpoI/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/QN6NnhbgfOpoI/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to style your React-Router links using styled-components</title>
      <dc:creator>Ridhik Govind</dc:creator>
      <pubDate>Sun, 28 Feb 2021 06:08:40 +0000</pubDate>
      <link>https://dev.to/ridhikgovind/how-to-style-your-react-router-links-using-styled-components-2350</link>
      <guid>https://dev.to/ridhikgovind/how-to-style-your-react-router-links-using-styled-components-2350</guid>
      <description>&lt;p&gt;So you've just started using React-Router for easily routing the pages in your React-web app and if you're also using Styled-components, then kudos - this article is for you. In this article I will explain how to easily style your React-Router Links by going through 3 main methods of styling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Goals&lt;/strong&gt; - To code out efficient and cleaner code that are reusable that can help us when we are making our application bigger.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: It is assumed that you already know how to work with React Router and styled-components at a basic level&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  METHOD 1: Styling links using inline style attribute.
&lt;/h2&gt;



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

import { Link } from "react-router-dom";
import styled from "styled-components";

const NavUnlisted = styled.ul`
  text-decoration: none;
`;

const linkStyle = {
  margin: "1rem",
  textDecoration: "none",
  color: 'blue'
};

function Nav() {
  return (
    &amp;lt;NavUnlisted&amp;gt;
      &amp;lt;Link to="/" style={linkStyle}&amp;gt;
        Home
      &amp;lt;/Link&amp;gt;
      &amp;lt;Link to="/about" style={linkStyle}&amp;gt;
        About
      &amp;lt;/Link&amp;gt;
    &amp;lt;/NavUnlisted&amp;gt;
  );
}

export default Nav;

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

&lt;/div&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%2Fraw.githubusercontent.com%2FRidhikGovind%2Ftestlinks%2Fmain%2FPART-1.gif%3Ftoken%3DAO6Z76PFJR2YJQXJZNYXHV3AHISR4" 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%2Fraw.githubusercontent.com%2FRidhikGovind%2Ftestlinks%2Fmain%2FPART-1.gif%3Ftoken%3DAO6Z76PFJR2YJQXJZNYXHV3AHISR4" alt="gif" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt; : So you have the following code with you. It contains an 'unordered nav list' component named &lt;code&gt;NavUnlisted&lt;/code&gt;. But let's just keep our main focus on the &lt;code&gt;Link&lt;/code&gt; component shall we. Now the thing is you cannot directly style the &lt;code&gt;Link&lt;/code&gt; using styled components this way. Why ? Because under the hood, &lt;code&gt;Link&lt;/code&gt; is just an anchor tag  or &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tag that we are importing from the styled-components. So we can't create a constant variable with this &lt;code&gt;Link&lt;/code&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 Link = styled.a``;  //error - **Link** already has been declared
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt; : Use &lt;code&gt;inline style attribute&lt;/code&gt;. These basically are similar to how we use inline styles in HTML.So we create a &lt;code&gt;style&lt;/code&gt; attribute with the styles inside it in an object form. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt; : This is not so great way as it will become difficult to code out and track individual styles as the app scales - Also, it does not meet the standards of our &lt;strong&gt;Goals&lt;/strong&gt; stated at the beginning of the article.  &lt;/p&gt;

&lt;p&gt;Here is a &lt;a href="https://codesandbox.io/s/styling-react-router-linksstarter-template-tctnj?file=/src/Nav.js" rel="noopener noreferrer"&gt;Codesandbox&lt;/a&gt; link if you are interesting in taking a quick glance at the code for METHOD-1 and if you wish you can follow along too. &lt;/p&gt;

&lt;h2&gt;
  
  
  METHOD 2: Styling links using 'styled.componentName' format.
&lt;/h2&gt;

&lt;p&gt;If you are familiar with styled components, you should know that &lt;code&gt;styled&lt;/code&gt; is like the very basic thing you import from styled-components.&lt;code&gt;styled&lt;/code&gt; together with 'tagNames' (e.g div or li or h1 etc) or a valid component name can be used to apply styles to a component.&lt;/p&gt;

&lt;p&gt;The reason why we can use the latter one i.e component name, is because we have imported a component here that is &lt;code&gt;Link&lt;/code&gt;, now we can pass this &lt;code&gt;Link&lt;/code&gt; 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;const StyledLink  = styled(Link)`
     //some CSS styles here
`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt; : I know this one's a little tricky but here it goes. So basically what we are doing here is, we are creating a new component and telling it, "Hey I am a new component and I want to be like Mr.Link but in a stylish manner, so I am going to take all of the Mr.Link characteristics and add a little bit a style of my own". So in the end the code 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;const StyledLink = styled(Link)`
  color: Blue;
  text-decoration: none;
  margin: 1rem;
  position: relative;
`;

function Nav() {
  return (
    &amp;lt;NavUnlisted&amp;gt;
      &amp;lt;StyledLink to="/"&amp;gt;Home&amp;lt;/StyledLink&amp;gt;
      &amp;lt;StyledLink to="/about"&amp;gt;About&amp;lt;/StyledLink&amp;gt;
    &amp;lt;/NavUnlisted&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt; : Now you can style your &lt;code&gt;Link&lt;/code&gt; directly by creating another component instance i.e StyledLink, and then applying style to it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt; : This is a cleaner way than METHOD-1 because unlike in the earlier method, here we are actually writing CSS. What I meant by is that - in METHOD-1,we had to write &lt;code&gt;textDecoration&lt;/code&gt; instead of &lt;code&gt;text-decoration&lt;/code&gt;. Are you just noticing this now ?  Greatttt !&lt;/p&gt;

&lt;h2&gt;
  
  
  METHOD-3: Styling React-Router links using 'NavLinks' and 'activeClassNames'.
&lt;/h2&gt;

&lt;p&gt;Well well well, this was the moment where I found something really interesting that led me to write this article. React-Router has a module called &lt;code&gt;NavLinks&lt;/code&gt; that we can use as a component. What's so special about it you may ask ? Well, it was built specifically for styling links you use with React-Router. &lt;strong&gt;NAVLINK is provided by REACT-ROUTER and NOT by STYLED-COMPONENTS&lt;/strong&gt;. Well, that a whole lot of &lt;em&gt;Well's&lt;/em&gt; in one paragraph. Anyway, let's jump into the code but beware there are some major changes here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { NavLink } from "react-router-dom";  

const NavUnlisted = styled.ul`

  display: flex;

  a {
    text-decoration: none;
  }

  li {
    color: red;
    margin: 0 0.8rem;
    font-size: 1.3rem;
    position: relative;
    list-style: none;
  }

  .current {
    li {
      border-bottom: 2px solid black;
    }
  }
`;


function Nav() {
  return (
    &amp;lt;NavUnlisted&amp;gt;
      &amp;lt;NavLink to="/" activeClassName="current" exact&amp;gt;
        &amp;lt;li&amp;gt;Home&amp;lt;/li&amp;gt;
      &amp;lt;/NavLink&amp;gt;
      &amp;lt;NavLink to="/about" activeClassName="current" exact&amp;gt;
        &amp;lt;li&amp;gt;About&amp;lt;/li&amp;gt;
      &amp;lt;/NavLink&amp;gt;
    &amp;lt;/NavUnlisted&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt; : So basically, &lt;code&gt;NavLink&lt;/code&gt; is a special style of &lt;code&gt;Link&lt;/code&gt; that we can add styles to it when it matches a certain path in the URL.So if I am currently in '/' i.e Home, I can apply a style border-bottom to the 'Home' link and if I am in '/about' path then apply style to 'About' link . It would look something like this: &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%2Fraw.githubusercontent.com%2FRidhikGovind%2Ftestlinks%2Fmain%2FPART-2.gif%3Ftoken%3DAO6Z76LC42QV7FAEICXG6O3AHISQG" 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%2Fraw.githubusercontent.com%2FRidhikGovind%2Ftestlinks%2Fmain%2FPART-2.gif%3Ftoken%3DAO6Z76LC42QV7FAEICXG6O3AHISQG" alt="gif" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;activeClassName&lt;/code&gt; is an attribute that we can use to create a class that we can later apply style to it. In our case, we have used the &lt;code&gt;.current&lt;/code&gt; and &lt;code&gt;li&lt;/code&gt; tag to make a border-bottom to the corresponding link whenenver the path of the URL changes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;exact&lt;/code&gt; keyword is used so as to specifically target the currently selected URL. So if the path is '/' then styling will &lt;strong&gt;ONLY&lt;/strong&gt; be applied to the Home page. &lt;em&gt;try removing the exact and see the effect for yourself&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;  : Using NavLink, we can save time by not writing any JavaScript for triggering the current selection and applying corresponding styling. Instead we can simply use the NavLink provided by the React-Router.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt; : This method is effective when you want to apply certain specific styling e.g when the link is currently selected and so on.  &lt;/p&gt;

&lt;p&gt;METHOD - 4: Writing a much cleaner code for METHOD - 3 [BONUS PART]&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const NavUnlisted = styled.ul`
  display: flex;
  a {
    text-decoration: none;
  }
  li {
    color: red;
    margin: 0 0.8rem;
    font-size: 1.3rem;
    position: relative;
    list-style: none;
  }

  .current {
    li {
      border-bottom: 2px solid black;
    }
  }
`;


const links = [
  {name: "Home",path:"/"},
  {name: "About",path:"/about"},
];

function Nav() {
  return (
    &amp;lt;NavUnlisted&amp;gt;
      {links.map((link,index) =&amp;gt; (
        &amp;lt;NavLink key={index} to={link.path} exact activeClassName="current"&amp;gt;
          &amp;lt;li&amp;gt;{link.name}&amp;lt;/li&amp;gt;
        &amp;lt;/NavLink&amp;gt;
      ))}
    &amp;lt;/NavUnlisted&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;Explanation: What we have done here is simple but it can save us a loads of time in the future if we want add or change links easily. Just create a new array with all the objects you need and just 'map' over them. That's pretty much it.  &lt;/p&gt;

&lt;h2&gt;
  
  
  End
&lt;/h2&gt;

&lt;p&gt;So I hope you have learned something new. If you happen to notice any errors or mistakes in this article please feel free to point it out. Wait... You have a much better way to do the above methods ?? Let me know in the comments below 😃&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%2F6tde8rndjs0ckiut9xfn.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%2F6tde8rndjs0ckiut9xfn.gif" width="480" height="270"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I know I know !&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;References: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://styled-components.com/docs/api#styled" rel="noopener noreferrer"&gt;styled - Styled Components&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactrouter.com/web/api/NavLink" rel="noopener noreferrer"&gt;NavLink - React Router&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Find the entire code for all the methods in this &lt;a href="https://codesandbox.io/s/styling-react-router-linksstyled-comp-h7k2i?file=/src/Nav.js:0-2756" rel="noopener noreferrer"&gt;Codesandbox&lt;/a&gt;. Don't forget to uncomment the code for each method 😉  &lt;/p&gt;

&lt;p&gt;This is for the Twitter peeps - find me &lt;a href="https://twitter.com/fluffyRidz" rel="noopener noreferrer"&gt;@fluffyRidz&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactrouter</category>
      <category>styledcomponents</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>MVC architecture: Explained with an example</title>
      <dc:creator>Ridhik Govind</dc:creator>
      <pubDate>Sat, 20 Feb 2021 03:46:59 +0000</pubDate>
      <link>https://dev.to/ridhikgovind/mvc-architecture-explained-with-an-example-9od</link>
      <guid>https://dev.to/ridhikgovind/mvc-architecture-explained-with-an-example-9od</guid>
      <description>&lt;h2&gt;
  
  
  What is an MVC architecture ?
&lt;/h2&gt;

&lt;p&gt;An MVC is a popular software design pattern which is used to make applications by dividing the user interface(UI) and the logic of the program into different components. MVC stands for Model, View and Controller.In simple terms, it is used to divide the server code and the client code into different parts.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/Tl25hw4bD37AQ/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/Tl25hw4bD37AQ/giphy.gif" alt=""&gt;&lt;/a&gt;  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How I used to think an MVC structure was at first. But don't be scared its not that complex okay ! &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why use MVC ?
&lt;/h2&gt;

&lt;p&gt;If you already using React, then you know how writing code into 'components' has made our life easier than before. An MVC architecture has an almost similar approach - meaning we can write code into 3 main components which has the following benefits: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Easy to maintain and expand since each important parts are in separate files namely Model,View and Controller.&lt;/li&gt;
&lt;li&gt;Can easily be tested individually without affecting one another.&lt;/li&gt;
&lt;li&gt;There are already many frameworks which uses MVC frameworks to build out web apps e.g Express, Ruby on Rails etc.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  So what are Model,View and Controller first of all ?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;View&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Consists of the UI(What the User actually sees and interacts with)&lt;/li&gt;
&lt;li&gt;e.g React can be considered a view or if you are using a JavaScript templating engine like Ejs,this can be a view too.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Model  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Contains all the logic of how the data should structure inside a database. e.g Mongoose Schemas (if you are familiar with it) or in simple terms the structure in which data is to be stored in the Database.
&lt;/li&gt;
&lt;li&gt;Handles all the behind the scenes activities of storing data into the Database(Inserting,deleting,updating data etc.)
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Controller  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The controller is like the intermediary through which all the requests and data between the View and the Model go through. &lt;strong&gt;Also do notice that: the View and the Model never meet or pass data with each other,every request must pass through the controller making it such an important component in an MVC architecture&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;It basically handles all the requests like (GET,POST,PUT,DELETE etc) through the use of &lt;strong&gt;View&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pictorial explanation
&lt;/h2&gt;

&lt;p&gt;So lets explain this concept with a help of a diagram: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vf4xB1QL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6fm9dfcvwxdphhnsq6n9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vf4xB1QL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6fm9dfcvwxdphhnsq6n9.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;So what happens here is there is a User, who wants to add a movie.He uses the &lt;strong&gt;View&lt;/strong&gt; (which is the UI of the web app) to makes some changes to it - by inputting some movie details  into an input field.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When the user submits the Data using a submit button in Step 1, a request( in this case its a HTTP POST request) is made to the &lt;strong&gt;Controller&lt;/strong&gt;. So in simple terms we are sending the input value to the &lt;strong&gt;Controller&lt;/strong&gt;.   &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;Controller&lt;/strong&gt; receives this data from the &lt;strong&gt;View&lt;/strong&gt; and then handles or passes the request (in our case it is a POST request) to the Database for storing - Because unless the data is stored,we wont be able to retrieve it later on.For this we need to send it to the &lt;strong&gt;Model&lt;/strong&gt;.  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;Model&lt;/strong&gt; handles all the data logic for getting this data from the &lt;strong&gt;Controller&lt;/strong&gt; and then storing it in the Database behind the scenes (e.g storing the data with the correct 'key:value' pair in the Database).In our case, we are inserting this movie details into our database which contains a list of movies. But its not finished yet, because the data is only inserted into our Database's movie list but the user is not made aware of this. That is why we have to send the "updated movie list" back to the user. How do we do this?  &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The &lt;strong&gt;Model&lt;/strong&gt; sends the updated movie list back to the &lt;strong&gt;Controller&lt;/strong&gt; with a message - "Hey I have successfully inserted the movies into the Database okay ? :)".  But if the &lt;strong&gt;Model&lt;/strong&gt; fails to insert the movie,then it sends an error message instead saying "Sorry but the movie couldn't be inserted into the Database :( ".  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Now the &lt;strong&gt;Controller&lt;/strong&gt; cannot 'control the happiness' (yep, pun intended!) of receiving the updated movie list from the &lt;strong&gt;Model&lt;/strong&gt; and shares its happiness with the &lt;strong&gt;View&lt;/strong&gt;.  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the &lt;strong&gt;View&lt;/strong&gt; the movie list is finally updated and sends this results back to the browser that the user is using. Now the user can finally see the updated movie list on his screen including the new movie that he has created.  &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  An example of how the MVC code structure looks like for a simple app.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;#1 When using Ejs template engine as the view&lt;/strong&gt; &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gL-nq0Oq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qypm8vujvbedzms5a6fz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gL-nq0Oq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qypm8vujvbedzms5a6fz.png" alt="Alt Text"&gt;&lt;/a&gt; &lt;br&gt;
&lt;em&gt;Inside the 'Views' folder contains all the necessary pages that a user will interact with&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;#2 When using React as the view&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DA4DEyK4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1a17v0kfwzzc07vlfyl7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DA4DEyK4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1a17v0kfwzzc07vlfyl7.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
*Inside the 'client' folder contains all the UI.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Learning the MVC pattern can be pretty useful when you want to big bigger apps that are easy to manage.If you havn't understood this concept in your first shot,no worries it takes some time.Please let me know if there are any mistakes or suggestions to be made in this article :)&lt;/p&gt;

&lt;p&gt;Incase we are meeting for the time - Hi ! my name is Ridhik, I love building stuff up from scratch and jot down my learning into articles. Feel free to connect with me on Twitter(&lt;a href="https://twitter.com/fluffyRidz"&gt;@fluffyRidz&lt;/a&gt;).&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Learn React Context by building a dead simple wallet app (For beginners)</title>
      <dc:creator>Ridhik Govind</dc:creator>
      <pubDate>Wed, 13 Jan 2021 09:35:57 +0000</pubDate>
      <link>https://dev.to/ridhikgovind/learn-react-context-by-building-a-dead-simple-wallet-app-for-beginners-20a5</link>
      <guid>https://dev.to/ridhikgovind/learn-react-context-by-building-a-dead-simple-wallet-app-for-beginners-20a5</guid>
      <description>&lt;p&gt;If you are a beginner to React and you have been using props to pass data from one component to another then this article is for you to help level up your context game.&lt;/p&gt;

&lt;p&gt;P.S - This article is mainly focused on absolute beginners and thats why I have tried to explain things in details - its gonna be pretty long. It is also assumed that you have the basic knowledge of how React Hooks work.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why use React Context ?
&lt;/h3&gt;

&lt;p&gt;You see there is a concept called as 'prop-drilling' in React . Basically it means passing down certain data from a parent component to another through props. React context is an easier alternative to this method.(explained in detail later on)  &lt;/p&gt;

&lt;h3&gt;
  
  
  What are props ?
&lt;/h3&gt;

&lt;p&gt;Imagine your own parents, see they must be having some kind of special characteristics. It could be the color of the eyes, skin color, shape of the nose etc. Similarly, in React each parent component has characteristics( or lets call it properties or even a better word would be - props) can be passed down to the children in case we want to make changes to the child components based on the parent components. &lt;/p&gt;

&lt;h3&gt;
  
  
  So what's with the whole 'props' my friend ?
&lt;/h3&gt;

&lt;p&gt;Let's understand this with the help of an example.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Create a parent component. Now also notice that we have created some styles that the parent wants to pass to its child.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";

style = {
color: "red"
}

function Parent() {
  return (
    &amp;lt;div&amp;gt;
      Parent component
    &amp;lt;/div&amp;gt;
  );
}

export default Parent;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Create a Child component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'

function Child() {
  return(
    &amp;lt;div&amp;gt;
      Child component
    &amp;lt;/div&amp;gt;
  )
}

export default Child
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Now comes the important part - Passing props. First of all in your parent component import the Child component and wrap the child inside the parent. Second, pass the "style" variable as the props(named style) to the child component. Here's how you can do do that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;//Parent.js
&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;function Parent() {
&lt;/span&gt;  return (
    &amp;lt;div&amp;gt;
      Parent component
&lt;span class="gi"&gt;+     &amp;lt;Child style={style} /&amp;gt;
&lt;/span&gt;    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Now the only part left is grabbing this passed prop from the child component's side and applying the style to the child element.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; First, using the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#object_destructuring" rel="noopener noreferrer"&gt;destructuring method&lt;/a&gt;, we extract the "style" prop from the parent. Now this is available as a variable we can use anywhere. e.g:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Child({ style }) {
  return &amp;lt;div style={style}&amp;gt;Child component&amp;lt;/div&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Second, just apply the style to the child element, that's it voila ! The text in your element in now Red.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's how the final code for both parent and child components will look like: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/react-context-parent-child-props-example-1f8kl?file=/src/App.js" rel="noopener noreferrer"&gt;https://codesandbox.io/s/react-context-parent-child-props-example-1f8kl?file=/src/App.js&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  But the title says we were going to build a dead simple wallet app? Where is that ?
&lt;/h3&gt;

&lt;p&gt;So the reason I explained using the prop method this extensively is because first we won't be jumping into building a wallet using React Context. Instead, we will go through some base model code of how this wallet should be &lt;strong&gt;Using props&lt;/strong&gt; and then we shall look at how easy-peasy it is using &lt;strong&gt;React context&lt;/strong&gt;.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Let's begin &amp;gt;&amp;gt;&amp;gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  METHOD 1: BASE CODE FOR WALLET - (USING PROPS)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Story time&lt;/strong&gt; : There are 3 banks - Paytm, Sbi and Axis. These 3 banks they regularly deal with money. But the only problem is, if Paytm(1st bank) wants to send money to Axis(3rd bank), Paytm can ONLY do this by first transferring the money to Sbi and then Sbi will transfer the money to Axis. ( I know normally, this is not the case, but in our example we assume it this way ). &lt;/p&gt;

&lt;p&gt;So the main way these three banks transfer money through each other is through 'props' - yep, the same props we have explained above. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GOAL:&lt;/strong&gt;  Our goal is to transfer money from Paytm to Axis bank by having Sbi as a middle group just for passing the props and nothing else. You can think Sbi as more of a bridge/agent here.&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%2Fi%2F9ivolzg2cpm567q5msno.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%2Fi%2F9ivolzg2cpm567q5msno.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Create 3 components namely - Paytm.js, Sbi.js, Axis.js.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Let's talk about Paytm.js first. Here you go:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from "react";
import Sbi from "./Sbi";
function Paytm() {
  const [amount, setAmount] = useState(200);

 //send PaytmName money to Axis bank
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h2&amp;gt;Paytm&amp;lt;/h2&amp;gt;
      &amp;lt;Sbi amount={amount}/&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default Paytm;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Explanation:&lt;/em&gt; So here have created a Paytm.js component. Inside there is a state hook that is having a default 'amount' value of 200. But could you just look at our goal: It says 'transfer this amount 200  to Axis bank using Sbi as an agent'. So we pass this 'amount' state varibale to the prop 'amount' (you can name your prop anything you want but for the sake of simplicity I'm naming it amount).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Time for Sbi to act as an agent.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import Axis from "./Axis";

function Sbi({amount}) {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h2&amp;gt;Sbi&amp;lt;/h2&amp;gt;
      &amp;lt;Axis amount={amount}/&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default Sbi;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Explanation:&lt;/em&gt; Remember the Parent-Child example I explained from above ? And how I explained the whole dealing with the 'prop', 'destructuring' etc ? Here is the exact same thing we are doing. Here, Sbi extracts the prop passed from Paytm and then passes it as the props to  its child element (Axis).If you havn't understood this part, don't worry, just go through the Parent-Child example.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Last and final part - creating our Axis.js.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";

function Axis({amount}) {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h2&amp;gt;Axis received {amount} from Paytm through sbi by way of props&amp;lt;/h2&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default Axis;

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

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Explanation:&lt;/em&gt; Same concepts applied - you destructure the value from the parent of Axis i.e Sbi and then you use the variable inside the text. Now if you are coding along with me until now, you will notice that the amount value of 200 has been Successfully been transferred from Paytm -&amp;gt; Axis Bank. This is how you pass a value through props &lt;em&gt;MISSION COMPLETED&lt;/em&gt;.  &lt;/p&gt;

&lt;p&gt;Have a look at the code: &lt;a href="https://codesandbox.io/s/react-bank-exampleusing-props-xpxv6?file=/src/Axis.js:0-192" rel="noopener noreferrer"&gt;https://codesandbox.io/s/react-bank-exampleusing-props-xpxv6?file=/src/Axis.js:0-192&lt;/a&gt;  &lt;/p&gt;




&lt;h3&gt;
  
  
  METHOD 2: BUILDING A WALLET (USING REACT CONTEXT)
&lt;/h3&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%2Fi%2Fluu3g5au7a85hjqr1ark.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%2Fi%2Fluu3g5au7a85hjqr1ark.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So this is basically what we will be building, I know its not much of a wallet app but there's a lot going on here. &lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding React Context
&lt;/h3&gt;

&lt;p&gt;'Context' in english language simply means 'any data/information that has been set already which can help us understand the current situation'. In React, creating a context simply means creating a universal store of values and grabbing them whenever we need.&lt;br&gt;
The rule that React Context follow is: Store once, use anywhere. How it works is because React context has two objects  - a Provider and a Consumer. A Provider, as the name suggests is where we have to store the data and a Consumer is the one who uses this data. So in our example, if Paytm is the Provider, then both Sbi and Axis are consumers. But when we look at our goal, then which one do you think our consumer should be ? Axis bank of course !&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Goal:&lt;/strong&gt; Send money from Paytm -&amp;gt; Axis bank &lt;strong&gt;without transferring the money through Sbi&lt;/strong&gt; by using React Context.   &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; We start by creating &lt;code&gt;MyContext.js&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

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

import React from "react";

const MyContext = React.createContext();

export default MyContext;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Explanation:&lt;/em&gt; In React, the context object comes pre-loaded. So if we need to make use of context, we have to initialize context by creating React context object using &lt;code&gt;createContext&lt;/code&gt; and then storing this in a variable called 'MyContext', then exporting it. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Create a Context Provider inside Paytm.js&lt;br&gt;
&lt;/p&gt;

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

import React, { useState } from "react";
import Sbi from "./Sbi";

import Total from "./Total";
import Balance from "./Balance";

import MyContext from "./MyContext";

const style = {
  background: "black",
  color: "red",
  padding: "0.5rem",
  display: "grid",
  gridTemplateColumns: "1fr 1fr"
};

 function Paytm() {

  const [amount, setAmount] = useState(200);
  const [total, setTotal] = useState(500);
  const [balance, setBalance] = useState(null);

  //Goal: send Paytm money to Axis bank without the passing the money through Sbi

  return (
    &amp;lt;MyContext.Provider value={{
        amount,
        total,
        setTotal,
        balance,
        setBalance,

      }}&amp;gt;
      &amp;lt;div&amp;gt;
      &amp;lt;div className="paytmWallet" style={style}&amp;gt;
          &amp;lt;Total /&amp;gt;
          &amp;lt;Balance /&amp;gt;
      &amp;lt;/div&amp;gt;
        &amp;lt;h2&amp;gt;Paytm&amp;lt;/h2&amp;gt;
        &amp;lt;Sbi /&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/MyContext.Provider&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Explanation:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, we import the MyContext variable from &lt;code&gt;MyContext.js&lt;/code&gt;. Why ? So that we can create a Provider.
&lt;/li&gt;
&lt;li&gt;Create a Provider object using MyContext as shown above. One important thing to notice here is that, you have to wrap the child components inside the Provider so that each child has access to the data from the central store of the Provider. E.g Since Axis is a child of Sbi, we have to wrap Sbi around Paytm's Provider.
&lt;/li&gt;
&lt;li&gt;Next, we create some state hooks with some default values that we will pass as 'props' to the Provider. This 'props' that we are passing is available to all the child elements down the road that is wrapped inside this Provider. e.g Since Sbi is wrapped inside, Sbi.js has access to it, so does Axis.js.
&lt;/li&gt;
&lt;li&gt;We have also created a small wallet section that is going to display our Paytm wallet details with some basic styling.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; We create the Sbi.js component which has nothing but its child component Axis.js&lt;br&gt;
&lt;/p&gt;

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

import Axis from "./Axis";

function Sbi() {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h2&amp;gt;Sbi&amp;lt;/h2&amp;gt;
      &amp;lt;Axis /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Create Axis.js component. This is where we have to use &lt;code&gt;useContext&lt;/code&gt; to grab the data that we have passed as props to the Provider.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In React, we can either follow one approach which is creating a Consumer object from 'MyContext'. But here we will be following the 'useContext' approach.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useContext&lt;/code&gt; is a hook (just like useEffect and useState) that is used to grab the prop values from the nearest Provider. It works by accepting the context object i.e MyContext and then returning the prop value of the nearest Provider i.e Paytm.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import MyContext from './MyContext'

function Axis() {
  const value = React.useContext(MyContext)
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h2&amp;gt;Axis received &amp;lt;span style={value.amountStyle}&amp;gt;{value.amount}&amp;lt;/span&amp;gt; from Paytm through sbi by way of useContext&amp;lt;/h2&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default Axis;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Explanation:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As you can see, the variable 'value' contains all the values of the 'value' prop that we have passed in the Provider. So in order to display the amount that has been transferred, you can use &lt;code&gt;{value.amount}&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;But it is not only state hooks that we can pass as 'props' to the Provider, we can even pass objects. E.g If we need to apply a certain style, we the pass the object 'amountStyle' and in Axis we can use it by way of &lt;code&gt;{value.amountStyle}&lt;/code&gt; and this will apply all the styles as usual. See how easy it is than doing 'prop-drilling' ! &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In paytm.js pass in the following 'amountStyle' below 'style' object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const amountStyle = {
  background: "black",
  color: "red"
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now pass 'amountStyle' to the list of value props of the Provider.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; This step will be one of the easist, because you already have an idea of how Context works now. &lt;br&gt;
This step involves creating a 'Total' and 'Balance' component:&lt;br&gt;
&lt;/p&gt;

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

import React from 'react'
import MyContext from './MyContext'

function Total() {
  const value = React.useContext(MyContext)
  return (
    &amp;lt;div className="total"&amp;gt;Total:{value.total} &amp;lt;/div&amp;gt;
  )
}

export default Total;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





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

import React from 'react'
import MyContext from './MyContext'

function Balance() {

  const value = React.useContext(MyContext)

  return (
    &amp;lt;div className="Balance" &amp;gt;Balance:{value.total - value.amount} &amp;lt;/div&amp;gt;
  )
}

export default Balance;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Explanation:&lt;/em&gt; While the total value remains the same, the balance value is changed dynamically according to the amount that has been transferred from 'Paytm wallet' to 'Axis' bank.&lt;br&gt;&lt;br&gt;
Try changing the 'amount' value from 200 to 300. You can notice that the 'balance' amount has changed - this is because we are changing the state of 'amount' in Paytm (Provider) also.  &lt;/p&gt;

&lt;p&gt;At the end the whole code would look something like this:&lt;br&gt;&lt;br&gt;
&lt;a href="https://codesandbox.io/s/react-bank-wallet-using-usecontext-final-u8guh?file=/src/App.js" rel="noopener noreferrer"&gt;https://codesandbox.io/s/react-bank-wallet-using-usecontext-final-u8guh?file=/src/App.js&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Side note:&lt;/em&gt;&lt;/strong&gt; So if you looked at the above code, and you are thinking "What would I gain by just passing values around, I don't think I will be using this often !". &lt;br&gt;
I used to think like this, until I started building bigger applications and passing data from one component to another can turn out to be a total mess. So if you are building a really small app - go on and use the prop method. But, if it has like components that are nested real deep - don't even think twice, use Context.It can be confusing at first, but you'll get the hang of it.  &lt;/p&gt;

&lt;p&gt;What we have learned so far:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Meaning of props through a 'Parent-Child' component example. &lt;/li&gt;
&lt;li&gt;Prop-drilling (when to use it and when not to use it)&lt;/li&gt;
&lt;li&gt;Understand how destructuring works(refer MDN docs for more)&lt;/li&gt;
&lt;li&gt;How to use React context - Provider and Consumer concept&lt;/li&gt;
&lt;li&gt;How to use the 'useContext' hook to grab data from the Context Provider easily by way of creating a wallet app.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As always the mandatory Gif is here: &lt;br&gt;
&lt;a href="https://i.giphy.com/media/ToMjGpESSdSGmC8drfq/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/ToMjGpESSdSGmC8drfq/giphy.gif" alt=" after using react context for the first time"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;This will be you after using React Context for the first time.&lt;/em&gt; &lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Selecting and manipulating CSS pseudo-elements such as ::before and ::after using JavaScript (EASY WAY)</title>
      <dc:creator>Ridhik Govind</dc:creator>
      <pubDate>Sat, 05 Dec 2020 09:13:42 +0000</pubDate>
      <link>https://dev.to/ridhikgovind/selecting-and-manipulating-css-pseudo-elements-such-as-before-and-after-using-javascript-easy-way-1bfj</link>
      <guid>https://dev.to/ridhikgovind/selecting-and-manipulating-css-pseudo-elements-such-as-before-and-after-using-javascript-easy-way-1bfj</guid>
      <description>&lt;p&gt;So this title you are looking at - Yes its an actual stackoverflow question.The question has 980 upvotes and it's top answer has 723 upvotes. Then the question arises, why write an article about it then. Well, for starters I really wanted to answer this question, but since I didnt have enough "reputations",this article is my way of letting people know of this new easy method. &lt;/p&gt;

&lt;h2&gt;
  
  
  First things first
&lt;/h2&gt;

&lt;p&gt;Scenerio 1:&lt;br&gt;
Imagine you want to grab an element using JavaScript and change its color using JavaScript. Its pretty easy, here we go:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//HTML
&amp;lt;div id="text"&amp;gt;Hey there !&amp;lt;/div&amp;gt;

//CSS
#text{
color: red;
}

//JS
const text = document.getElementById('text');
text.style.color = 'blue';

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

&lt;/div&gt;



&lt;p&gt;Scenerio 2:&lt;br&gt;
This time we create a &lt;code&gt;:before&lt;/code&gt; pseudo element on the &lt;code&gt;#text&lt;/code&gt; element and then try to change the pseudo element's background color. So lets see what happens here: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Create a basic pseudo element with the styling(if you are new to creating a pseudo element, I suggest you learn that first and come back here):
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//CSS
#text{
color: red;
position: relative;
}

#text:before{
  position: absolute;
  height: 25px;
  width: 100px;
  background: black;
  top: -30px;
  left: 0;
  content: "";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fi%2F4yqxifphuo1eui11r2uz.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%2Fi%2F4yqxifphuo1eui11r2uz.png" alt="Alt Text" width="250" height="90"&gt;&lt;/a&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now just a little twist to this, instead of using  black as the background color, we are gonna create a &lt;code&gt;:root&lt;/code&gt; element in our CSS file inside which we will create a variable '--pseudo-backgroundcolor' with a value of 'red' and use this varible as the value for 'background'  as shown below.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//CSS
:root{
--pseudo-backgroundcolor: red;
}

#test:before{
 background: var(--pseudo-backgroundcolor);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fi%2Fxlqb2vo7fd7gz55j7t1h.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%2Fi%2Fxlqb2vo7fd7gz55j7t1h.png" alt="Alt Text" width="280" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;So by now I hope you are getting some hint of where I am heading. No ? Okay, let me explain. Our goal is simple, we should create a global background color variable and with the help of JavaScript we will grab the root value of the variable and update it using JavaScript so that the effect will be applied to the pseudo elements background color style automatically.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lets get to work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//JS
const root = document.querySelector(":root"); //grabbing the root element

**important part below** 

root.style.setProperty("--pseudo-backgroundcolor", 'green');

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

&lt;/div&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%2Fi%2F45qycf8kgokg6czvpy5e.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%2Fi%2F45qycf8kgokg6czvpy5e.png" alt="Alt Text" width="267" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So as you can see, we grabbed the &lt;code&gt;--pseudo-backgroundcolor&lt;/code&gt; varible and used &lt;code&gt;setProperty&lt;/code&gt; function to first select the variable and then set its color value from red to green. This is pretty much the code we need.So now if we ever need to change the color, you can just do this dynamically.An example would be to create a button and on click of that button, you can loop through an array of different colors and apply it to this varible.&lt;br&gt;&lt;br&gt;
In the stackoverflow answer, you can see other right ways too, but they just seemed a bit long, while this one just needs you to set the root variable and just write some JS code.&lt;/p&gt;
&lt;h2&gt;
  
  
  BONUS PART:
&lt;/h2&gt;

&lt;p&gt;Suppose you want to add a text to a pseudo element - we usually add text using the &lt;code&gt;content = ""&lt;/code&gt; property.So instead of "" , just create a root variable like we did above and manipulate it using one line of JavaScript.Here's the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//CSS 
:root{
--pseudo-text: "just some text";
}

#text:before {
  content: var(--pseudo-text);
}

//JS
root.style.setProperty("--pseudo-text", `"YAY new text"`);
//**!! Dont forget to add the 'double quotes' around this new text above or else you can't see any changes

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

&lt;/div&gt;



&lt;p&gt;So thats it, Hope you learned something new today or even saved time finding this solution. I got to find this solution while I was making my app - &lt;a href="https://ridhikgovind.github.io/PrettyCover/" rel="noopener noreferrer"&gt;PrettyCover&lt;/a&gt; - which you can use to create Beautiful Cover Images for your blogs.In fact, I have made this cover image using PrettyCover. I would appreciate if you could go and try it out, and if you like it, don't forget to ⭐ the repo.&lt;/p&gt;

&lt;p&gt;Also, here is the codePen containing the full example codes:  &lt;a href="https://codepen.io/ridzman/pen/poEgPGq?editors=0011" rel="noopener noreferrer"&gt;https://codepen.io/ridzman/pen/poEgPGq?editors=0011&lt;/a&gt;    &lt;/p&gt;

&lt;p&gt;And as always ending the article with a Gif. Please do let me know if there are any corrections or clarifications to be made. Ciao !&lt;br&gt;&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%2Fwtj9086yu1gjg1m7amyh.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%2Fwtj9086yu1gjg1m7amyh.gif" width="480" height="266"&gt;&lt;/a&gt;  &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>css</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>Asynchronous JavaScript 
explained in human language</title>
      <dc:creator>Ridhik Govind</dc:creator>
      <pubDate>Fri, 20 Nov 2020 20:28:19 +0000</pubDate>
      <link>https://dev.to/ridhikgovind/asynchronous-javascript-explained-in-human-language-2p9l</link>
      <guid>https://dev.to/ridhikgovind/asynchronous-javascript-explained-in-human-language-2p9l</guid>
      <description>&lt;h2&gt;
  
  
  Async..WHAT JavaScript ??
&lt;/h2&gt;

&lt;p&gt;An Async (short for Asynchronous) JavaScript allows us to run callback functions without actually blocking the execution block, which helps us to run the functions in pararrel to one another.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/WpOsb5eNVWHRr8wi3D/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/WpOsb5eNVWHRr8wi3D/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You know what ? Even I didnt understand a word of what an Async JS meant when I first heard of it - it took me a while to understand what it actually means. So here I won't be going that deep, but I will be explaining just the simple meaning of this while 'Asynckkyy' process. I have divided this article into 2 parts - Part 1 where I explain in language which humans can understand and Part 2 where you become a super human to understand more about it&lt;/p&gt;

&lt;h2&gt;
  
  
  PART 1: HUMAN LANGUAGE
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;RS Hotel&lt;/strong&gt; is an amazing hotel that makes some of the best food in the city. But RS hotel has 2 branches: A branch named 'RS Hotel Sync' and another one named 'RS Hotel Async'. Lets first see how things are working in the first branch : &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sync Branch&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%2Fi%2Feku34g8v7x9t9x9mbl8t.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%2Fi%2Feku34g8v7x9t9x9mbl8t.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So what's happening in this branch ?   &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There are 4 people (A,B,C,D) waiting in line to order food at the kitchen counter.But there is a speciality at this branch - after you place an order you have to wait at the kitchen counter until the food has completed cooking.
&lt;/li&gt;
&lt;li&gt;So here if A places an order, A has to wait there until the food is cooked and delivered to him right there at the kitchen counter.&lt;/li&gt;
&lt;li&gt;A leaves the counter after grabbing his food, now B comes up next a little frustrated because he had to wait for a long time, places his order and waits for the food to get cooked.&lt;/li&gt;
&lt;li&gt;Similarly for C and D the same happens.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Async Branch&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%2Fi%2Fp1mcmt321w0lo05qat9f.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%2Fi%2Fp1mcmt321w0lo05qat9f.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So what's happening in this branch ?  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At first, A place his order and while his food is being cooked, A is informed that he can go to the waiting area.&lt;/li&gt;
&lt;li&gt;This clears up the kitchen counter so next up, B place his order, and right then you see a man behind the kitchen counter shouting "Hey A your food is ready, please come get it before it gets cold !!". A comes, grabs his food and leaves. B's food starts getting prepared and on seeing this B leave towards the waiting area.&lt;/li&gt;
&lt;li&gt;Next up is C, C orders, right then the man shouts for B to come and grabs his food and leave, C's food starts getting prepared and C leaves. &lt;/li&gt;
&lt;li&gt;similarly for D the same happens.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion for PART 1:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Notice in 'Sync' branch how people had to queue up for a long period of time for the food to actually finsish cooking ? This is called the Synchronous way. At a time only one dish can be prepared. This is actually ineffective as other people ( B, C, D) had to actually wait.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Meanwhile, in 'Async' branch, things are going as smooth as butter. People are placing orders, food is getting prepared and when the next person comes to order, the earlier person's food has finished cooking. Also, people are told to wait in a waiting room after ordering so there is literally no queue.This is actually an efficient way to handle mutliple number of processes because the people behind one other don't have to wait for a long time in a queue and also the cooking is being done at the same time behind the scenes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  PART 2: SUPER HUMAN LANGUAGE (ASYNC EXPLAINED A BIT DEEP)
&lt;/h2&gt;

&lt;p&gt;This is where things starts to get a little interesting. Now since you have understood what Async basically is, lets actually see what is really happening asynchronously in an app with the help of my beautiful crafted 'figm-orial representation' - that I so call. &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%2Fi%2Ff4n4mkp2ivkzvv4nvh0i.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%2Fi%2Ff4n4mkp2ivkzvv4nvh0i.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Explanation
&lt;/h4&gt;

&lt;p&gt;Whats happening is similar to the PART 1 explanation, but just that you can see an additional item here - the 'Cooks'. Now the cooks inside the kitchen are really the ones who does all the hardwork okay! &lt;/p&gt;

&lt;p&gt;So what happens here ? Suppose A orders the food, the order is taken by the kitchen and A's order is allocated to 'Cook 1' (the red arrow's gives you a much clear idea). As soon as A's food is done, 'Cook 1' returns this food to the kitchen and back to A. And in Async the orders are taken so fast that each  order should be allocated to separate cooks. &lt;br&gt;
So as soon as A's order is taken, A's food is allocated to 'Cook 1', next up B's order is taken, allocated to 'Cook 2', C's - 'Cook 3', D - 'Cook 4'. As soon as each cook is done, the food is returned to the kitchen where each customer can take the food. See no hassle - feels pretty organised to me.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;&lt;em&gt;Similarly&lt;/em&gt;&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%2Fi%2Flvzdkuqe6aydrwnpguqb.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%2Fi%2Flvzdkuqe6aydrwnpguqb.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is actually what happens in JavaScript. Just replace:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; the 'Customers' with 'Tasks' - These tasks are usually functions inside an app(e.g a Node.js app) with a &lt;em&gt;callback function&lt;/em&gt; (explained below)&lt;/li&gt;
&lt;li&gt; replace the 'Kitchen' with the 'Event loop'- which takes in all the tasks and allocate it to 'threads'&lt;/li&gt;
&lt;li&gt;replace 'cooks' with 'threads' - which handles and processes each task and returns back the completed version to the 'Event loop' which then returns back to the 'Node.js app'.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Someone explain to me what a callback function is !!!
&lt;/h3&gt;

&lt;p&gt;Now a &lt;em&gt;callback function&lt;/em&gt; is basically what I like to call it as a 'Boomerang function'. Know what a Boomerang is ? That curvy little thing that you flip in the air and it just returns back to you. Yup, callback functions can be thought like boomerangs who takes certain orders and then returns back the result after processing them.&lt;/p&gt;

&lt;h2&gt;
  
  
  But where is this 'Async' even used ?
&lt;/h2&gt;

&lt;p&gt;Now, do note in your mind that, the word 'Async' just doesn't only have one meaning that is 'Async functions'. E.g Node.js is a server side framework which actually runs on the basis of these 'Async' operations. And guess what? That is why Node.js is such a popular framework (my favourite too !) because it can handle as many processes as it wants as it won't get blocked because the processes are run Asynchronously.This means less server overhead and faster processes.  Now statements like this might make at least 40% sense to you now.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are some things that you should know while using 'Async functions' ?
&lt;/h2&gt;

&lt;p&gt;First things first, every good thing in this world also has to have some bad sides to it right ? One thing you should be doing is handling errors well. Because if you don't do that well there is a chance that your app might crash. Let the fast processing be the motivation behind handling errors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Time to say bye
&lt;/h2&gt;

&lt;p&gt;So, I hope you have got some idea of what Async is. I have tried to explain this as simple as possible keeping in mind you are an absolute beginner. But its fine if you don't understand it in your first read, because there will be a time when you finally understand it and feel like this: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/l1BgRWyZb4vf0ZKuc/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/l1BgRWyZb4vf0ZKuc/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Meanwhile, Here is one amazing video which will help you understand it better: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=8aGhZQkoFbQ&amp;amp;vl=en&amp;amp;ab_channel=JSConf" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=8aGhZQkoFbQ&amp;amp;vl=en&amp;amp;ab_channel=JSConf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Oh and I am &lt;a href="https://ridhikgovind.netlify.app/" rel="noopener noreferrer"&gt;Ridhik&lt;/a&gt; and nice to meet ya. I am learning how to code better everyday, build and write about them. Currently I am learning React.js and Node.js. Please let me know if there are any errors or corrections to be made.Also, Feel free to Connect with me on &lt;a href="https://twitter.com/fluffyRidz" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>asynchronous</category>
      <category>webdev</category>
    </item>
    <item>
      <title>z-index; Tackling this tiny beast.</title>
      <dc:creator>Ridhik Govind</dc:creator>
      <pubDate>Sun, 08 Nov 2020 08:53:10 +0000</pubDate>
      <link>https://dev.to/ridhikgovind/z-index-tackling-this-tiny-beast-109b</link>
      <guid>https://dev.to/ridhikgovind/z-index-tackling-this-tiny-beast-109b</guid>
      <description>&lt;p&gt;Ever since I start learning how to code, all I knew about z-index was that z-index was something that you could use to make one element &lt;em&gt;position&lt;/em&gt; above another element. In fact, this is true and this is what most beginner friendly tutorials will tell you and that's all I knew about it. &lt;strong&gt;UNTIL&lt;/strong&gt; I started designing my portfolio [&lt;a href="http://ridhikgovind.netlify.app/" rel="noopener noreferrer"&gt;http://ridhikgovind.netlify.app/&lt;/a&gt;] and I had to personally deal with this z-index trouble which made me go haywire for 3 whole days. So what did I do ? I did what 100% of what all those who code would do - google it. And the search results I found out was an eye opening for me.&lt;/p&gt;

&lt;h2&gt;
  
  
  first of all why 'z-index' ? why not 'a-index' or 'g-index' ? Ever thought of that ?
&lt;/h2&gt;

&lt;p&gt;Okay so everyone knows the graph with x and y-axis right ? What about the z-axis in the graph ? For who don't know what it looks like: &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%2Frb.gy%2Fq2t8is" 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%2Frb.gy%2Fq2t8is" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now 'z-index' works the same way like 'z-axis' and this is where the elements stack up. Here is a even better illustration from a *smashingmagazine blog (&lt;a href="https://www.smashingmagazine.com/2009/09/the-z-index-css-property-a-comprehensive-look/):" rel="noopener noreferrer"&gt;https://www.smashingmagazine.com/2009/09/the-z-index-css-property-a-comprehensive-look/):&lt;/a&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%2Frb.gy%2Fyi54bd" 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%2Frb.gy%2Fyi54bd" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that is why its called 'z-index' becuase the elements are stacked along the z-axis.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to tame the z-index !
&lt;/h2&gt;

&lt;p&gt;z-index may look pretty simple, but if you don't understand some of the underlying concepts of it, there is a great chance you might end up in the rabbit hole of using z-index: 999999; (okay maybe a little exaggerated here). Let's talk about some rules now: &lt;/p&gt;

&lt;p&gt;Also, open up this codepen in another tab as we will be  referring to it often but just don't make any modifications yet : &lt;a href="https://codepen.io/ridzman/pen/qBNMQKB" rel="noopener noreferrer"&gt;https://codepen.io/ridzman/pen/qBNMQKB&lt;/a&gt;      &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RULE 1:&lt;/strong&gt; &lt;strong&gt;Always remember the 'stacking order' concept&lt;/strong&gt; - Elements that come later in the HTML markup will have a natural tendancy to be on top of the former elements. You can notice in the codepen you have opened how the grey,blue and green boxes are stacked well on top of each other.     &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RULE 2:&lt;/strong&gt; &lt;strong&gt;z-index will ONLY work on positioned elements&lt;/strong&gt; - Unless you set a fixed or relative or an absolute position property on an element, z-index will have no effect on those elements whatsoever.       &lt;/p&gt;

&lt;p&gt;Can't believe this? worry not ! Go ahead and uncomment that "position:relative" in the CSS file of the codepen above. Now watch the stack order get reversed. Wait what just happened ? Rule 2 has happened - grey having the highest z-index becomes the element of top, followed by blue and green having the lowest z-index &lt;strong&gt;only after applying the position property&lt;/strong&gt;.     &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RULE 3&lt;/strong&gt;:  &lt;strong&gt;A child element takes in the z-index of the parent&lt;/strong&gt; - Time to work your fingers now. Go ahead and make a new  &lt;code&gt;div&lt;/code&gt; with an &lt;code&gt;id="parent"&lt;/code&gt; on top of all HTML elements. Inside this div, cut and paste the whole div with &lt;code&gt;id="grey"&lt;/code&gt;. Final code would look 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;&amp;lt;div id="parent"&amp;gt;
  &amp;lt;div id="grey"&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div id="blue"&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;div id="green"&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and give this &lt;code&gt;parent&lt;/code&gt; the following CSS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#parent {
  position: relative;
  z-index: 1;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;br&gt;&lt;br&gt;
What did we do here ? We made 'grey' the child element of 'parent'. Also, did you notice the grey element now? Oh, grey is under blue element even with a z-index of 9999. How ridiculous is that ! Well, time to face the truth my friend - A child can only act powerful and all bossy only if he is independent(which in reality its true !). But if the child is living under its parent, it only has power which the parents impose. Here the &lt;code&gt;z-index: 1;&lt;/code&gt; of the parent is being passed down to the child as well and thus 'grey' element comes under 'blue' element.     &lt;/p&gt;

&lt;p&gt;Here is a codepen demo of RULE 3 to play with: &lt;a href="https://codepen.io/ridzman/pen/mdEGYbG" rel="noopener noreferrer"&gt;https://codepen.io/ridzman/pen/mdEGYbG&lt;/a&gt;      &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Allow me to quote Paulo Coelho:&lt;br&gt;&lt;br&gt;
"The simple things are also the most extraordinary things, and only the wise can see them."&lt;/p&gt;

&lt;p&gt;I hope reading this made you a little wise ! &lt;/p&gt;




&lt;p&gt;Hey there, I am Ridhik - a beginner learning how to code and allocating insane amount of time for fixing errors. Let me know if I've made any mistakes or there are areas which needs corrections. Connect with me on twitter: &lt;a href="https://twitter.com/fluffyRidz" rel="noopener noreferrer"&gt;https://twitter.com/fluffyRidz&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>html</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>How to hide your precious API keys in React  for beginners.</title>
      <dc:creator>Ridhik Govind</dc:creator>
      <pubDate>Wed, 21 Oct 2020 18:54:08 +0000</pubDate>
      <link>https://dev.to/ridhikgovind/how-to-hide-your-precious-api-keys-in-react-for-beginners-49ng</link>
      <guid>https://dev.to/ridhikgovind/how-to-hide-your-precious-api-keys-in-react-for-beginners-49ng</guid>
      <description>&lt;p&gt;&lt;em&gt;Before reading this, it's assumed that you know the basics of how to push code using git and a bit of React but I have tried to keep it as simple as possible.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What are API keys anyway ?
&lt;/h2&gt;

&lt;p&gt;These can be thought of like an access code that allows us to grab some form of data from somewhere. Eg. Weather data from a weather server (openweathermap.org).&lt;/p&gt;

&lt;h2&gt;
  
  
  Why should we hide it ?
&lt;/h2&gt;

&lt;p&gt;Now for a simple app like this, revealing your API_KEY might not cause you much harm, but imagine if you are building a bigger app you definitely don’t want a random stranger to use your services using your API_KEY – hence its always important to maintain best practices from the beginning itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  So the main question, How do we hide it?
&lt;/h2&gt;

&lt;p&gt;Simple, create a &lt;code&gt;.gitignore&lt;/code&gt; file and type in the file name you wanna hide.Okay if you are a beginner its a bit intense. Don’t worry we’ll figure it out along the way.  &lt;/p&gt;

&lt;h2&gt;
  
  
  What in the world is a .gitignore file ?
&lt;/h2&gt;

&lt;p&gt;It is basically a file where we can include the name of the file/folder we want to hide from the prying eyes of human beings when you are uploading your project files into your Github repo. Now what type of files are we talking about here ? Imagine if you are building a simple weather app and you have in your hand an API_KEY which gives access to the weather info and you don't want anyone else to grab this piece of info.   &lt;/p&gt;

&lt;h2&gt;
  
  
  So now lets get our hands dirty, shall we ?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Create a &lt;code&gt;.gitignore&lt;/code&gt; file in the root (main section) of your project folder (this is important!).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Create another file and name it  &lt;code&gt;api.js&lt;/code&gt; . Now you must have your &lt;strong&gt;API_KEY&lt;/strong&gt; (or whatever you have named) variable with the key value in your &lt;code&gt;App.js&lt;/code&gt; right ? Cut and paste this &lt;strong&gt;API_KEY&lt;/strong&gt; and its &lt;strong&gt;key value&lt;/strong&gt; to &lt;code&gt;api.js&lt;/code&gt;. Dont forget to add &lt;em&gt;export&lt;/em&gt; keyword before the variable. It would look something like this inside your &lt;code&gt;api.js&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const API_KEY = "629ce335eae4t5cce613adf9c55a514a";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This &lt;code&gt;api.js&lt;/code&gt; now contains your &lt;em&gt;API_KEY&lt;/em&gt; details, but hold your horses as your app won't work because now you don’t have any API_KEY in your &lt;code&gt;App.js&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; import the &lt;em&gt;API_KEY&lt;/em&gt; from &lt;code&gt;api.js&lt;/code&gt; to &lt;code&gt;App.js&lt;/code&gt;. What are we doing here ? In simple terms we want to make a copy of the variable in order to use it inside App.js. In React we can import 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;import {API_KEY} from './api'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can notice that the full circle is complete and now your app is working fine.But even though we made a copy of the key to another file, notice that this &lt;code&gt;api.js&lt;/code&gt; file is still visible.   &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Remember in &lt;strong&gt;Step 1&lt;/strong&gt; we created a weird file called &lt;code&gt;.gitignore&lt;/code&gt;. Well, now its time to type in the name of the file we want to hide inside this file. Type in:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Here are ‘/’ means the root of your project folder. So &lt;em&gt;/src/api.js&lt;/em&gt; means we are going inside &lt;strong&gt;&lt;em&gt;src&lt;/em&gt;&lt;/strong&gt; folder then grabbing &lt;code&gt;api.js&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;Step 5. We are almost done, now you can push your code to Github using Git.Now open your Github and go to your weather app project.Are u seeing the &lt;code&gt;.gitignore&lt;/code&gt; file in your root of your folder ? Yes you do. But wanna see the real magic? Go to your &lt;em&gt;src&lt;/em&gt; folder and keep looking for that poor &lt;code&gt;api.js&lt;/code&gt; file as you won’t be seeing it anymore. If you have played the game Among us, you can imagine &lt;code&gt;api.js&lt;/code&gt; as an imposter and now he has vented into thin air.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;BONUS SCENERIO&lt;/strong&gt;: If your have already pushed your code containing this &lt;code&gt;api.js&lt;/code&gt; file in your project folder even before creating a &lt;code&gt;.gitignore&lt;/code&gt; file – Don't worry I have a simple one liner solution. Okay, go ahead and perform step 1 and step 4, and open git and type in the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git rm -–cached  api.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now what this does is it removes that specific file from the working tree or in simple terms the upload list. So when you are uploading/pushing your code this &lt;code&gt;api.js&lt;/code&gt; file is ignored/not included. Now go ahead and push your code again to Github. Enjoy your magic again.  &lt;/p&gt;

&lt;p&gt;Thats it for now folks.Being a beginner in React I might have made some writing mistakes, please do let me know so that I can correct myself . Also this is my first blog  post so yay!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt; HappyCoding /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>react</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
