<?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 Sašo Sabotin on Medium]]></title>
        <description><![CDATA[Stories by Sašo Sabotin on Medium]]></description>
        <link>https://medium.com/@saosabotin?source=rss-e15020b38615------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*2hIlSD4RSX3CDVrKhlwnbw.jpeg</url>
            <title>Stories by Sašo Sabotin on Medium</title>
            <link>https://medium.com/@saosabotin?source=rss-e15020b38615------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 04:38:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@saosabotin/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[Immutability in Array of objects, using Map method]]></title>
            <link>https://medium.com/front-end-weekly/immutability-in-array-of-objects-using-map-method-dd61584c7188?source=rss-e15020b38615------2</link>
            <guid isPermaLink="false">https://medium.com/p/dd61584c7188</guid>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[array-methods]]></category>
            <category><![CDATA[immutability]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Sašo Sabotin]]></dc:creator>
            <pubDate>Tue, 26 Dec 2017 11:29:13 GMT</pubDate>
            <atom:updated>2018-11-09T16:39:03.205Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MZNBNQbu2QZ1_Aek6VrK1w.png" /></figure><p>If this article’s title spiked your interest enough to open it, then I must assume you know about how Array.map is implemented. It clearly states, that map method is not going to mutate the existing array, but it rather creates the new array of mutated data. Everything works as expected until it comes to the array of objects, which technically is working as expected as well.</p><blockquote>“You must be wrong, it does not work for me though!” is what you are thinking right now. Am I right?</blockquote><p>Let’s say, that we have an array of objects which looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/259ceeacc0822109ea032913aaa2cd88/href">https://medium.com/media/259ceeacc0822109ea032913aaa2cd88/href</a></iframe><p>Each event item has its own name and some metadata, where the type of the event is specified. In order to mutate this array of objects, we have multiple approaches. But essentially you have to loop through the array of items and modify its state.</p><p>To achieve that, I am normally using array’s map method implementation which accepts the callback function as the parameter, where all the magic happens.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Ff%2F788447%3Fas_embed%3Dtrue&amp;dntp=1&amp;display_name=Upscribe&amp;url=https%3A%2F%2Fupscri.be%2F788447%2F&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/18214d8333adbb5668f31791a3202b60/href">https://medium.com/media/18214d8333adbb5668f31791a3202b60/href</a></iframe><h3>Mutating the object is mutating the original array</h3><p>The map method is looping through the array of items, which are in our case Javascript objects. To change the object item, we normally mutate the actual item’s properties and return it as a new item, which is working as expected. But the problem is that the original array of objects is also going to be mutated.</p><h3>Why is original array’s data mutated as well?</h3><p>The map method is cloning the array as it is intended to do, but since we have an array of objects, each item is actually only a reference to the object in memory. This means, it is essentially cloning the object’s reference, which will point to the same block in the memory as the original array’s objects.</p><p>So when we are changing the properties of the object in the callback method, we are actually changing the properties via reference, which will mutate the original object as well. Since the object’s reference is located in the original array, we will also see the changes in the original array.</p><h3>Mutating the object without changing the original array</h3><p>If you want to make changes only in the cloned array of objects and not in the original one, there is a way to do it.</p><p>The basic idea is to create a new empty object for each item in the array and assign the properties from old objects to them, respectively. This means we get an object with a new reference, so we are not going to modify the old one anymore.</p><h4>Creating a new object and assign the properties</h4><p>To achieve that, we can use Object.assign() method, or from ES6 update on, we can use spread operator {...}, which will do that for us.</p><p>The code below will now create a new object with the value of the name property modified.</p><pre>const newObject = {...oldObject, name: &#39;Modified name&#39;}</pre><h3>Let’s try it with real example</h3><p>You have seen the array of objects which is going to be our example array. So this is what we are going to do:</p><ol><li>Loop through events, using Map method,</li><li>make a condition, to find the Event 2 event by name,</li><li>modify the object’s properties with help of the spread operator {...}.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/59da64f4d952c9f854b60fa2de8dff25/href">https://medium.com/media/59da64f4d952c9f854b60fa2de8dff25/href</a></iframe><p>In line 22, you can see that we are overwriting the original object with a newly created one and with the modified name to Second Event.</p><blockquote>It works! But why do we modify the metadata in the same way? Isn’t that cloned as well?</blockquote><p>You got the point! It is cloned, but still only the reference to the object. So even though we have cloned the whole object and created a new reference, we still have an object inside of an object, which is holding its own reference. The reference to this object is again located in the old array, therefore we have to create a new reference for that object if we don’t want to make changes to the old array.</p><p>You can find the solution, of how to create a new object and assign the new metadata properties to it, in line 23.</p><blockquote>Having a lot of nested objects will make cloning of the object a lot more difficult!</blockquote><p>I wish this article was helpful to you, so you don’t have to debug the Map method for hours! :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dd61584c7188" width="1" height="1" alt=""><hr><p><a href="https://medium.com/front-end-weekly/immutability-in-array-of-objects-using-map-method-dd61584c7188">Immutability in Array of objects, using Map method</a> was originally published in <a href="https://medium.com/front-end-weekly">Frontend Weekly</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Router Dom v4 with Redux]]></title>
            <link>https://medium.com/@saosabotin/react-router-dom-v4-with-redux-ed4c74746b8?source=rss-e15020b38615------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed4c74746b8</guid>
            <category><![CDATA[react-redux]]></category>
            <category><![CDATA[react-router-4]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[redux]]></category>
            <dc:creator><![CDATA[Sašo Sabotin]]></dc:creator>
            <pubDate>Tue, 21 Nov 2017 14:25:01 GMT</pubDate>
            <atom:updated>2017-11-21T14:25:01.076Z</atom:updated>
            <content:encoded><![CDATA[<p>I am working on some frontend user interface, using React library and I was surprised how easy it was to learn some of the basics of React, with combination of ES6 features and JSX extension which really simplifies the work with React.</p><blockquote>To summarize that, ES6 simplifies the coding but JSX simplifies the component concept understanding.</blockquote><p>Due to some Router’s API changes, I came across to some issue where I was having a hard time solving it for couple of hours and I am going to address it in this article. Since it was practically impossible for me to find some working example of React Router Dom and Redux combined, I will provide the reference to one (hopefully long-term) working example at the end of the article.</p><p>So I created the project with <em>create-react-app </em>command, went out for some tutorials of React, Router and Redux respectively. React was working fine, Redux as well, also Router was working fine in the beginning.</p><h3>What went wrong</h3><p>In React we differ between two types of components: <strong>Smart </strong>(also called Container)<strong> </strong>and <strong>Dumb</strong> (also called Presentational<strong>)</strong> components. Let’s not go too deep with that and just say that in our specific case <strong>Smart</strong> means that we are using the Redux’s store features in the component, but <strong>Dumb</strong> means that we don’t. There are even more different definitions, but we are not going to cover them in that article.</p><p>As soon as I wanted to use the <strong>Smart </strong>component in my app, where I was going to extract the user’s name, the Router stopped updating the DOM. The URL was changing in the address bar, but the active links and the components were just not updated on the page.</p><h3>Let’s see some code</h3><p>Here is the snippet of the root component which consists of Provider, Router and the Body components.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/829a6a5fd8db0bf3cdfcd0bf26ed5ea9/href">https://medium.com/media/829a6a5fd8db0bf3cdfcd0bf26ed5ea9/href</a></iframe><p>This probably looks familiar with you already, right? In order to use the Redux store we have to wrap all of our components into the<strong>&lt;<em>Provider /&gt;</em></strong><em> </em>component. We provide the store as property, which was previously created in the <em>store.js</em> file and exported as default variable.</p><p>Next thing to do, is to add the child component called <strong><em>&lt;Router /&gt;</em></strong> to make routes available in the whole application. As you can see the <strong>Router </strong>component does not require any properties to be provided anymore. In older versions of <em>React-Router-Dom </em>the <strong><em>history</em></strong> property was required but in <strong>v4 </strong>there are no required parameters.</p><h3>Router stopped working</h3><p>Now we have the Router running, Redux store created and almost everything seems fine. But the problem is that, whenever we want to navigate to a different URL either with <strong><em>&lt;Link /&gt;</em></strong> component, which generates a navigation link, or with some Redux’s <strong><em>push()</em></strong> action, the app is just not updating the DOM anymore.</p><h3><strong>Solution</strong></h3><p>Whenever you see some updates blocking, you can think about your Smart components missing one thing.</p><p>Each Smart component that is using some other Router components like <strong><em>&lt;Link /&gt;</em></strong> for example, should be exported using <strong><em>withRouter() </em></strong>function and should wrap the <strong><em>connect()</em></strong> function, which will make the component update the DOM on demand. You can see the example in the last line of following snippet.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d5926f913c2f3c01db46107cd93577bc/href">https://medium.com/media/d5926f913c2f3c01db46107cd93577bc/href</a></iframe><p>At this point, you also get some additional props injected into the component like:</p><ul><li>history<em> — history in the session</em></li><li>location<em> — current app location</em></li><li>match<em> — information about how path of the Route matched the URL</em></li><li>staticContext<em> — render route with StaticRouter which is only used for server-side rendering (undefined when using BrowserRouter)</em></li></ul><h3>The complete example</h3><p>You can check the whole working app in my repository, in order to get into some details that might help you developing your React application!</p><p><a href="https://github.com/sasos90/react-frontend">sasos90/react-frontend</a></p><p>Hopefully that article was helpful to you!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed4c74746b8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>