<?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: asiddharth94</title>
    <description>The latest articles on DEV Community by asiddharth94 (@asiddharth94).</description>
    <link>https://dev.to/asiddharth94</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%2F349688%2F66b7f024-2af0-4c2a-ba69-1a25a2f8c238.jpg</url>
      <title>DEV Community: asiddharth94</title>
      <link>https://dev.to/asiddharth94</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/asiddharth94"/>
    <language>en</language>
    <item>
      <title>Introduction to Redux pattern</title>
      <dc:creator>asiddharth94</dc:creator>
      <pubDate>Tue, 15 Mar 2022 12:23:35 +0000</pubDate>
      <link>https://dev.to/asiddharth94/introduction-to-redux-pattern-41ig</link>
      <guid>https://dev.to/asiddharth94/introduction-to-redux-pattern-41ig</guid>
      <description>&lt;p&gt;As the &lt;a href="https://redux.js.org/"&gt;documentation&lt;/a&gt; reads - Redux is &lt;strong&gt;a predictable state container for JS apps&lt;/strong&gt; .&lt;/p&gt;

&lt;p&gt;Let's break this up -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;&lt;strong&gt;predictable&lt;/strong&gt;&lt;/em&gt; - Redux helps you write applications that behave consistently (we update the state using &lt;em&gt;&lt;u&gt;'reducers'&lt;/u&gt;&lt;/em&gt;, which are pure functions. We'll get to reducers later), run in different environments (client, server, and native). All state transitions are explicit and it is possible to keep track of them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;&lt;strong&gt;state container&lt;/strong&gt;&lt;/em&gt; - Redux &lt;em&gt;&lt;u&gt;'store'&lt;/u&gt;&lt;/em&gt; is an object which holds the whole global state of our app. And then, we can just get the values from the store to wherever we need in our app. Awesome! Right?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;&lt;strong&gt;for JS apps&lt;/strong&gt;&lt;/em&gt; - Redux can be used with vanilla JS or any JS framework. (People, No! It is not a React thing 😛)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Redux can be described in &lt;a href="https://redux.js.org/understanding/thinking-in-redux/three-principles"&gt;Three fundamental Principals&lt;/a&gt; - &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Single source of truth&lt;/strong&gt; - A fancy way of saying that we have one single big object that describes the entire state of the app. &lt;em&gt;(Note - It is also a common pattern to keep only important state in Redux store while keeping UI specific state like form-inputs in component’s state)&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;State is read-only&lt;/strong&gt; - The state is immutable. The only way to change the state is to emit an &lt;em&gt;&lt;u&gt;'action'&lt;/u&gt;&lt;/em&gt;. &lt;em&gt;(Don't worry, if you don't know what 'actions' are yet. Keep reading! You'll know! 🙂)&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Changes using pure functions&lt;/strong&gt; - We write pure reducers which takes in 'currentState' and an 'action', and returns the 'nextState'. &lt;em&gt;(pure - given same input, the output is always same)&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Cool! Let's get Coding!&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Giq-uXpK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z6ky55whcrnq0moxj9dq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Giq-uXpK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z6ky55whcrnq0moxj9dq.png" alt="Image description" width="441" height="501"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An &lt;strong&gt;Action&lt;/strong&gt; is a plain object which has two properties -&lt;br&gt;
'type' (name of action: string)&lt;br&gt;
'payload' (the value to be updated)&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
   type: 'add_todo',
   payload: 'learn redux'
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;A &lt;strong&gt;Reducer&lt;/strong&gt; is a pure function that gets two arguments &lt;br&gt;
(current/prev state &amp;amp; action) and returns the new state based on the action's type.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const reducer = (state={todo: [], otherValues: {}}, action) =&amp;gt; {
  switch (action.type) {
    case "add_todo":
      return {
         ...state,
         todo: [...state.todo, action.payload]
      };
    default:
      return state;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, store!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As we know, &lt;strong&gt;store&lt;/strong&gt; is a single object which holds the complete global state of our app. Redux provides us with 'createStore' api to which we pass our reducer and that is it, we have our global state, i.e, the store.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createStore } from "redux";

let store = createStore(reducer);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we need to subscribe to the store to listen when the store is updated and thus, update our UI accordingly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;store.subscribe(() =&amp;gt; console.log(store.getState()));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note - subscribe() is not the only way as different frameworks have different view binding libraries for this use case, e.g, react-redux.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Last thing! Now we know that our store can only be updated by dispatching actions! Let's do that! 🚀&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;store.dispatch({ type: "add_todo", payload: "learn redux" });

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

&lt;/div&gt;



&lt;p&gt;ANNNDDDDD, THAT IS IT ! YAY !&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/dank-rain-0ltfjl"&gt;Here&lt;/a&gt; is a working link! 🙂&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MORE -&lt;/strong&gt;&lt;br&gt;
As our app grows, we might need to have multiple reducers to manage different parts of our application's state.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;combineReducers()&lt;/strong&gt; takes an object which has these multiple reducers and returns a single reducer which is a combination of all reducers passed to it. We can then pass this to createStore().&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const rootReducer = combineReducers({
      one: reducerOne,
      two: reducerTwo
});

const store = createStore(rootReducer);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>redux</category>
      <category>react</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>bind() polyfill - JavaScript</title>
      <dc:creator>asiddharth94</dc:creator>
      <pubDate>Sun, 06 Mar 2022 14:59:56 +0000</pubDate>
      <link>https://dev.to/asiddharth94/understanding-bind-polyfill-5fpb</link>
      <guid>https://dev.to/asiddharth94/understanding-bind-polyfill-5fpb</guid>
      <description>&lt;p&gt;"&lt;strong&gt;Write your own 'bind' function&lt;/strong&gt;" - pretty common in interviews, right?&lt;/p&gt;

&lt;p&gt;Let me try to simplify it for you! 😃&lt;/p&gt;

&lt;p&gt;Let's begin with the &lt;em&gt;definitions&lt;/em&gt; -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The bind() method creates a new function that, when called, has its &lt;em&gt;'this'&lt;/em&gt; keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.&lt;/strong&gt;&lt;br&gt;
Refer - &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;A polyfill is a piece of code (usually JavaScript on the Web) used to provide modern functionality on older browsers that do not natively support it.&lt;/strong&gt;&lt;br&gt;
Refer - &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Polyfill"&gt;https://developer.mozilla.org/en-US/docs/Glossary/Polyfill&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, the fun and easy part. &lt;em&gt;Implementation&lt;/em&gt; -&lt;/p&gt;

&lt;p&gt;First things first, adding our 'myBind' to the prototype (&lt;em&gt;to be able to use it on other functions, right?&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;Function.prototype.myBind = function() {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, one thing we know for sure is that the 'bind()' returns a function (copy of the function on which it was called). Let's do that with our own bind(), i.e, myBind()! &lt;em&gt;One step at a time.&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;Function.prototype.myBind = function() {
    return function() {}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, as we know the '.bind()' returns a copy of the function on which it was called, let's get the caller function first with the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; inside our '.myBind' function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Function.prototype.myBind = function() {
    //'this' here will be the function on which 'myBind' was called
    var callerFunc = this;
    return function() {}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we'll call the callerFunc in our returned function, because that is our ultimate goal, right?&lt;/p&gt;

&lt;p&gt;I'm using the 'call' method because we need to set the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; context in the callerFunc based on the arguments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Function.prototype.myBind = function() {
    //'this' here will be the function on which 'myBind' was called
    var callerFunc = this;
    return function() {
        callerFunc.call();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, the bind() method accepts a list of parameters. First one is the object to which the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; refers in the returned copy function, followed by the function parameters.&lt;/p&gt;

&lt;p&gt;So, let's get the arguments (using &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters"&gt;rest&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax"&gt;spread&lt;/a&gt; syntax) and set the context in our myBind().&lt;/p&gt;

&lt;p&gt;💡 &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call"&gt;Function.prototype.call()&lt;/a&gt; 's first arg is the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; arg, followed by list of args.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Function.prototype.myBind = function(...args) {
    //'this' here will be the function on which 'myBind' was called
    var callerFunc = this;
    var thisArg = args[0];
    return function() {
        callerFunc.call(thisArg);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; context set properly, let's handle the next list of parameters, which &lt;strong&gt;myBind()&lt;/strong&gt; might receive (optional params).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HOLD ON!!&lt;/strong&gt;&lt;br&gt;
We can get the remaining parameters (i.e, other than thisArg) passed to myBind() by using the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice"&gt;slice&lt;/a&gt; method on our 'args' array.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var params = args.slice(1)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;BUT, the 'params' is an array now and we can only pass comma separated values to our call() method.&lt;/strong&gt; &lt;em&gt;What to do?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Here comes,&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply"&gt; Function.protoype.apply()&lt;/a&gt; 🚀&lt;/p&gt;

&lt;p&gt;The apply() method is similar to call(). Only difference is that it accepts argsArray[] as the second argument. Problem Solved!!! Let's do the changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Function.prototype.myBind = function(...args) {
    //'this' here will be the function on which 'myBind' was called
    var callerFunc = this;
    var thisArg = args[0];
    var params = args.slice(1);
    return function() {
        // using apply instead of call!
        callerFunc.apply(thisArg, params);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Last step, we also need to take care of the arguments which might not be passed in the beginning to myBind() but instead, received when the returned function from myBind() is called.&lt;/p&gt;

&lt;p&gt;Let's use the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters"&gt;rest&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax"&gt;spread&lt;/a&gt; and achieve our goal!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Function.prototype.myBind = function(...args) {
    //'this' here will be the function on which 'myBind' was called
    var callerFunc = this;
    var thisArg = args[0];
    var params = args.slice(1);
    return function(...remArgs) {
        // using apply instead of call!
        callerFunc.apply(thisArg, [...params, ...remArgs]);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ANNNDDDDD, THAT IS IT ! YAY ! &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>functions</category>
      <category>bind</category>
      <category>polyfill</category>
    </item>
    <item>
      <title>repeat() - JavaScript</title>
      <dc:creator>asiddharth94</dc:creator>
      <pubDate>Thu, 12 Mar 2020 12:46:41 +0000</pubDate>
      <link>https://dev.to/asiddharth94/repeat-javascript-4d8b</link>
      <guid>https://dev.to/asiddharth94/repeat-javascript-4d8b</guid>
      <description>&lt;p&gt;The repeat() is called on a string which takes one argument (number of times the string needs to be repeated) and returns a new string , containing the repetitions of the string on which it was called (argument times), concatenated together.&lt;/p&gt;

&lt;p&gt;Note - The argument passed must be non-negative and the new string formed must not overflow the maximum string size.&lt;/p&gt;

&lt;pre&gt;&lt;b&gt;'#'.repeat(3)&lt;/b&gt; returns &lt;b&gt;'###'&lt;/b&gt;&lt;/pre&gt;

</description>
      <category>firstpost</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>repeat</category>
    </item>
  </channel>
</rss>
