<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Keshav Kabra on Medium]]></title>
        <description><![CDATA[Stories by Keshav Kabra on Medium]]></description>
        <link>https://medium.com/@ksvkabra?source=rss-64edb5d7129f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*bjF9_7nKq7Qrnuyp.jpg</url>
            <title>Stories by Keshav Kabra on Medium</title>
            <link>https://medium.com/@ksvkabra?source=rss-64edb5d7129f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 00:26:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ksvkabra/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Destructuring Objects]]></title>
            <link>https://engineering.wisflux.com/destructuring-objects-a30f64c66088?source=rss-64edb5d7129f------2</link>
            <guid isPermaLink="false">https://medium.com/p/a30f64c66088</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[full-stack-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Keshav Kabra]]></dc:creator>
            <pubDate>Sun, 20 Sep 2020 05:20:17 GMT</pubDate>
            <atom:updated>2020-09-20T05:20:17.096Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L5RXpjs21prlUK_Iop20iw.png" /><figcaption>Photo: Chirayu Bansal/ Khyati Jha/ Wallpaper Flare</figcaption></figure><p>In this post, I will talk about destructuring, first introduced in the ES6 version of Javascript. Destructuring is a way of extracting properties from an object or values from an array into variables.</p><p>As mentioned destructuring was first introduced in the ES6 version of Javascript, so the earlier version of Javascript does not support the destructuring syntax, in that cases either you have to use the traditional way to unpack object and arrays, or you use Babel or anything that will compile your code to the previous version for use.</p><p>An <strong>object</strong> is a collection of properties, and a property is an association between a name (or key) and a value. Sometimes we need a few properties of the object assigned to variables. Before destructuring was introduced we used the dot operator to access properties from an object like in the below code, and you can keep doing that. Or you can use destructuring to make this process cleaner and simple.</p><pre><em>const</em> user <em>=</em> {<br>  name: &#39;John Doe&#39;,<br>  age: 12,<br>  gender: &#39;male&#39;<br>}<br>// Extracting properties using dot operator<br>const userName = user.name;<br>const userAge = user.age;<br>console.log(userName, userAge) // John Doe 12</pre><pre>// Extracting properties using destructuring<br><em>const</em> { name, age } <em>=</em> user;</pre><pre>console.log(name, age) // John Doe 12</pre><p>Here in the destructuring what happening is that the name and age properties from the user are assigned to name and age variables.</p><blockquote>NOTE: Although order of properties does not matter, the name of properties matter, if I add any other variable for destructuring I will get undefined assigned to it. Let’s see a small implementation for this</blockquote><pre>const user = {<br>  name: &#39;John Doe&#39;<br>}</pre><pre>const { name, age } = user;<br>console.log(name, age) // &#39;John Doe&#39;, undefined</pre><blockquote><em>age</em> is <em>undefined</em> because there is no property named<em>age</em> in the user object. So if you are destructuring an object you need to be specific with variable names you use.</blockquote><h3>Destructuring into variable name of your choice</h3><p>Destructuring objects with the variable names the same as the property name might cause trouble like redeclaring the variable in the same scope. Overcoming this challenge is easy, let’s see how.</p><pre>const userOne = {<br>  name: &#39;John Doe&#39;,<br>  age: 20<br>}</pre><pre>const userTwo = {<br>  name: &#39;Jane Doe&#39;,<br>  age: 19<br>}</pre><pre>const { name: userOneName, age: userOneAge } = userOne<br>const { name: userTwoName, age: userTwoAge } = userTwo</pre><pre>console.log(userOneName, userOneAge); // John Doe 20<br>console.log(userTwoName, userTwoAge); // Jane Doe 19</pre><blockquote>In the above code I have destructured the userOne properties name and age to userOneName and<em>userOneAg</em>e .</blockquote><pre>{ name: userOneName } = userOne</pre><p>This means that the property name from userOne is assigned to the variable userOneName.</p><h3>Default values while destructuring</h3><p>When destructuring we can provide default values to the variables, so in case if the property is not found in the object, a default value is assigned to the variable. The Default value is only assigned when a property is not found in the object.</p><pre>const user = {<br>  name: &#39;John Doe&#39;,<br>}</pre><pre>const { gender: userGender = &#39;male&#39;, age = 20 } = user;<br>console.log(userGender, age) //male, 20</pre><p>In the above code snippet, there is no gender andage property in the user object. So male is assigned to userGender variable and 20 are assigned to age.</p><h3>Get object with remaining properties (Rest)</h3><p>Destructuring remaining properties is also a similar action as we earlier performed in our array destructuring. Let’s see that for the object too. It is also known as the Rest operation of Spread and Rest operator (...)</p><pre>const userOne = {<br>  name: &#39;John Doe&#39;,<br>  gender: &#39;male&#39;,<br>  age: 20,<br>  isStudent: true,<br>}</pre><pre>const { isStudent, ...userData } = userOne<br>console.log(userData, isStudent)<br>// { name: &#39;John Doe&#39;, gender: &#39;male&#39;, age: 20 } true</pre><p>The isStudent variable is assigned the property isStudent from userOne, and the remaining properties of userOne are assigned to userData. To achieve this we used the rest operator.</p><blockquote><strong>Note</strong>: To do proper destructuring the remaining variable should be at the rightmost part in the variable declaration. Or there will be an error</blockquote><blockquote><strong>‘ SyntaxError: Rest element must be last element’</strong></blockquote><pre>const userTwo = {<br>  name: &#39;John Doe&#39;,<br>  gender: &#39;male&#39;,<br>  age: 19,<br>  isStudent: true,<br>}</pre><pre>const { isStudent, name, gender, age, ...userDataTwo } = userTwo<br>console.log(isStudent, name, gender, age, userDataTwo)<br>// true &#39;John Doe&#39; &#39;male&#39; 19 {}</pre><p>Here when we will destructure the object, all the properties will be assigned to corresponding variables before reaching the variable userDataTwo. Now userDataTwo will not get assigned anything, now the question is what will happen if nothing is assigned to it. One can assume that it will be undefined but actually will be an empty object.</p><blockquote>Here comes a question of why some variables are undefined when not matched and others just empty. Let get this one clear. When a property is not found in the object, the variable with the same name gets undefined, but when we use rest operator then the variable is an empty object.</blockquote><h3>Two-step declaration and assignment</h3><p>In destructuring, it is not mandatory to destructure at the time of variable declaration (not applicable for <em>const</em> variable). To destructure after variable declaration we have to use parenthesis around the expression, otherwise, it will be a syntax error</p><blockquote>“<strong>Declaration or statement expected”.</strong></blockquote><pre><em>const</em> user <em>=</em> {<br>  name: &#39;John Doe&#39;,<br>  age: 12,<br>  gender: &#39;male&#39;<br>}</pre><pre>let name, age;<br>({ name, age } <em>=</em> user);<br>console.log(name, age) // John Doe 12</pre><h3>Destructuring for nested objects</h3><p>The object can have nested objects or arrays. When destructuring nested objects we can destructure them by nesting as the objects or arrays are nested. When we have the property, then we can break it into smaller parts by destructuring.</p><pre>const userData = {<br>  name: &#39;John Doe&#39;,<br>  age: 22,<br>  gender: &#39;male&#39;,<br>  friends: [&#39;Phoebe&#39;,&#39;Ross&#39;,&#39;Monica&#39;,&#39;Chandler&#39;,&#39;Joey&#39;,&#39;Rachel&#39;],<br>  medium: {<br>    username: &#39;johndoe&#39;,<br>    blogs: 2,<br>    organization: &#39;Central Perk&#39;,<br>    claps: 5,<br>  }<br>}</pre><pre>const { friends:[ friendOne, friendTwo ] } = userData<br>const { <br>  medium: { username, organization: userOrganization } <br>} = userData</pre><pre>console.log(friendOne, friendTwo) // Phoebe Ross<br>console.log(username, userOrganization) // johndoe Central Perk</pre><pre>console.log(friends) // ReferenceError: friends is not defined<br>console.log(medium) // referenceError: medium is not defined</pre><p>Here theuserData object has a property named friends which carries an array and medium property which carries an object. To destructure and extract values from the array we have to first get the property and then destructure the array. To destructure and extract properties from the medium we have to extract the medium first then destructure it again at the same moment.</p><h3>Wrapping it all up</h3><p>As you can see from the above examples, destructuring is a clean and nice way to work with objects and arrays. I have covered almost everything basically needed to use destructuring in your code. I hope this helps you understand destructuring in Javascript and you feel more confident when working with arrays and objects.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a30f64c66088" width="1" height="1" alt=""><hr><p><a href="https://engineering.wisflux.com/destructuring-objects-a30f64c66088">Destructuring Objects</a> was originally published in <a href="https://engineering.wisflux.com/">Wisflux Engineering Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Destructuring Array]]></title>
            <link>https://engineering.wisflux.com/destructuring-array-82837973a95a?source=rss-64edb5d7129f------2</link>
            <guid isPermaLink="false">https://medium.com/p/82837973a95a</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Keshav Kabra]]></dc:creator>
            <pubDate>Mon, 03 Aug 2020 06:57:36 GMT</pubDate>
            <atom:updated>2020-08-03T06:57:36.233Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XxjimgYDYoxWmhoJlkOaNQ.jpeg" /></figure><p>Today I will talk about destructuring an array, first introduced in the ES6 version of Javascript. Destructuring is a way of extracting values from an array into variables.</p><p>As mentioned destructuring came with ES6 version of Javascript, so earlier versions of Javascript do not support the destructuring syntax, in that cases either you have to use the traditional way to unpack object and arrays, or you use Babel or anything that will compile your code to the previous version for use.</p><h3>Destructuring Array</h3><blockquote>Array properties are accessed using index values, but what if you have an array and you want to assign the elements to different variables. If you use the old way of assigning the values one by one, it is tedious. Like in the below code <em>first</em> and <em>second</em> have the values <em>1</em> and <em>2</em> from the array but that can be done in a single line by destructuring the array <em>arr.</em></blockquote><pre>const arr = [1, 2, 3, 4, 5, 6];</pre><pre>// The way we did before ES6<br>const first = arr[0];<br>const second = arr[1];<br>console.log(first, second); // 1 2</pre><pre>// Using destructuring<br>const [dFirst, dSecond] = arr;<br>console.log(dFirst, dSecond); // 1 2</pre><h3>Assignment separate from declaration</h3><p>In destructuring array, it is not mandatory to destructure at the time of variable declaration (remember do not declare the variables as const, or you won’t be able to assign values to them at the time of destructuring).</p><pre>const arr = [1, 2, 3, 4, 5, 6];<br>let first, second;</pre><pre>[first, second] = arr;<br>console.log(first, second); // 1 2</pre><h3>Destructuring with default values</h3><p>Sometimes, we are not sure about the size of an array, and that might be something that leads to values undefined, so to avoid such a situation, we can give default values to the variables, at the time of destructuring.</p><pre>const arr = [1, 2, 3, 4, 5, 6];<br>const arrWithOneValue = [1]</pre><pre>const [dFirst = 3, dSecond = 4] = arr;<br>console.log(dFirst, dSecond); // 1 2</pre><pre>const [first = 4, second = 5] = arrWithOneValue;<br>console.log(first, second) // 1 5</pre><p>Here the length of the array arrWithOneValue is 1 so when we will console, the output from first will be 1 and output from second will be 5 instead of undefined.</p><h3>Destructuring and skipping</h3><p>You want to destructure, but with that, you want to skip some values in between. You can use the following method to achieve that.</p><pre>const arr = [1, 2, 3, 4, 5];<br>const [dFirst,,dSecond,,,dThird] = arr;<br>console.log(dFirst, dSecond, dThird); // 1 3 undefined</pre><p>Here 1 will be assigned to dFirst and then on getting no variable between the next two commas, 2 will be skipped. Similarly, 3 will be assigned to dSecond and then again 4 and 5 will be skipped on empty commas. Since there are no values left in the array to be assigned dThird will be assigned undefined .</p><h3>Rest or assign remaining</h3><p>There are times when we want to assign elements of an array to some variables and assign remaining elements to a new array. It can be done using spread operators in js.</p><pre>const arr = [1, 2, 3, 4, 5];<br>const [dFirst, ...remaining] = arr;<br>console.log(dFirst, remaining); // 1 [2,3,4,5] </pre><p>Here the first element of the array will be assigned to dFirst and then the remaining elements of array arr will be copied to remaining array.</p><blockquote>NOTE: When there is no elements remaining in the array <em>arr</em>, the remaining array on the left side is declared as an empty array.</blockquote><h3>Destructuring Array of Objects</h3><p>Destructuring an array of objects needs the understanding of both the array and object destructuring. One thing to keep in mind is to what extent we want to destructure our array. Now let us see the implementation below and understand how to do this.</p><pre>const users = [<br>  {<br>    name: &#39;John Doe&#39;,<br>    age: 22,<br>  },{<br>    name: &#39;Jane Doe&#39;,<br>    age: 19,<br>  }<br>]</pre><pre>// Destructuring array indices in variables<br>const [userOne, userTwo] = users;<br>console.log(userOne); { name: &#39;Jane Doe&#39;, age: 19 }<br>console.log(userTwo); { name: &#39;Jane Doe&#39;, age: 19 }</pre><pre>// Destructuring array and object inside the array<br>const [{name, age}] = users;<br>console.log(name, age); //John Doe 22</pre><p>Here we have an array named users, We are destructuring array users in two variables, userOne and userTwo, here both the variables will be assigned the object with the corresponding index. Next, we are destructuring deep at a deep level by destructuring the object inside the array at the same moment.</p><h3>Destructuring Array of Array</h3><p>Destructuring an array of an array is similar to an array of objects. We just go deep and extract properties of the array in variables we want. Here I am destructuring array inside an array. Now, I have two choices here either I extract the array inside the numbers array in a variable of type array or I can destructure deep and extract the array data in different variables.</p><pre>const numbers = [1, 2, [1, 2, 3], 4, [5, 6]]<br>const [<br>  firstNumber,<br>  ,<br>  [thirdNumberOne, thirNumberTwo, thirdNumberThree],<br>  fourthNumber,<br>  fifthNumber<br>] = numbers</pre><pre>console.log(firstNumber); // 1<br>console.log(thirdNumberOne, thirdNumberTwo, thirdNumberThree) <br>// 1 2 3</pre><pre>console.log(fourthNumber) // 4<br>console.log(fifthNumber) // [5,6]</pre><pre>const [valueOne, valueTwo] = fun(user);<br>console.log(valueOne, valueTwo); // John Doe 10</pre><h3>Wrapping it all up</h3><p>As you can see from the above examples, destructuring is a clean and nice way to work with arrays in JavaScript. I have covered almost all patterns in which destructuring could be used. I hope this helps you understand destructuring in JavaScript and you feel more confident when working with arrays.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=82837973a95a" width="1" height="1" alt=""><hr><p><a href="https://engineering.wisflux.com/destructuring-array-82837973a95a">Destructuring Array</a> was originally published in <a href="https://engineering.wisflux.com/">Wisflux Engineering Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React with Typescript]]></title>
            <link>https://engineering.wisflux.com/react-with-typescript-d01ce5dc4eb3?source=rss-64edb5d7129f------2</link>
            <guid isPermaLink="false">https://medium.com/p/d01ce5dc4eb3</guid>
            <category><![CDATA[typescript-with-react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Keshav Kabra]]></dc:creator>
            <pubDate>Sun, 12 Jul 2020 13:48:25 GMT</pubDate>
            <atom:updated>2020-07-12T13:48:25.106Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vum7WqnVNOU5KqLZ0wYbKw.jpeg" /></figure><p>Using typescript in React can be hard in the beginning but as we move ahead with this, it starts to grow on us, and instead of feeling forced to use types everywhere it becomes a habit, a good one. This article is about how one can use type definitions properly in React components. All you need is a basic knowledge of React and Typescript.</p><h3>React Javascript Components</h3><p>Before diving into the React typescript environment lets just see how we create components in React javascript.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/500984c6c3fbdb985d71f4e001a31adb/href">https://medium.com/media/500984c6c3fbdb985d71f4e001a31adb/href</a></iframe><p>As you can see, the Input component is a class component and the Button component is a functional component. For understanding functional or class component, you can <a href="https://reactjs.org/docs/components-and-props.html"><strong>visit here</strong></a>. We will take these as examples and will change these components to React typescript.</p><h3>React Typescript Components</h3><p>Lets first of all setup our React typescript project from the beginning. Just enter the following command in your command line and wait for it.</p><p>$ npx create-react-app react-ts-dummy --typescript</p><p>This will create a React project with typescript build setup from beloved create-react-app. After the project is all set up. You can see components with extension .tsx and the reason behind it is that .tsx is a special extension which supports jsx .</p><p>Now go to the src folder and create three new components with the name Form.tsx , Button.tsx and Input.tsx . Your project directory might look something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/421/1*Za9h6fj5Gaivo6GkJRYCIg.png" /><figcaption>react-ts-dummy directory structure</figcaption></figure><p>The below code snippet contains the code for Form component, first, please go through the following code then let’s look at what’s going on in here.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/03a41d94bd680ed2ac70e3ff2f01a8c0/href">https://medium.com/media/03a41d94bd680ed2ac70e3ff2f01a8c0/href</a></iframe><p>The Form.tsx file has two interfaces FormProps and FormState , now the question is how to use these interfaces as the type definitions of component’s state and props. To pass the FormProps and FormState component’s type definition we have to use angle brackets in as follows:</p><p>class Form extends Component&lt;FormProps, FormState&gt; {…</p><p>By defining the component with FormProps and FormState, now React APIs can benefit from these types. When we are inside these components we can utilize the type definition. Everything ahead will be the same as in the React javascript environment, with this change you get all the benefits of typescript all along.</p><p>Let’s go through the following code snippet and understand the function type definition when a function is passed in props. In the below code, onChange function is passed as property to Input component, so we have to define a type of what the function can take as an argument and what it will return.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af02a6b9a35dee087b569730f1f2d651/href">https://medium.com/media/af02a6b9a35dee087b569730f1f2d651/href</a></iframe><p>The Input component has no state, so we can pass an empty object as it’s type. Also, it has props coming in from the Form component and the InputProps interface can passed in angle brackets to define the props type of InputComponent.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/348dd125ecf4675650ce0af8d5b471da/href">https://medium.com/media/348dd125ecf4675650ce0af8d5b471da/href</a></iframe><p>The Button component is a functional component in our example. Now as we know functional component does not have a state, so we do not need to define the type of state, all we have to define is, type definitions for incoming props.</p><p>const Button: React.FC&lt;ButtonProps&gt; = (props) =&gt; {...</p><p>In the above code, I have use React.FC to add types to the props of the component. Even though children is not part of ButtonProps, React implicity provides children property with props we defined.</p><h3>useState</h3><p>Coming to one last thing, type definitions in react hooks can be tricky and I am not going to cover all the hooks, but the most famous one. Type definition for useState hook is almost the same as the type definition we gave to the functional component.</p><p>const [open, setOpen] = React.useState&lt;boolean&gt;(false)</p><p>Well useState is actually a <a href="https://www.typescriptlang.org/docs/handbook/generics.html">generic function</a> that can have the type passed into it, using the angle brackets afteruseState we can define the type.</p><p>Thank you for giving my blog a read. This is my first blog and I am feeling amazing writing this, I might have made some mistakes too, so if you find one just leave a comment.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d01ce5dc4eb3" width="1" height="1" alt=""><hr><p><a href="https://engineering.wisflux.com/react-with-typescript-d01ce5dc4eb3">React with Typescript</a> was originally published in <a href="https://engineering.wisflux.com/">Wisflux Engineering Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>