<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Result-oriented and self-motivated full-stack software engineer.]]></title><description><![CDATA[I like building things on the web.
I do that by using JavaScript and a lot of coffee☕.]]></description><link>https://blog.prathmeshdhatrak.com</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Apr 2026 18:17:28 GMT</lastBuildDate><atom:link href="https://blog.prathmeshdhatrak.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Promise VS setTimeout() in Javascript.]]></title><description><![CDATA[The other day I was talking to one of my seniors and he asked me is your JavaScript strong and do I understand how javascript works under the hood.
So I was confident that I had watched the Philip Roberts "JSConf" video (which BTW is very awesome vid...]]></description><link>https://blog.prathmeshdhatrak.com/promise-vs-settimeout-in-javascript</link><guid isPermaLink="true">https://blog.prathmeshdhatrak.com/promise-vs-settimeout-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><dc:creator><![CDATA[Prathmesh Santosh Dhatrak]]></dc:creator><pubDate>Wed, 09 Nov 2022 15:58:36 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1668010414836/5zqYgxZgP.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The other day I was talking to one of my seniors and he asked me is your JavaScript strong and do I understand how javascript works under the hood.
So I was confident that I had watched the <a target="_blank" href="https://www.youtube.com/watch?v=8aGhZQkoFbQ">Philip Roberts "JSConf" video</a> (which BTW is very awesome video) so that's why I said I have a strong understanding of javascript you can ask me questions.
So then he asked my simple (I thought then) Promise and setTimeout() question, that was. <br /></p>
<h3 id="heading-what-will-execute-faster-an-resolved-promise-callback-or-a-0sec-settimeout"><strong>What will execute faster: an resolved promise callback or a "0sec" setTimeout() ?</strong></h3>
<pre><code><span class="hljs-built_in">setTimeout</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">tO</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'setTimeout function called'</span>);
}, <span class="hljs-number">0</span>);
<span class="hljs-built_in">Promise</span>.resolve(<span class="hljs-literal">true</span>).then(<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">rS</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Promise Resolved function called'</span>);
});
</code></pre><p>So I tried to answer this question with the knowledge that I had.
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1668009168208/lgiS-UAil.png" alt="image.png" class="image--center mx-auto" /></p>
<ul>
<li>So first the setTimeout will be called in the call stack after the main execution contest is added. </li>
<li>Then as the setTimeout will execute through web API asynchronously.</li>
<li>So the javascript engine will get the Promise into the call stack. </li>
<li>And again the web API will execute the Promise asynchronously.</li>
<li>So at this movement, the setTimeout with <strong>0 sec</strong> will complete immediately and move to the <strong>call-back queue</strong>.</li>
<li>Also the Promise with <strong>true</strong> resolve will complete and move to <strong>call-back queue</strong>.</li>
<li>Then at last first the setTimeout callback function will come into the call stack and then Promise's callback function.</li>
<li>So the output will be (I thought) --&gt;</li>
</ul>
<pre><code>- <span class="hljs-built_in">setTimeout</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">called</span>
- <span class="hljs-title">Promise</span> <span class="hljs-title">Resolved</span> <span class="hljs-title">function</span> <span class="hljs-title">called</span></span>
</code></pre><p>But This was not right so first the "<strong>Promise Resolved function called</strong>" was printed then the "<strong>Promise setTimeout function called</strong>".</p>
<p>So where did  I want wrong, So we were correct till the point where the web API was executing the Promise and setTimeout. But in the call-back queues, there are two types of queues first is Task queue (macrostasks) and then the Job queue(microtasks).</p>
<p>And if the Promise is resolved in the web API. The Promise does not go to the Task queue it goes in the Job queue and the setTimeout or other async API after completion will go to the Task queue.</p>
<p>And this is what makes the difference because the event loop which takes the async APIs from the callback queue and put it into the call stack (after the call stack is emptied) has a priority of dequeueing first the Job queue and then the Task queue.</p>
<p>So that is why the output is.</p>
<pre><code>- <span class="hljs-built_in">Promise</span> Resolved <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">called</span>
- <span class="hljs-title">setTimeout</span> <span class="hljs-title">function</span> <span class="hljs-title">called</span></span>
</code></pre><h3 id="heading-summary">Summary</h3>
<p>So the resolved promise is processed faster than an 0-sec setTimeout().
Because of the event loop priorities dequeuing jobs from the job queue which has the promise callback over the task queue which stores the setTimeout() callbacks.</p>
]]></content:encoded></item><item><title><![CDATA[Optimizing API Calls in React with Debounce]]></title><description><![CDATA[As a front-end developer, you may have encountered a situation where you need to make API calls in a React application, but you want to avoid making too many calls at once. This can happen when you have an input field that makes an API call every tim...]]></description><link>https://blog.prathmeshdhatrak.com/multiple-api-calls-in-react</link><guid isPermaLink="true">https://blog.prathmeshdhatrak.com/multiple-api-calls-in-react</guid><category><![CDATA[React]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[js]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Frontend Development]]></category><dc:creator><![CDATA[Prathmesh Santosh Dhatrak]]></dc:creator><pubDate>Wed, 19 Oct 2022 20:04:29 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1671965819412/8357d699-fe25-4366-bfb0-6bfa92ec36df.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As a front-end developer, you may have encountered a situation where you need to make API calls in a React application, but you want to avoid making too many calls at once. This can happen when you have an input field that makes an API call every time the user types something, This can lead to multiple API calls being made in a short period of time, which can slow down the application and increase the load on the server.</p>
<p>One way to solve this problem is to use a technique called debounce. Debounce is a rate-restriction approach that allows a function or method to run just once after a predetermined period of time has passed. It is a higher-order function, i.e., a function that returns another function. It forms a closure around the function parameters.</p>
<p>Debounce is useful in situations where you want to make sure that a function is not called too frequently. For example, if you have an input field that makes an API call every time the user types something, you can use debounce to ensure that the API call is only made once the user has finished typing. This can help to reduce the number of API calls being made and improve the performance of your application.</p>
<p>There are several ways to implement debounce in a React application. One way is to use the lodash.debounce package, which is a JavaScript library that provides utility functions for common programming tasks using the functional programming paradigm. To use lodash.debounce, you can install the package via npm and then import it into your page component.</p>
<pre><code class="lang-javascript">npm install lodash.debounce
</code></pre>
<p>Then, import it into your page component:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> debounce <span class="hljs-keyword">from</span> <span class="hljs-string">'lodash.debounce'</span>;
</code></pre>
<p>You can then use it with an input element by passing a callback function and the desired delay time:</p>
<pre><code class="lang-javascript">&lt;input
  className=<span class="hljs-string">""</span>
  placeholder=<span class="hljs-string">"Search..."</span>
  type=<span class="hljs-string">"text"</span>
  onFocus={onFocusInput}
  onChange={debounce(onSearchInputChange, <span class="hljs-number">200</span>)}
  onKeyDown={onSearchSubmit}
/&gt;
</code></pre>
<p>Another option is to create your own custom debounce function. To do this, you can define a function that takes two arguments: the function that needs to be called after a delay and the duration of the delay. The debounce function can then use setTimeout to call the specified function after the specified delay time has passed. To pass the input from the current target element to the delayed function, you can use the apply method to apply the current value and arguments to the delayed function.</p>
<p>Here's an example of a custom debounce function:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> customDebounceFunction = <span class="hljs-function">(<span class="hljs-params"><span class="hljs-keyword">function</span>, delayTimeInMilliseconds</span>) =&gt;</span> {
  <span class="hljs-keyword">let</span> timer;
  <span class="hljs-keyword">return</span> <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">let</span> self = <span class="hljs-built_in">this</span>;
    <span class="hljs-keyword">let</span> args = <span class="hljs-built_in">arguments</span>;
    <span class="hljs-built_in">clearTimeout</span>(timer);
    timer = <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> {
      <span class="hljs-keyword">function</span>.apply(self, args)
    }, delayTimeInMilliseconds)
  }
}
</code></pre>
<p>To use the custom debounce function in a React component, you can use the useCallback hook to ensure that the function reference does not change. This will prevent the component from re-rendering unnecessarily.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> debounceValue = useCallback(customDebounceFunction(<span class="hljs-function">(<span class="hljs-params">nextValue</span>) =&gt;</span> onSearchInputChange(nextValue), <span class="hljs-number">1000</span>), [])
</code></pre>
<p>By using debounce, you can optimize API calls and improve the performance of your React application. If you're facing a similar issue in your own projects, consider using debounce to handle multiple API calls and reduce the load on your server.</p>
<p>It's worth noting that debounce is just one tool that can help you optimize API calls in a React application. There are other techniques you can use as well, such as caching API results or using a package like Axios to automatically cancel overlapping requests. Experiment with different approaches and see what works best for your specific use case.</p>
<p>So this is how you can handle and restrict multiple API calls in React using debounce. That's it from my side, I tried sharing everything I could. I hope this was helpful!</p>
<p>Let's connect on <a target="_blank" href="https://github.com/Prathmesh-Dhatrak">GitHub</a> or <a target="_blank" href="https://www.linkedin.com/in/prathmesh-dhatrak">LinkedIn</a>.</p>
]]></content:encoded></item><item><title><![CDATA[Understanding React Class-Based Components]]></title><description><![CDATA[As a developer learning React, you may have heard that class-based components are being phased out in favor of functional components and the React Hooks API. While it's true that functional components and Hooks offer several benefits, it's still impo...]]></description><link>https://blog.prathmeshdhatrak.com/understanding-react-class-based-component</link><guid isPermaLink="true">https://blog.prathmeshdhatrak.com/understanding-react-class-based-component</guid><category><![CDATA[React]]></category><category><![CDATA[ReactHooks]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[webdev]]></category><category><![CDATA[Frontend Development]]></category><dc:creator><![CDATA[Prathmesh Santosh Dhatrak]]></dc:creator><pubDate>Tue, 18 Oct 2022 14:36:11 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1671968091720/50ea49f0-2570-4273-8a5f-334f34bd340c.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As a developer learning React, you may have heard that class-based components are being phased out in favor of functional components and the React Hooks API. While it's true that functional components and Hooks offer several benefits, it's still important to understand class-based components and their lifecycle methods.</p>
<p>Class-based components are created by declaring a class that extends the React.Component subclass. The class includes a constructor method for initializing the component and a render method for rendering the component to the UI. Class-based components also have a state object, which allows you to manage changes to the component over time.</p>
<p>To update the state of a class-based component, you can use the setState method and pass in a new value for the state. For example, if you have a Counter component that increments and decrements a count value, you can use setState to update the count in response to user input.</p>
<p>Here's an example of a class-based Counter component:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  <span class="hljs-keyword">constructor</span>(props) {
    <span class="hljs-built_in">super</span>(props);
    <span class="hljs-built_in">this</span>.state = { <span class="hljs-attr">count</span>: <span class="hljs-number">0</span> };
  }

  increment = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-built_in">this</span>.setState({
      <span class="hljs-attr">count</span>: <span class="hljs-built_in">this</span>.state.count + <span class="hljs-number">1</span>,
    });
  }

  decrement = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-built_in">this</span>.setState({
      <span class="hljs-attr">count</span>: <span class="hljs-built_in">Math</span>.max(<span class="hljs-built_in">this</span>.state.count - <span class="hljs-number">1</span>),
    });
  }

  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"counter"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Counter<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{this.state.count}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"buttons"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.increment}</span>&gt;</span>+<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.decrement}</span>&gt;</span>-<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
}
</code></pre>
<p>One of the advantages of class-based components is that they have lifecycle methods that allow you to perform certain actions at specific points in the component's lifecycle. For example, you can use the componentDidMount method to fetch data from an API when the component first renders, or the componentDidUpdate method to update the component when the state changes.</p>
<p>Here's an example of a class-based component that fetches data from an API when it mounts:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DataFetcher</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
    <span class="hljs-keyword">constructor</span>(props) {
        <span class="hljs-built_in">super</span>(props);
        <span class="hljs-built_in">this</span>.state = {<span class="hljs-attr">data</span>: [] };
     }

    componentDidMount() {
        fetch(https:<span class="hljs-comment">//api.example.com/endpoint)</span>
        .then(<span class="hljs-function"><span class="hljs-params">response</span> =&gt;</span> response.json())
        .then(<span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> <span class="hljs-built_in">this</span>.setState({ data }));
    }

    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"data-fetcher"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Data Fetcher<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
                    {this.state.data.map(item =&gt; (
                        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{item.id}</span>&gt;</span>{item.name}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                    ))}
                <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        );
    }
}
</code></pre>
<p>While functional components and Hooks are more popular in modern React development, it's still important to understand class-based components and their lifecycle methods. Here are some situations where class-based components may be the better choice:</p>
<ul>
<li><p>You need to use lifecycle methods: If you need to perform certain actions at specific points in the component's lifecycle, you'll need to use class-based components and their lifecycle methods.</p>
</li>
<li><p>You're working on a legacy codebase: If you're working on an older codebase that uses class-based components, you'll need to understand how they work in order to make updates and improvements.</p>
</li>
<li><p>You want to use higher-order components: Higher-order components are functions that take a component as an argument and return a new component. They're often used to add additional functionality to a component. Class-based components are a good choice for implementing higher-order components because they have access to the component's instance, which allows you to manipulate the component's state and props.</p>
</li>
</ul>
<p>So, is it worth learning class-based components? It depends on your goals and the type of projects you're working on. If you're starting out with React, it's probably more important to focus on learning functional components and Hooks. However, if you're interested in working with legacy codebases or using higher-order components, it's definitely worth learning how class-based components work.</p>
<p>Here are some key differences between functional components and class-based components:</p>
<ul>
<li><p>State: Class-based components have a state object that can be used to store and update data, while functional components do not. Instead, functional components use Hooks to manage state and side effects.</p>
</li>
<li><p>Lifecycle methods: Class-based components have a number of lifecycle methods that can be used to perform actions at specific points in the component's lifecycle. Functional components do not have lifecycle methods, but they can use Hooks to perform similar actions.</p>
</li>
<li><p>Syntax: Class-based components use a class declaration and extend the React.Component class, while functional components are declared with a function.</p>
</li>
</ul>
<p>In summary, class-based components can be a useful tool in certain situations, but in most cases, functional components and Hooks are the preferred choices for modern React development. It's important to understand both approaches and choose the one that best fits your needs.</p>
<p>That's it from my side, I tried sharing everything I could. I hope this was helpful!</p>
<p>let's connect on <a target="_blank" href="https://github.com/Prathmesh-Dhatrak">GitHub</a> or <a target="_blank" href="https://www.linkedin.com/in/prathmesh-dhatrak">LinkedIn</a>.</p>
]]></content:encoded></item><item><title><![CDATA[Understanding JS concepts: Part 2]]></title><description><![CDATA[So this is a series of blogs in which I try to explain different types of Frontend Development concepts.
So In each part, We will cover 5 Js concepts. 
Note:- I am creating these blogs to understand myself and write about these different and importan...]]></description><link>https://blog.prathmeshdhatrak.com/understanding-js-concepts-part-2</link><guid isPermaLink="true">https://blog.prathmeshdhatrak.com/understanding-js-concepts-part-2</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[js]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Frontend Development]]></category><dc:creator><![CDATA[Prathmesh Santosh Dhatrak]]></dc:creator><pubDate>Mon, 17 Oct 2022 10:06:29 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1666001091317/VLcJ96c70.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-so-this-is-a-series-of-blogs-in-which-i-try-to-explain-different-types-of-frontend-development-concepts">So this is a series of blogs in which I try to explain different types of Frontend Development concepts.</h3>
<p>So In each part, We will cover 5 Js concepts. <br />
<strong>Note</strong>:- <em>I am creating these blogs to understand myself and write about these different and important concepts in detail</em>. <br />
So to start we will see some of the basic Js concepts.<br /></p>
<h3 id="heading-1-javascript-data-types">1. JavaScript data types.</h3>
<p>In JavaScript, there are three primary data types, two composite data types, and two special data types.<br /></p>
<ul>
<li><strong>Primary Data Types</strong> -: String, Number, Boolean </li>
<li><strong>Composite Data Types</strong>-: Object, Array </li>
<li><strong>Special Data Types</strong>-: Null, Undefined</li>
</ul>
<h3 id="heading-2-difference-between-null-and-undefined">2. Difference between Null and Undefined.</h3>
<p>All the other Data types can be understood by their name just the difference between Null and Undefined is complicated.</p>
<ul>
<li>So Undefined means the Variable used in the code doesn’t exist or the Variable is not assigned to any value or the Property doesn’t exist.</li>
<li>‘Null’ is a keyword in JavaScript that signifies ‘no value’ or the nonexistence of any value. If you wish to shred a variable of its assigned value, you can simply assign ‘null’ to it.</li>
</ul>
<h3 id="heading-3-types-of-errors-in-javascript">3. Types of errors in JavaScript.</h3>
<p>There are three types of errors:</p>
<ul>
<li><strong>Load time errors</strong>:- Errors that come up when loading a web page Like improper syntax errors are known as Load time errors and it generates the errors dynamically.</li>
<li><strong>Run time errors</strong>:- Errors that come due to misuse of the command inside the HTML language.</li>
<li><strong>Logical Errors</strong>:- These are errors that occur due to the wrong logic performed on a function.</li>
</ul>
<h3 id="heading-4-event-flow-in-javascript">4. Event Flow in JavaScript.</h3>
<p>There are three distinct phases an event goes through:</p>
<ul>
<li>Capture</li>
<li>Target</li>
<li>Bubble</li>
</ul>
<p>Events (such as ‘click’ events, for example) are registered not only by the target element where they originate but by all of that element’s parents all the way up to the global element (i.e. the Window/Browser).
An Event occurs (a ‘click’, for example) and the click event is registered (this means noted and recognized by the browser). <br />
The registered click event then goes from the Window Object down through the child elements and is registered on each during a ‘Capture Phase’ (<code>&lt;html&gt;</code> –&gt; <code>&lt;body&gt;</code> –&gt; <code>&lt;div&gt;</code> –&gt; <code>&lt;form&gt;</code> –&gt; <code>&lt;button&gt;</code>, for example) to find the Target Element (i.e to find what element was clicked).  <br />
Once the click event has registered on the Target (‘Target Phase’), it then bubbles back up the DOM Tree (‘Event Bubbling Phase’) and is registered on all parent elements up through the Window Object triggering any Event Listeners or Handlers in the process.</p>
<h3 id="heading-5-use-strict-in-javascript">5. <code>use strict</code> in JavaScript.</h3>
<p><code>use strict</code> is a way to voluntarily enforce stricter parsing and error handling on your JavaScript code at runtime.
Code errors that would otherwise have been ignored or would have failed silently will now generate errors or throw exceptions. In general, it is a good practice.
Key benefits of strict mode</p>
<ul>
<li>Makes debugging easier</li>
<li>Prevents accidental globals</li>
<li>Prohibits the use of the same property names twice.</li>
<li>Throws error on invalid usage of delete</li>
</ul>
<p>That's it from my side. I hope this was helpful!
let's connect on <a target="_blank" href="https://github.com/Prathmesh-Dhatrak">Github</a> or <a target="_blank" href="https://www.linkedin.com/in/prathmesh-dhatrak">LinkedIn</a>.</p>
]]></content:encoded></item><item><title><![CDATA[Understanding JS concepts: Part 1]]></title><description><![CDATA[So this is a series of blogs I'm starting regarding Frontend Development concepts.
In each part, I will try to explain 5 Js concepts or common javascript questions.
I am creating these blogs to understand and write about the concepts in detail. 
So t...]]></description><link>https://blog.prathmeshdhatrak.com/understanding-js-concepts-part-1</link><guid isPermaLink="true">https://blog.prathmeshdhatrak.com/understanding-js-concepts-part-1</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[HTML5]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Frontend Development]]></category><category><![CDATA[ES6]]></category><dc:creator><![CDATA[Prathmesh Santosh Dhatrak]]></dc:creator><pubDate>Sun, 16 Oct 2022 15:42:46 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1665934250352/bvk5hhzUd.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-so-this-is-a-series-of-blogs-im-starting-regarding-frontend-development-concepts">So this is a series of blogs I'm starting regarding Frontend Development concepts.</h3>
<p>In each part, I will try to explain 5 Js concepts or common javascript questions.
I am creating these blogs to understand and write about the concepts in detail. <br />
So to start we will see some of the basic Js concepts.
<br /></p>
<h3 id="heading-1-javascript">1. Javascript<br /></h3>
<p>JavaScript is a high-level programming language that is dynamic, untyped, and interpreted. 
Along with HTML and CSS, it is one of the three essential technologies for developing Web applications;<br /> 
It is used by the majority of websites and is supported by all modern web browsers without the use of plug-ins or other extensions. <br />
There are three ways to add JavaScript commands to your Web Pages: </p>
<ul>
<li>Embedding code</li>
<li>Inline code</li>
<li>External file</li>
</ul>
<h3 id="heading-2-javascript-objects">2. Javascript Objects <br /></h3>
<p>Object-oriented programming is supported by JavaScript. Objects are used to arrange variables, Web pages, forms, text boxes, images, and buttons are all treated as objects on the screen.<br />
Every object has its own properties and methods. Properties define the characteristics of an object. Examples: color, length, name, height, width. 
<br /> Methods are the actions that the object can perform or that can be performed on the object. Examples: alert, confirm, write, open, close.
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1665931255150/IhHjVuSdZ.png" alt="image.png" class="image--center mx-auto" /></p>
<h3 id="heading-3-javascript-events">3. Javascript Events <br /></h3>
<p>The objects on a Web page are organized in a hierarchy. All objects have properties and methods. In addition, some objects also have "events". Events are things that happen, usually user actions, that are associated with an object. The "event handler" is a command that is used to specify actions in response to an event.
<br />Common events are:</p>
<ul>
<li>onLoad - occurs when a page loads in a browser</li>
<li>onUnload - occurs just before the user exits a page</li>
<li>onMouseOver - occurs when you point to an object</li>
<li>onSubmit - occurs when you submit a form</li>
<li>onClick - occurs when an object is clicked <br /></li>
</ul>
<p>JavaScript interacts with HTML via events that occur when the user or browser manipulates a page. An event occurs when the page loads. When a user clicks a button, that is also an event.<br /> Other events include pressing any key, closing a window, resizing a window, and so on.<br />
Developers can use these events to execute JavaScript-coded responses, such as closing buttons, displaying messages to users, validating data, and virtually any other type of response imaginable.<br /> The Document Object Model (DOM) Level 3 includes events, and each HTML element contains a set of events that can be triggered by JavaScript code.</p>
<h3 id="heading-4-javascript-programming-paradigms">4. Javascript  programming paradigms <br /></h3>
<p>JavaScript is a multi-paradigm language, supporting procedural programming along with Object-Oriented Programming and functional programming. <br />
Functional programming produces programs by composing mathematical functions and avoids shared state &amp; mutable data. <br />
Functional programming is an essential concept in JavaScript (one of the two pillars of JavaScript). Several common functional utilities were added to JavaScript in ES5. JavaScript supports OOP (second pillar) with prototypal inheritance.</p>
<h3 id="heading-5-diff-between-classical-inheritance-and-prototypal-inheritance">5. Diff between classical inheritance and prototypal inheritance <br /></h3>
<p>Unlike most other languages, JavaScript’s object system is based on prototypes, not classes. 
<br />
<strong>Class Inheritance</strong>: instances inherit from classes (like a blueprint - a description of the class), and create sub-class relationships hierarchy. Instances are typically instantiated via constructor functions with the <code>new</code> keyword. Class inheritance may or may not use the class keyword from ES6. <br />
<strong>Prototypal Inheritance</strong>: instances inherit directly from other objects. Instances are typically instantiated via factory functions or <code>Object.create()</code>. Instances may be composed of many different objects, allowing for easy selective inheritance.</p>
<p>That's it from my side. I hope this was helpful!
let's connect on <a target="_blank" href="https://github.com/Prathmesh-Dhatrak">Github</a> or <a target="_blank" href="https://www.linkedin.com/in/prathmesh-dhatrak">LinkedIn</a>.</p>
]]></content:encoded></item><item><title><![CDATA[Getting Started with TypeScript]]></title><description><![CDATA[What is TypeScript?
Microsoft developed the well-known open-source language known as TypeScript. A superset of JavaScript gives the language type safety and static typing. Because TypeScript compiles to JavaScript, it can be used in any JavaScript-en...]]></description><link>https://blog.prathmeshdhatrak.com/getting-started-with-typescript</link><guid isPermaLink="true">https://blog.prathmeshdhatrak.com/getting-started-with-typescript</guid><category><![CDATA[TypeScript]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[React]]></category><category><![CDATA[Frontend Development]]></category><category><![CDATA[Web Development]]></category><dc:creator><![CDATA[Prathmesh Santosh Dhatrak]]></dc:creator><pubDate>Sun, 25 Sep 2022 07:26:33 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1664100766580/Ufe2T06k-.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-what-is-typescript">What is TypeScript?</h2>
<p>Microsoft developed the well-known open-source language known as TypeScript. A superset of JavaScript gives the language type safety and static typing. Because TypeScript compiles to JavaScript, it can be used in any JavaScript-enabled environment, including the browser, Node and Deno.</p>
<p>Due to the fact that JavaScript is an interpreted language, occasionally you may have errors in your code that are not apparent until you run it. But because TypeScript is a statically typed language, errors can be caught at compile time rather than during runtime. This means that TypeScript can be used to create safer and simpler-to-debug code.</p>
<h2 id="heading-why-typescript">Why TypeScript?</h2>
<p>The aforementioned advantages make TypeScript a well-liked language for usage in contemporary development. These are a few of these advantages:</p>
<ul>
<li><strong>Compile-time errors</strong>: You can save time and effort by using TypeScript, which can detect mistakes at build time rather than at runtime.</li>
<li><strong>Strong typing</strong>: Whenever you pass the function a type that isn't compatible with it, TypeScript will give you an error because it is statically typed.</li>
<li><strong>Object-oriented improvements</strong>: JavaScript lacks native object-oriented features, whereas TypeScript provides them.</li>
<li><strong>Tooling and ecosystem</strong>: Excellent tooling and support for TypeScript improve the developer experience.</li>
<li><strong>Organization</strong>: Because of namespaces, modules, and better OOP, TypeScript is a fantastic language for arranging your code.</li>
</ul>
<h2 id="heading-installing-typescript">Installing TypeScript</h2>
<p>To install TypeScript, you first need to install Node and NPM. If you need help with this, you can follow our <a target="_blank" href="https://docs.npmjs.com/downloading-and-installing-node-js-and-npm">how-to install Node guide</a>.</p>
<p>Once you have Node and NPM installed, you can install TypeScript with the following command:</p>
<pre><code>npm install -g typescript
</code></pre><p>Alternatively, you can use Yarn to install TypeScript:</p>
<pre><code>yarn <span class="hljs-built_in">global</span> add typescript
</code></pre><p>We want to install it globally, so we can use it in any app we create.</p>
<p>Check to see if TypeScript is installed by running the following command:</p>
<pre><code>tsc -v
</code></pre><p>If you see a version number in your terminal, you've installed TypeScript successfully.</p>
<h2 id="heading-using-typescript">Using TypeScript</h2>
<p>We can use TypeScript now that it has been installed. Make an app.ts file in your root directory. We can begin writing some TypeScript in this section.
Let's keep it simple and write a function that takes a number and returns the square of that number:</p>
<pre><code><span class="hljs-keyword">const</span> square = (x: number): <span class="hljs-function"><span class="hljs-params">number</span> =&gt;</span> x * x;
</code></pre><p>To test that it is working, let's call the function and print the result:</p>
<pre><code><span class="hljs-keyword">const</span> square = (x: number): <span class="hljs-function"><span class="hljs-params">number</span> =&gt;</span> x * x;
<span class="hljs-keyword">const</span> result = square(<span class="hljs-number">5</span>);
<span class="hljs-built_in">console</span>.log(result);
</code></pre><p>Great, now that we've written our first TypeScript code, we can move on to compiling it.</p>
<h2 id="heading-compiling-typescript-into-javascript">Compiling TypeScript into JavaScript</h2>
<p>To compile our TypeScript code, we can use the TypeScript compiler. To do so, use the tsc command and pass it the name of the file we want to compile.</p>
<pre><code>tsc app.ts
</code></pre><p>After the file is compiled, you should see an app.js file in the root directory.</p>
<pre><code><span class="hljs-keyword">var</span> square = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">x</span>) </span>{ <span class="hljs-keyword">return</span> x * x; };
<span class="hljs-keyword">var</span> result = square(<span class="hljs-number">5</span>);
<span class="hljs-built_in">console</span>.log(result);
</code></pre><p>Since app.js is just a regular JavaScript file, we can run it in the browser or Node.</p>
<p>Let's run it in Node: </p>
<pre><code>node app.js
</code></pre><p>You should see the output: </p>
<pre><code><span class="hljs-number">25</span>
</code></pre><p>The TypeScript compiler has successfully taken our TypeScript in app.ts and compiled it into valid JavaScript in our app.js.</p>
<h2 id="heading-typescript-type-safety">TypeScript Type Safety</h2>
<p>As mentioned earlier, TypeScript is a strictly-typed language, which means that it can catch errors at compile time, rather than at runtime.</p>
<p>Let's look at our function one more time:</p>
<pre><code><span class="hljs-keyword">const</span> square = (x: number): <span class="hljs-function"><span class="hljs-params">number</span> =&gt;</span> x * x;
</code></pre><p>Notice how we have to specify the type of the parameter x. This is because we are using TypeScript, and TypeScript requires that we specify the type of the parameter.</p>
<p>This is how TypeScript can help us catch errors at compile time.</p>
<p>We have also typed the return value of the function to be a number as well. This helps the caller know that the function will return a number, instead of a string, or something else. In this way, TypeScript code is somewhat self-documenting.</p>
<p>Watch what happens if we try to accidentally pass a string instead of a number:</p>
<pre><code><span class="hljs-keyword">const</span> square = (x: number): <span class="hljs-function"><span class="hljs-params">number</span> =&gt;</span> x * x;
<span class="hljs-keyword">const</span> result = square(<span class="hljs-string">"5"</span>);
<span class="hljs-built_in">console</span>.log(result);
</code></pre><p>If you try to compile this TypeScript code, the compiler will throw an error:</p>
<pre><code>app.ts:<span class="hljs-number">3</span>:<span class="hljs-number">23</span> - error TS2345: Argument <span class="hljs-keyword">of</span> type <span class="hljs-string">'string'</span> is not assignable to parameter <span class="hljs-keyword">of</span> type <span class="hljs-string">'number'</span>.

<span class="hljs-number">3</span> <span class="hljs-keyword">const</span> result = square(<span class="hljs-string">"5"</span>);
                        ~~~
Found <span class="hljs-number">1</span> error.
</code></pre><p>The TypeScript compiler will throw an error, letting you know that the parameter x is of type number and that the argument "5" is not of type number.</p>
<p>Because TypeScript caught the issue at compile time, we can fix the issue by changing the input from a square to a number, saving us from a potential runtime error.</p>
<p>That's it from my side. I hope this was helpful!
let's connect on <a target="_blank" href="https://github.com/Prathmesh-Dhatrak">Github</a> or <a target="_blank" href="https://www.linkedin.com/in/prathmesh-dhatrak">LinkedIn</a>.</p>
]]></content:encoded></item><item><title><![CDATA[What We Can Do To Lazy Load React Components?]]></title><description><![CDATA[What is lazy loading?
Your website should render pages as rapidly as feasible when a visitor visits. Only the topmost part of the page would be visible to the user. Imagine that the page's lower portion contains numerous images. The user will acquire...]]></description><link>https://blog.prathmeshdhatrak.com/lazyload-react-components</link><guid isPermaLink="true">https://blog.prathmeshdhatrak.com/lazyload-react-components</guid><category><![CDATA[React]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Frontend Development]]></category><dc:creator><![CDATA[Prathmesh Santosh Dhatrak]]></dc:creator><pubDate>Sun, 25 Sep 2022 06:24:56 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1664087114595/u3N5XoTYp.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-what-is-lazy-loading">What is lazy loading?</h2>
<p>Your website should render pages as rapidly as feasible when a visitor visits. Only the topmost part of the page would be visible to the user. Imagine that the page's lower portion contains numerous images. The user will acquire unnecessary data and the website will appear to be of becoming slow if you download all the pictures on the first page.</p>
<p>Instead, it's indeed beneficial to delay image loading until the user sees or is about to see them. This will significantly improve performance, user experience, and even SEO.</p>
<p>So, how can we straightforwardly achieve lazy loading? Several packages address this problem. The <strong><a target="_blank" href="https://www.npmjs.com/package/react-lazyload">react-lazyload</a></strong> package is among my absolute favorites and is probably the most widely used. I prefer to use a third-party package for this because it is fast, performant, and simple to use.</p>
<h2 id="heading-using-react-lazyload">Using react-lazyload</h2>
<p><strong><a target="_blank" href="https://www.npmjs.com/package/react-lazyload">react-lazyload</a></strong>  is an open-sourced and free package that controls the lazy loading of React components, images, and other resources. It has over 100k weekly downloads and simplifies lazy loading.</p>
<p>To install the package run</p>
<p><code>yarn add react-lazyload</code>
Then import the component into your application</p>
<p><code>import LazyLoad from 'react-lazyload'</code></p>
<p>Then wrap the component you want to lazy load with<code>&lt;LazyLoad&gt;</code></p>
<pre><code>&lt;LazyLoad&gt;
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Component</span> /&gt;</span></span>
&lt;/LazyLoad&gt;
</code></pre><p>That’s it. This will load the <code>&lt;Component /&gt;</code> first when it is visible in the viewport.</p>
<h2 id="heading-specify-the-lazyload-components-properties">Specify the LazyLoad component's properties.</h2>
<p>The components themselves will default remain hidden till the LazyLoad component loads itself. You have several alternatives depending on the type of component you want to lazy load.</p>
<p>To prevent layout changes whenever the component loads, you can provide a height for images. You may accomplish this using the property height. Set the height to a specific amount of pixels and the component will take up that much space before being loaded.</p>
<pre><code>&lt;LazyLoad height={<span class="hljs-number">300</span>}&gt;
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Component</span> /&gt;</span></span>
&lt;/LazyLoad&gt;
</code></pre><p>Use the offset property to render the component immediately before the user actually sees that portion of the page. Thus, you can specify that a component should be loaded when a user is x pixels away from seeing it.</p>
<pre><code>&lt;LazyLoad offset={<span class="hljs-number">100</span>} height={<span class="hljs-number">300</span>}&gt;
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Component</span> /&gt;</span></span>
&lt;/LazyLoad&gt;
</code></pre><p>So when the component is 100 pixels away from the viewport, it is loaded. Since the user won't detect the actual loading and can keep scrolling unobserved, this improves the user experience.</p>
<p>Other attributes, including style, resize events, and placeholders, can be set. To learn more, visit the official documents.</p>
<p>Happy loading, lazy in React!</p>
<p>That's it from my side. I hope this was helpful!
let's connect on <a target="_blank" href="https://github.com/Prathmesh-Dhatrak">Github</a> or <a target="_blank" href="https://www.linkedin.com/in/prathmesh-dhatrak">LinkedIn</a>.</p>
]]></content:encoded></item><item><title><![CDATA[An Introduction to Webpack and Other Module Bundlers in React]]></title><description><![CDATA[As a beginner in the ReactJS framework, you may have started with Create React App (CRA) and never encountered the need for a module bundler like webpack. But as you dive deeper into building web applications with React, you'll soon realize that buil...]]></description><link>https://blog.prathmeshdhatrak.com/understanding-webpack</link><guid isPermaLink="true">https://blog.prathmeshdhatrak.com/understanding-webpack</guid><category><![CDATA[webpack]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[React]]></category><dc:creator><![CDATA[Prathmesh Santosh Dhatrak]]></dc:creator><pubDate>Tue, 17 May 2022 16:16:10 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1652804033307/JWq9E7n6g.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As a beginner in the ReactJS framework, you may have started with Create React App (CRA) and never encountered the need for a module bundler like webpack. But as you dive deeper into building web applications with React, you'll soon realize that building a website with just HTML, CSS, and JavaScript is not enough.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1652801588345/H2YdRXHnH.webp" alt="1636485841-bundle-up.webp" class="image--center mx-auto" /></p>
<p>You might want to use TypeScript instead of JavaScript, or a UI library like React instead of plain HTML. You'll also probably want to use a CSS preprocessor like SCSS or SASS. This is where module bundlers like webpack, rollup, parcel, and snowpack come in handy.</p>
<p>But what exactly is a module bundler, and why do you need one?</p>
<h2 id="heading-what-is-a-module-bundler"><strong>What is a Module Bundler?</strong></h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1652801252199/0Pr7LnpPk.png" alt="Blog2-Moduler-bundlers.png" /></p>
<p>A module bundler is a tool that takes pieces of JavaScript and their dependencies and bundles them together into a single file. This file contains both your own code and the required libraries needed to run your code in the browser.</p>
<h2 id="heading-why-do-i-need-one">Why do I need one?</h2>
<p>Module bundlers are important because they make it easier to manage complex projects that involve multiple JavaScript files and dependencies. Without a module bundler, you would have to add separate <code>&lt;script&gt;</code> tags for each file in your HTML, which can be a hassle. There are also limitations on the number of concurrent AJAX requests, so fetching and loading all the modules individually can take a significant amount of time.</p>
<p>Module bundlers also help you manage the correct sequential order of dependencies in your HTML. For example, if module A depends on module B, you'll want to make sure that module B is loaded before module A. Without a module bundler, you would have to manually ensure that the <code>&lt;script&gt;</code> tags are in the correct order.</p>
<p>Finally, module bundlers help you avoid issues with global declarations of functions and variables. Without a module bundler, there is a risk of name collision and overridden variables, which can lead to unpredictable behavior in your code.</p>
<p>Here's an example of what your HTML might look like without a module bundler:</p>
<pre><code class="lang-xml"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">HTML</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Module Bundlers<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"js/utils/add.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"js/utils/subtract.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"js/utils/multiply.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"js/utils/divide.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"js/utils/maths.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"js/main.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>Each of these files requires a separate HTTP request, so it would take five round-trip requests just to get your application started. With a module bundler, you can combine all these files into a single file, reducing the number of requests and speeding up the loading time of your application.</p>
<h2 id="heading-how-to-use-a-module-bundler-like-webpack"><strong>How to Use a Module Bundler like Webpack</strong>.</h2>
<p>To get started with webpack, you'll need to install it and the webpack command-line interface (CLI). You can do this with the following command:</p>
<pre><code class="lang-javascript">npm install -D webpack webpack-cli
</code></pre>
<p>To use webpack, you'll need to create a <code>webpack.config.js</code> file with the following options:</p>
<ul>
<li><p><code>devtool</code>: Enables source map generation in development mode.</p>
</li>
<li><p><code>entry</code>: The main file of your React application.</p>
</li>
<li><p><code>output.path</code>: The root directory to store output files in.</p>
</li>
<li><p><code>output.filename</code>: The filename pattern to use for generated files.</p>
</li>
<li><p><code>output.publicPath</code>: The path to the root directory where the files will be deployed on the web server.</p>
</li>
</ul>
<p>Here is an example of what your <code>webpack.config.js</code> the file might look like this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">"path"</span>);

<span class="hljs-built_in">module</span>.exports = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">_env, argv</span>) </span>{
  <span class="hljs-keyword">const</span> isProduction = argv.mode === <span class="hljs-string">"production"</span>;
  <span class="hljs-keyword">const</span> isDevelopment = !isProduction;

  <span class="hljs-keyword">return</span> {
    <span class="hljs-attr">devtool</span>: isDevelopment &amp;&amp; <span class="hljs-string">"cheap-module-source-map"</span>,
    <span class="hljs-attr">entry</span>: <span class="hljs-string">"./src/index.js"</span>,
    <span class="hljs-attr">output</span>: {
      <span class="hljs-attr">path</span>: path.resolve(__dirname, <span class="hljs-string">"dist"</span>),
      <span class="hljs-attr">filename</span>: <span class="hljs-string">"assets/js/[name].[contenthash:8].js"</span>,
      <span class="hljs-attr">publicPath</span>: <span class="hljs-string">"/"</span>
    }
  };
};
</code></pre>
<p>This configuration works fine for plain JavaScript files, but when using webpack with React, you'll often need to perform additional transformations before shipping your code to users. This is where loaders and plugins come in handy.</p>
<p>Loaders are transformations that are applied to your code before it is bundled by webpack. They allow you to import and use non-JavaScript files, such as TypeScript or SCSS, in your JavaScript code. For example, the following configuration uses the <code>ts-loader</code> and <code>sass-loader</code> to transpile TypeScript and SCSS files:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">module</span>.exports = {
  <span class="hljs-comment">// ...</span>
  <span class="hljs-attr">module</span>: {
    <span class="hljs-attr">rules</span>: [
      {
        <span class="hljs-attr">test</span>: <span class="hljs-regexp">/\.tsx?$/</span>,
        use: <span class="hljs-string">"ts-loader"</span>
      },
      {
        <span class="hljs-attr">test</span>: <span class="hljs-regexp">/\.scss$/</span>,
        use: [<span class="hljs-string">"style-loader"</span>, <span class="hljs-string">"css-loader"</span>, <span class="hljs-string">"sass-loader"</span>]
      }
    ]
  }
};
</code></pre>
<p>Plugins are extensions that allow you to perform a wide range of tasks, such as generating an HTML file or optimizing your code for production. For example, the <code>HtmlWebpackPlugin</code> generates an HTML file that includes a <code>&lt;script&gt;</code> tag for your bundled JavaScript:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> HtmlWebpackPlugin = <span class="hljs-built_in">require</span>(<span class="hljs-string">"html-webpack-plugin"</span>);

<span class="hljs-built_in">module</span>.exports = {
  <span class="hljs-comment">// ...</span>
  <span class="hljs-attr">plugins</span>: [
    <span class="hljs-keyword">new</span> HtmlWebpackPlugin({
      <span class="hljs-attr">template</span>: <span class="hljs-string">"src/index.html"</span>
    })
  ]
};
</code></pre>
<p>With these configurations in place, you can run a webpack to build your application. You can use the following command to build in development mode:</p>
<pre><code class="lang-javascript">webpack --mode=development
</code></pre>
<p>And the following command to build in production mode:</p>
<pre><code class="lang-javascript">webpack --mode=production
</code></pre>
<p>In development mode, webpack will generate source maps and perform transformations in an unminified manner. In production mode, webpack will minify your code and perform other optimizations to reduce the size and improve the performance of your application.</p>
<h2 id="heading-module-bundlers-vs-create-react-app"><strong>Module Bundlers vs. Create React App</strong></h2>
<p>Now that you have a basic understanding of module bundlers, you might be wondering how they compare to Create React App (CRA).</p>
<p>CRA is a tool that generates a template for a new React application with webpack and other dependencies already set up for you. It allows you to get started with a new React project quickly without having to worry about configuring webpack yourself.</p>
<p>However, one of the downsides of CRA is that it hides the webpack configuration from you. This can be a disadvantage if you want to customize the configuration or add additional plugins and loaders. In these cases, you'll have to eject the configuration from CRA, which can be a complex process.</p>
<p>On the other hand, using a module bundler like webpack gives you full control over the configuration of your build process. This can be a powerful advantage if you have specific requirements or need to optimize your build process for performance.</p>
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p>Module bundlers like webpack are important tools for building complex React applications. They allow you to manage multiple JavaScript files and dependencies and perform transformations on your code before it is bundled for the browser. While Create React App is a convenient option for getting started with a new React project, using a module bundler gives you more control over the build process and can be a powerful advantage for more advanced projects.  </p>
<p>That's it from my side. I hope this was helpful! let's connect on <a target="_blank" href="https://github.com/Prathmesh-Dhatrak">GitHub</a> or <a target="_blank" href="https://www.linkedin.com/in/prathmesh-dhatrak">LinkedIn</a>.</p>
]]></content:encoded></item><item><title><![CDATA[Setting Up React App Without Create-React-App (CRA)]]></title><description><![CDATA[Hello friends! Today I am going to share how to set up React project without using Create-React-App (CRA). Many developers directly jump to CRA for new projects, but sometimes we need more control on our build process, right?
I was working on a proje...]]></description><link>https://blog.prathmeshdhatrak.com/how-to-setup-react-without-cra</link><guid isPermaLink="true">https://blog.prathmeshdhatrak.com/how-to-setup-react-without-cra</guid><category><![CDATA[React]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Prathmesh Santosh Dhatrak]]></dc:creator><pubDate>Tue, 17 May 2022 10:43:51 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1743423452777/3eeeb354-c356-4665-9041-b70bae1cd263.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hello friends! Today I am going to share how to set up React project without using Create-React-App (CRA). Many developers directly jump to CRA for new projects, but sometimes we need more control on our build process, right?</p>
<p>I was working on a project last week where I needed custom webpack configuration. CRA was making things complicated with "eject" and all that headache. So I thought why not set up everything from scratch and have full control?</p>
<p>Let me share my approach which is working perfectly for our team now.</p>
<h2 id="heading-why-not-use-cra">Why Not Use CRA?</h2>
<p>CRA is good for beginners, no doubt. But when you work in production:</p>
<ul>
<li>It installs too many unnecessary packages </li>
<li>Customization requires ejecting which is irreversible</li>
<li>Build size becomes larger than needed</li>
<li>You don't learn what's happening behind the scenes</li>
</ul>
<h2 id="heading-steps-to-set-up-react-without-cra">Steps to Set Up React Without CRA</h2>
<h3 id="heading-1-initialize-your-project">1. Initialize Your Project</h3>
<p>First thing first, initialize your npm project:</p>
<pre><code class="lang-bash">npm init
</code></pre>
<p>Fill the details or simply use <code>npm init -y</code> if you are in hurry like me always!</p>
<h3 id="heading-2-install-required-packages">2. Install Required Packages</h3>
<p>Now we need to install required dependencies:</p>
<pre><code class="lang-bash">npm install --save-dev @babel/core @babel/preset-env @babel/preset-react webpack webpack-cli webpack-dev-server babel-loader css-loader style-loader html-webpack-plugin
</code></pre>
<p>What are these packages for?</p>
<ul>
<li>@babel/core: Main babel package to transpile ES6+ to browser compatible JavaScript</li>
<li>@babel/preset-env: Preset for converting modern JavaScript</li>
<li>@babel/preset-react: For JSX support (most important for React!)</li>
<li>babel-loader: Connects babel with webpack</li>
<li>webpack: Module bundler (the heart of our setup)</li>
<li>webpack-cli: Command line interface for webpack</li>
<li>webpack-dev-server: Development server with hot reload</li>
<li>html-webpack-plugin: Creates HTML files to serve webpack bundles</li>
<li>css-loader: Interprets @import and url() like import/require()</li>
<li>style-loader: Injects CSS into DOM</li>
</ul>
<p>Also, don't forget to install React itself:</p>
<pre><code class="lang-bash">npm install react react-dom
</code></pre>
<h3 id="heading-3-create-basic-files">3. Create Basic Files</h3>
<p>We need minimum 3 files to start with:</p>
<p><strong>index.html</strong></p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>My React App<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"root"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p><strong>App.js</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Hello from React without CRA!<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This setup is much faster and lighter.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<p><strong>index.js</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">'react-dom'</span>;
<span class="hljs-keyword">import</span> App <span class="hljs-keyword">from</span> <span class="hljs-string">'./App'</span>;

ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span></span>, <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>));
</code></pre>
<h3 id="heading-4-configure-webpack">4. Configure Webpack</h3>
<p>Create webpack.config.js in root:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">"path"</span>);
<span class="hljs-keyword">const</span> HtmlWebpackPlugin = <span class="hljs-built_in">require</span>(<span class="hljs-string">"html-webpack-plugin"</span>);

<span class="hljs-built_in">module</span>.exports = {
  <span class="hljs-attr">entry</span>: <span class="hljs-string">"./index.js"</span>,  <span class="hljs-comment">// Note: fixed the entry point path</span>
  <span class="hljs-attr">output</span>: {
    <span class="hljs-attr">path</span>: path.resolve(__dirname, <span class="hljs-string">"dist"</span>),
    <span class="hljs-attr">filename</span>: <span class="hljs-string">"index_bundle.js"</span>,
  },
  <span class="hljs-attr">module</span>: {
    <span class="hljs-attr">rules</span>: [
      {
        <span class="hljs-attr">test</span>: <span class="hljs-regexp">/\.(js|jsx)$/</span>,  <span class="hljs-comment">// Added jsx support</span>
        exclude: <span class="hljs-regexp">/node_modules/</span>,  <span class="hljs-comment">// Important to exclude node_modules!</span>
        use: <span class="hljs-string">"babel-loader"</span>,
      },
      {
        <span class="hljs-attr">test</span>: <span class="hljs-regexp">/\.css$/</span>,
        use: [<span class="hljs-string">"style-loader"</span>, <span class="hljs-string">"css-loader"</span>],
      },
    ],
  },
  <span class="hljs-attr">resolve</span>: {
    <span class="hljs-attr">extensions</span>: [<span class="hljs-string">'.js'</span>, <span class="hljs-string">'.jsx'</span>],  <span class="hljs-comment">// Added to resolve imports without extensions</span>
  },
  <span class="hljs-attr">mode</span>: <span class="hljs-string">"development"</span>,
  <span class="hljs-attr">plugins</span>: [
    <span class="hljs-keyword">new</span> HtmlWebpackPlugin({
      <span class="hljs-attr">template</span>: <span class="hljs-string">"index.html"</span>,
    }),
  ],
};
</code></pre>
<h3 id="heading-5-create-babel-configuration">5. Create Babel Configuration</h3>
<p>Create .babelrc file in project root:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"presets"</span>: [<span class="hljs-string">"@babel/preset-env"</span>, <span class="hljs-string">"@babel/preset-react"</span>]
}
</code></pre>
<h3 id="heading-6-update-packagejson-scripts">6. Update package.json Scripts</h3>
<p>Add these scripts to your package.json:</p>
<pre><code class="lang-json"><span class="hljs-string">"scripts"</span>: {
  <span class="hljs-attr">"start"</span>: <span class="hljs-string">"webpack serve --open"</span>,
  <span class="hljs-attr">"build"</span>: <span class="hljs-string">"webpack --mode production"</span>
}
</code></pre>
<h2 id="heading-running-your-app">Running Your App</h2>
<p>Now simply run:</p>
<pre><code class="lang-bash">npm start
</code></pre>
<p>Webpack dev server will start and open your app in browser. For production build:</p>
<pre><code class="lang-bash">npm run build
</code></pre>
<p>This will create optimized files in dist folder ready for deployment.</p>
<h2 id="heading-troubleshooting-common-issues">Troubleshooting Common Issues</h2>
<p>If you face "Module not found" error, check your file paths. I wasted 2 hours last time because I wrote <code>./src/index.js</code> but my file was in root folder!</p>
<p>Sometimes babel config doesn't pick up. Try creating babel.config.js instead of .babelrc.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Setting up React without CRA gives you full control and better understanding of your project structure. It's not that complicated once you get familiar with webpack basics.</p>
<p>Hope this helps someone! If you have any questions, drop a comment.</p>
<p>Happy coding!</p>
]]></content:encoded></item><item><title><![CDATA[Connect React JS to Firebase]]></title><description><![CDATA[What is Firebase?
Firebase is a BaaS (backend as assistance) platform developed by Google for making web and mobile applications. It provides developers with a number of tools and services to help create quality applications and scale over a long tim...]]></description><link>https://blog.prathmeshdhatrak.com/connect-react-js-to-firebase</link><guid isPermaLink="true">https://blog.prathmeshdhatrak.com/connect-react-js-to-firebase</guid><dc:creator><![CDATA[Prathmesh Santosh Dhatrak]]></dc:creator><pubDate>Fri, 01 Apr 2022 06:19:55 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1650866832012/f1zYVF-Dn.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>What is Firebase?</p>
<p>Firebase is a BaaS (backend as assistance) platform developed by Google for making web and mobile applications. It provides developers with a number of tools and services to help create quality applications and scale over a long time as their user base increments.</p>
<p>A few outstanding features Firebase gives:</p>
<p>•    Authentication/Validation — Firebase Authentication can be used to manually integrate one or more sign-in methods into an app.
•    Realtime database — data is synced across all clients in real-time and remains available even when an app goes offline.
•    Hosting - Firebase gives quick hosting to a web application.
•    Analytics — allows you to track and prioritize issues with your app, as well as monitor the success of your app
•    It offers Free and Paid options</p>
<p>Setting up your Firebase account</p>
<p>To start the Firebase account setting, you'll need to have a Google account. If you don't have one or need to make one for Firebase you can go to the Google Gmail Home page and make an account for free.</p>
<p>Once you’ve made an account, go to Firebase and click the ‘Get started’ button.
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1650866832012/f1zYVF-Dn.png" alt="1.png" /></p>
<p>Continue to click ‘Create a project’.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1650866846843/Qg3gnC3mg.png" alt="2.png" /></p>
<p>Now enter your project name &amp; click on the check box. Then click continue </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1650866865832/lFYv8ziNh.png" alt="3.png" /></p>
<p>Again just click on continue</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1650866879381/OKd_YWk3q.png" alt="4.png" /></p>
<p>If you want to analytics then, allow the analytics, and then click ‘create project’.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1650866898678/01NLa5p7o.png" alt="5.png" /></p>
<p>Now Select Your Google Analytics Account.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1650866906617/S86qoHB5r.png" alt="6.png" /></p>
<p>Firebase will then create the project and then click on continue to direct you to the project’s overview.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1650866915110/nfZYc6ClO.png" alt="7.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1650867184247/IFTDyidp-.png" alt="8.png" />
Depending on the device your application is for, select the right icon (iOS, Android, Web). We'll associate our Firebase undertaking to a web application in this blog so go ahead and select the web icon.
You'll be prompted to register your application.</p>
<p>Once registered, Firebase will create your Firebase SDK script that you will use to connect your react-app to the project.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1650867209088/tQmc3IK34.png" alt="9.png" /></p>
<p>Click 'continue to console' and you will be directed back to the overview page.</p>
<p>•    To get to this code whenever simply click on the settings dial and select 'project settings'</p>
<p>Select your database
From your Firebase console, select the 'Build' column on the right-side panel and various sub-headings will show underneath it - you'll want to take a look at Firestore Database and the Realtime Database.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1650867254991/RIanbPMAr.png" alt="10.png" /></p>
<p>The difference between the two described by Firebase is:
•    Cloud Firestore is Firebase's newest database for mobile application improvement. It expands on the achievements of the Realtime Database with a new, more intuitive data model. Cloud Firestore additionally includes more features, faster queries, and scales farther than the Realtime Database.
•    Realtime Database is Firebase's original database. It's an effective, low-latency solution for mobile applications that require synced states across clients in real-time.</p>
<p>For this blog, we’ll choose Firestore as our database.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1650867303622/pfRNgcFDC.png" alt="11.png" /></p>
<p>Go ahead and click ‘Create database’.
You’ll then be prompted to start in Production or Test mode.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1650867320098/fC4hrPIb9.png" alt="12.png" /></p>
<p>Then, at that point, choose an area where your information will be stored. Select the best area for your requirements and click 'Enable'.</p>
<p><img src="Upload failed. Please re-upload the image" alt="13.png" /></p>
<p>Once done, you’ll be redirected to your Firestore database.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1650867373155/85PSzD0DK.png" alt="14.png" /></p>
<p>Fantastic. We now have a project registered with Firebase and a Firestore database associated with the project. You can manually add information to your database through this control center and follows a Collection to Document model.</p>
<p>Now let's connect it to our react-app.</p>
<p>Linking Firebase project to our react-app with Firebase SDK</p>
<p>If you haven’t created a react-app yet, go ahead and enter this line in your terminal in the folder where you’d like to store your app.</p>
<p>npx create-react-app My First Project</p>
<p>Once create-react-app has done its thing, make a new folder titled ‘utils’ within your components folder. Within the ‘utils’ folder create a file called ‘firebase.js’.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1650867431994/NcHHtI3fh.png" alt="15.png" /></p>
<p>Within the file add your imports at the top and create a firebaseConfig variable using the SDK code from your Firebase project settings. Once the firebaseConfig variable has been made, initialize it via firebase.initializeApp() method. After that create a database variable to reference your project’s firestore database.</p>
<hr />
<p>import firebase from 'firebase/compat/app';
import "firebase/firestore"</p>
<p>const firebaseConfig = {
  apiKey: "AIzaSyBkzpkAYhekD5zDA-jOBpRA0ypq9CWHYZE",
  authDomain: "level-storm-330005.firebaseapp.com",
  projectId: "level-storm-330005",
  storageBucket: "level-storm-330005.appspot.com",
  messagingSenderId: "669292141198",
  appId: "1:669292141198:web:50599f5b45b83950d747df"
};</p>
<p>firebase.initializeApp(firebaseConfig);</p>
<p>export const db = firebase.firestore();</p>
<p>export default firebase;</p>
<hr />
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1650867503802/Dn7Cd1LGA.png" alt="16.png" /></p>
<p>Once that has been set up. The last step is to import it into your index.js file.</p>
<hr />
<p>import React from "react";
import ReactDOM from "react-dom";
import Routes from "./routes";
import firebase from "./utils/firebase";</p>
<p>ReactDOM.render(
  
    
  ,
  document.getElementById("root")
);</p>
<hr />
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1650867522199/Rk_t91s4K.png" alt="17.png" /></p>
<p>Although the line is subdued it's okay because you’ve initialized the app and is now connected to your index.js file.</p>
<p>That it! You’ve officially created a Firebase project with an accompanying Firestore database and linked it to your react-app!</p>
]]></content:encoded></item></channel></rss>