<?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[Knowit Development - Medium]]></title>
        <description><![CDATA[Learnings, insights and inspos in tech, design, people, QA and what not as consultants for one of the leading digital consultancy firms in the Nordic region — Knowit. We are experts in systems development. Find us at www.knowit.se - Medium]]></description>
        <link>https://medium.com/knowit-development?source=rss----5c849d797438---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Knowit Development - Medium</title>
            <link>https://medium.com/knowit-development?source=rss----5c849d797438---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 12 Nov 2026 11:14:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/knowit-development" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Recursive components in React]]></title>
            <link>https://medium.com/knowit-development/recursive-components-in-react-36bad5f0402b?source=rss----5c849d797438---4</link>
            <guid isPermaLink="false">https://medium.com/p/36bad5f0402b</guid>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Joel Gustafsson]]></dc:creator>
            <pubDate>Wed, 18 Nov 2020 18:52:35 GMT</pubDate>
            <atom:updated>2020-11-18T18:55:15.775Z</atom:updated>
            <content:encoded><![CDATA[<p>I think most developers have touched upon the concept of recursion, whether it be in that algorithmic course you dreaded in school or when working with some functional language. I also believe most developers have had some problems wrapping their head around the concept. That was at least my experience — so I definitely felt a bit apprehensive but also intrigued when I realised I had a problem that best could be solved with recursion. In this case by building a recursive component in React. To my great relief it was actually a lot simpler than I first thought.</p><p>To start off I will quickly go through the concept of recursion before showing how you can build your own recursive components. If you already have some knowledge about this feel free to skip ahead.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6WFRE1zikpo1cw7-W30RWQ.jpeg" /><figcaption>A recursive image! There’s an image of the image in the image 🤯. A visual form of recursion. Source: <a href="https://en.wikipedia.org/wiki/Recursion">https://en.wikipedia.org/wiki/Recursion</a></figcaption></figure><h3>Recursion</h3><p>Recursion is very common in divide and conquer algorithms or when working with tree structures of data. It is actually as simple as a function that is invoking itself until it reaches a base case. The base case is the end of the recursion where the function stops calling/invoking itself. If a recursion doesn’t have a base case it will create an infinite loop and go on until something finally crashes.</p><h3>Recursive React components</h3><p>Okay, so now we have a basic idea of what recursion is, let’s apply it to some React code. To understand the concept we will look at a very simple implementation of a recursive component.</p><p>As previously mentioned, a recursive function is calling itself for <em>n</em> number of times until it reaches a base case. React components are also functions, so what if we call a react component from itself? Et voilà, we have a recursive component! In the below example our component has two props: name and items . We render the name and if we have another level of items we call our component again with the next level instead — in other words <strong>we are “recursing”</strong>. We do this until we reach an item that doesn’t have any more children, that’s our base case. As you can see we will only call our component if it has children.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/948c2d59341310dae03a2d9296cc7a7e/href">https://medium.com/media/948c2d59341310dae03a2d9296cc7a7e/href</a></iframe><p>To use the component we simply pass some data to it and there you have it!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/34c67191532f1afa8db589c371282c07/href">https://medium.com/media/34c67191532f1afa8db589c371282c07/href</a></iframe><p>It doesn’t have to be harder than this. We can pass an indefinite number of levels to the component and it still works. This is actually quite a good foundation to build some cool stuff from by just adding some CSS and a tiny bit of more functionality to it.</p><p>Here’s an example of a multi-level stack navigation component built with the same concept. Didn’t spend too much time on the CSS but you get the gist of it ;)</p><p><a href="https://codesandbox.io/s/gallant-lehmann-g8ifc?file=/src/App.js">https://codesandbox.io/s/gallant-lehmann-g8ifc?file=/src/App.js</a></p><h3>Some final words</h3><p>There’s nothing magical about recursion and most of the time we can achieve the same results by just using loops. In my opinion recursion should be used lightly as it adds some complexity to the code. But for some specific use cases like a folder structure or a navigation menu where we can have an arbitrary number of children it makes sense to use it, as other solutions like loops could be more complex.</p><p>Feel free to comment or link to some of your own implementations using recursive components. Until next time!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36bad5f0402b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/knowit-development/recursive-components-in-react-36bad5f0402b">Recursive components in React</a> was originally published in <a href="https://medium.com/knowit-development">Knowit Development</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Recreating redux]]></title>
            <link>https://medium.com/knowit-development/recreating-redux-a28d4e47b76f?source=rss----5c849d797438---4</link>
            <guid isPermaLink="false">https://medium.com/p/a28d4e47b76f</guid>
            <category><![CDATA[knowit]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[redux]]></category>
            <dc:creator><![CDATA[Joel Gustafsson]]></dc:creator>
            <pubDate>Tue, 22 Sep 2020 13:59:42 GMT</pubDate>
            <atom:updated>2020-09-29T10:17:19.704Z</atom:updated>
            <content:encoded><![CDATA[<p><em>I had a professor that once said; you can’t fully understand an algorithm until you can code it yourself. This is the first part of a series of articles where we will follow his wise (but maybe not completely true) advise. We will learn some of the most popular tools used in modern front end development by recreating them from scratch. Coming articles will include implementations of module bundling tools, testing frameworks and server side rendering.</em></p><p>Redux is one of the most used, loved, and confusing libraries in modern frontend. It is something that most new React developers stumble upon and can have a hard time wrapping their head around. In this article I will try to explain how Redux works by creating our own lightweight implementation of it.</p><p>But first, what is Redux? According to the Redux docs:</p><blockquote>“Redux is a predictable state container for JavaScript apps.”</blockquote><p>In other words, redux helps you manage <strong>state</strong> for JavaScript web applications. It’s most commonly used together with React but can be used together with any framework like Vue, Angular or for vanilla applications without any framework at all.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/792/0*28K-LjfCWZrCLgIW" /><figcaption>Redux architecture</figcaption></figure><p>Illustrated above are the building blocks of Redux architecture; <strong>actions</strong>, <strong>reducer</strong>, <strong>store</strong> and <strong>view</strong>. Our custom implementation of Redux will follow the above pattern.</p><p>The application state is managed in the store. The state itself is just a plain JavaScript object. The store also provides methods to update the state and read from the state.</p><p>Redux is using a publish/subscribe (PubSub) pattern to handle state updates. When a user interacts with the UI, actions can be dispatched (published) to update the state. Views can listen (subscribe) to those updates and modify the UI accordingly. Actions in redux are plain JavaScript objects with a <strong>type</strong> attribute containing a unique key for the action and a <strong>payload</strong> attribute containing the data.</p><p>Actions can’t update the state themselves, they simple tell the reducers what they should update. All updates are handled by reducers which are pure functions taking the state and action as inputs, applying the action to the state and returning the new state.</p><p>Built upon this concepts are the three core principles of Redux:</p><h4><strong>Single source of truth</strong></h4><p>The state of your whole application is stored in an object tree within a single store.</p><h4>State is read-only</h4><p>The only way to change the state is to emit an action, an object describing what happened.</p><h4>Changes are made with pure functions</h4><p>To specify how the state tree is transformed by actions, we write pure reducers (pure = functions that returns a given output for every given input).</p><p>It might seem complex at first but after creating or own implementation of redux I hope everything will be a bit more clear. Now, let’s apply what we’ve learned in code!</p><h3>Creating Redux</h3><p>We will start off with the full implementation of our Redux store and then break it down into pieces. I think this might come as a surprise for many (it certainly did for me), but we can implement a fully working redux store with 16 lines of code (!).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/00fa683f06d1f5a5a15f81a1f415f22f/href">https://medium.com/media/00fa683f06d1f5a5a15f81a1f415f22f/href</a></iframe><p>If we simplify the above, the store looks something like this:</p><pre>const store = {<br>  state: {}, // a plain object holding our state<br>  listeners: [], // an array containing all listeners (subscribers)<br>  getState: () =&gt; {}, // function to get the current state<br>  dispatch: () =&gt; {}, // function to dispatch updates to the state<br>  subscribe: () =&gt; {}, // subscribe function to add new listeners<br>}</pre><p>In the full implementation you can see that everything is happening inside the function create store. This function takes two arguments; reducer and initialState. As mentioned before, reducers are the only ones allowed to change the state. They do so, not by mutating the state, but by returning a new state with the action applied to the previous state. The initialState argument simply lets us create a new store with some predefined data.</p><p>In our implementation state, listeners, and getState are quite self-explaining. State is simply an object holding our state. Listeners is an array holding all listeners (subscribers) and getState is a function that returns the current state. The subscribe function adds new subscribers to the array of listeners. A subscriber is a function that will be executed every time the state is updated.</p><p>That’s it for the store, if you remember our architecture we just need to add a reducer, actions and views to have a fully working implementation.</p><h4>Reducer and actions</h4><p>It is in the reducer function that most of the magic happens. Reducer takes the previous state and an action as input (we already know that actions are objects containing a type and a payload). The reducer function will apply the corresponding changes for the action and return a new state. A reducer typically looks something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/29bed7a4ba6c347fee3fcb579e0cfe80/href">https://medium.com/media/29bed7a4ba6c347fee3fcb579e0cfe80/href</a></iframe><p>In this example we have two types of actions to modify the state: ADD and SUBTRACT. If we dispatch an action with the type of “add” the reducer will add the number in the payload to the number in the current state. The reducer then returns the updated state. If we dispatch any other action than add or subtract, the reducer will fallback to the default and return the current state without modifications.</p><p>The second part of our dispatch function is the listeners. On every state change, we iterate through the array of listeners and execute each one. By doing this we make sure that all views that are subscribing can update accordingly as the state changes.</p><pre>store.listeners.forEach((listener) =&gt; listener());</pre><p>That is all for our createStore function. Now we will do a full implementation containing all the building blocks; <strong>actions, reducers, store </strong>and <strong>views.</strong></p><h4>View</h4><p>We have already created our store and reducer above. The view in our example is a counter that shows a number and have buttons to perform the actions of adding or subtracting from that number. We achieve this with some simple DOM-elements and a subscriber to our store.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b70620b58db1dca1fedb1372fcd6e28/href">https://medium.com/media/5b70620b58db1dca1fedb1372fcd6e28/href</a></iframe><p>And finally we have the actions and dispatch. This is also mapped to our view — if the user clicks the add or subtract button the corresponding action should be dispatched.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c4f137b6ebcee52a8d5b6941c0bf2c3/href">https://medium.com/media/7c4f137b6ebcee52a8d5b6941c0bf2c3/href</a></iframe><p>All together we get something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d98acb6082ffa227d433ad026d4f04a1/href">https://medium.com/media/d98acb6082ffa227d433ad026d4f04a1/href</a></iframe><p>This is not a production ready implementation but it can get you quite far. The redux library covers a lot of edge cases and have a few more advanced features. Especially if you use its react wrapper that comes with hooks and other nice functionalities.</p><h3>Wrap up</h3><p>Some tools and libraries that can seem daunting at first are actually quite simple when you break them down. Hopefully this article have given you a basic understanding how Redux works. When you feel ready it might be time to experiment with some of the more advanced features of redux or look into popular design patterns like duck and similar.</p><ul><li><a href="https://redux.js.org/">Redux - A JS library for predictable and maintainable global state management | Redux</a></li><li><a href="https://github.com/erikras/ducks-modular-redux">GitHub - erikras/ducks-modular-redux: A proposal for bundling reducers, action types and actions when using Redux</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a28d4e47b76f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/knowit-development/recreating-redux-a28d4e47b76f">Recreating redux</a> was originally published in <a href="https://medium.com/knowit-development">Knowit Development</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>