<?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>Forem: Saksham Malhotra</title>
    <description>The latest articles on Forem by Saksham Malhotra (@saksham-malhotra).</description>
    <link>https://forem.com/saksham-malhotra</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%2F1252785%2F42cdd898-8819-4d3d-bc65-1ab0c5937d2d.jpeg</url>
      <title>Forem: Saksham Malhotra</title>
      <link>https://forem.com/saksham-malhotra</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/saksham-malhotra"/>
    <language>en</language>
    <item>
      <title>Currying can save passing extra props!</title>
      <dc:creator>Saksham Malhotra</dc:creator>
      <pubDate>Tue, 09 Jan 2024 20:58:19 +0000</pubDate>
      <link>https://forem.com/saksham-malhotra/currying-can-save-passing-extra-props-f55</link>
      <guid>https://forem.com/saksham-malhotra/currying-can-save-passing-extra-props-f55</guid>
      <description>&lt;p&gt;Before we proceed with using currying in a sample React example to omit passing extra props, do check out &lt;strong&gt;&lt;a href="https://dev.to/saksham-malhotra/lets-make-a-curry-426k"&gt;what currying is&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's consider a simple example where we are developing the players section in a game and the player names are editable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Players = () =&amp;gt; {
    const [playerNames, setPlayerNames] = useState(["Player 1", "Player 2"]);

    function updatePlayerName(index, name) {
        setPlayerNames((prevPlayers) =&amp;gt; {
            prevPlayers[index] = name;
            return [ ...prevPlayers ];
        });
    }

    return (
        &amp;lt;&amp;gt;
            {playerNames.map((playerName, index) =&amp;gt; {
                &amp;lt;Player
                    key={index}
                    name={playerName}
                    index={index}
                    updatePlayer={updatePlayerName}
                &amp;gt;&amp;lt;/Player&amp;gt;;
            })}
        &amp;lt;/&amp;gt;
    );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we are passing a prop called &lt;code&gt;index&lt;/code&gt; just to enable updating name at a particular index in the state. But apart from that, there should be no use of index in the child &lt;code&gt;Player&lt;/code&gt; component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How can we avoid passing this extra &lt;code&gt;index&lt;/code&gt; prop?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You guessed it right, by using the concept of currying. We can simply curry the &lt;code&gt;setPlayerName&lt;/code&gt; method as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function updatePlayerName(index) {
    return function (name: string) {
        setPlayerNames((prevPlayers) =&amp;gt; {
            const updatedPlayers = [...prevPlayers];
            updatedPlayers[index] = name;
            return updatedPlayers;
        });
    };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and call it in the template as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;updatePlayer={updatePlayerName(index)}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that now the &lt;code&gt;updatePlayerName&lt;/code&gt; function is invoked inline in the template instead of being just assigned as a reference. This helps return a new curried reference to the function to pass down thus eliminating the need of passing index as an additional prop.&lt;/p&gt;

&lt;p&gt;Best of coding!&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactjsdevelopment</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Let's make a curry!</title>
      <dc:creator>Saksham Malhotra</dc:creator>
      <pubDate>Tue, 09 Jan 2024 19:32:41 +0000</pubDate>
      <link>https://forem.com/saksham-malhotra/lets-make-a-curry-426k</link>
      <guid>https://forem.com/saksham-malhotra/lets-make-a-curry-426k</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Currying is a technique in JavaScript that allows you to transform functions with multiple arguments into a sequence of functions, each taking one argument at a time. That is from &lt;code&gt;f(a, b, c)&lt;/code&gt; to &lt;code&gt;f(a)(b)(c)&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This might sound overwhelming at first but should be very easy to understand with an example.&lt;/p&gt;

&lt;p&gt;Suppose we want to multiply 2 numbers together. Assuming it is not straight forward, we can declare a function called &lt;code&gt;multiply&lt;/code&gt; as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function multiply(num1, num2) {
    // pseudo complex logic
    return num1 * num2;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;But what if we want to generate variants which can either double or triple or quadruple a number?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One option could be to simply define each variant separately as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function double(num1) {
    return multiply(2, num1);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or we can be clever and use currying here&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const generateMultiplier = function(multiplier) {
    return function multiply(num) {
        return multiplier * num;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would give us the power to generate variants as we like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const double = generateMultiplier(2);

const result = double(2); // returns 4

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

&lt;/div&gt;



&lt;p&gt;Best of coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
