<?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 Nick Matenaar on Medium]]></title>
        <description><![CDATA[Stories by Nick Matenaar on Medium]]></description>
        <link>https://medium.com/@nicktho?source=rss-6725db0539------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*x2umQtgeG_KaC1VQ.jpg</url>
            <title>Stories by Nick Matenaar on Medium</title>
            <link>https://medium.com/@nicktho?source=rss-6725db0539------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 03:05:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nicktho/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[Recreating Redux — Behind the magic curtain.]]></title>
            <link>https://medium.com/@nicktho/recreating-redux-behind-the-magic-curtain-c21edd542c9c?source=rss-6725db0539------2</link>
            <guid isPermaLink="false">https://medium.com/p/c21edd542c9c</guid>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Nick Matenaar]]></dc:creator>
            <pubDate>Wed, 06 Apr 2016 02:31:22 GMT</pubDate>
            <atom:updated>2016-06-20T21:27:27.617Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JDQdEzKVxxvDh1EV0Rem1Q.png" /></figure><p>Released mid 2015, <a href="https://github.com/reactjs/redux/">Redux</a> has quickly become one of the most popular libraries to manage state in a React application. Libraries such as <a href="https://github.com/reactjs/react-redux">React-Redux</a>, <a href="https://github.com/reactjs/react-router-redux">React-Router-Redux</a> and middleware such as <a href="https://github.com/gaearon/redux-thunk">redux-thunk</a> make it easy to quickly create single page applications with react and redux, though can crowd and overwhelm someone just beginning to explore these ideas.</p><p>Throughout this article we’ll be taking a step back, to implement a simplified version of the core of redux, with the intention of developing a deeper understanding of the underlying concepts behind the library.</p><h3>The Core</h3><p>The idea behind Redux is quite simple:</p><ul><li>All the state of your application lives in a single <strong>store</strong>.</li><li>To change the state of your application, you must <strong>dispatch</strong> an <strong>action</strong>.</li><li>Actions transform the state through the use of a <strong>reducer</strong>.</li><li>When an action is dispatched, the store lets <strong>subscribers</strong> know the state has changed.</li></ul><p>I’ll be implementing these using ES2015, transpiling with <a href="https://babeljs.io/">Babel</a> and testing with <a href="https://mochajs.org/">Mocha</a> and <a href="http://chaijs.com/">Chai</a>. All code below is <a href="https://github.com/Nicktho/recreating-redux">available on Github.</a></p><h4>All the state of your application lives in a single <strong>store</strong>.</h4><p>Redux exports the function <em>createStore</em>, which, as the name implies, creates and returns our store, the center of a redux application.</p><p>We’ll start here.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2baf68d30e2045dad829262ad2c94106/href">https://medium.com/media/2baf68d30e2045dad829262ad2c94106/href</a></iframe><p><em>createStore</em> takes two arguments, <em>reducer</em> and <em>initialState. </em>Let’s store the initialState as our current state for now.</p><p>We’ll need a way to access our state, the store supplies us with a <em>getState</em> method to do just that. This is also a good opportunity for us to write our first test.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b873287a6c1f2ffc7b275dbf601fe67/href">https://medium.com/media/3b873287a6c1f2ffc7b275dbf601fe67/href</a></iframe><p>Our state tree in this and future tests will resemble a simple note taking application. Our <em>reducer</em> here returns any argument passed into it, as we won’t be focusing on that just yet.</p><p>Let’s implement our method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7801eab0c9ed74e4c167c24f8aa5a557/href">https://medium.com/media/7801eab0c9ed74e4c167c24f8aa5a557/href</a></iframe><p>Great, now we can access our store’s state, but to change it we’ll need to dispatch an action.</p><h4>To change the state of your application, you must <strong>dispatch</strong> an <strong>action, </strong>which transforms the state through the use of a <strong>reducer</strong>.</h4><p>Let’s start out with another test</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/63415099e2683fe7a4a26117d22f1663/href">https://medium.com/media/63415099e2683fe7a4a26117d22f1663/href</a></iframe><p>First we’re creating our notes reducer and an action creator, <em>addNote</em>, if you would like to learn more about reducers and action creators, the <a href="http://redux.js.org/docs/basics/Reducers.html">Redux Documentation</a> is an excellent resource<em>.</em></p><p>In our test, we’ll create our store, dispatch our action with a new payload and check to see if the store was successfully updated.</p><p>Let’s implement it!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/06fc8d89c370e2a1029cf48a88df653a/href">https://medium.com/media/06fc8d89c370e2a1029cf48a88df653a/href</a></iframe><p>Notice we’re instantly dispatching an <em>INIT </em>action. This will flow throw all our reducers and return the default state, setting up our store.</p><p>Great, almost there, the last step is to notify the store’s subscribers that a change has been made.</p><h4>When an action is dispatched, the store lets <strong>subscribers</strong> know the state has changed.</h4><p>We’ll need a way to subscribe to our store, let&#39;s write a test.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8c19912ab6a1450a44975b1625d10b8/href">https://medium.com/media/f8c19912ab6a1450a44975b1625d10b8/href</a></iframe><p>Since our listeners will be private, we’ll need update our dispatch method to notify listeners to pass this test.</p><p>We’ll go ahead and implement <em>subscribe</em>, which will add the passed function to an array of listeners, also, we’ll update our dispatch to let our listeners know something has changed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/90f7c925238c08d47f6962e858ca95ef/href">https://medium.com/media/90f7c925238c08d47f6962e858ca95ef/href</a></iframe><p>Believe it or not, we’ve just implemented a simplified version of Redux!</p><p>Let’s look at one more function the redux package gives us, <em>combineReducers.</em></p><h3>combineReducers, the helper.</h3><p><em>combineReducers </em>is, in my opinion, the most beneficial helper method supplied by the Redux package. Essentially, it allows you to split up your reducer in to multiple reducers for each part of your state tree. Before continuing, I recommend reading through the <a href="http://redux.js.org/docs/basics/Reducers.html">official documentation for reducers</a> if you have not done so already.</p><p>As usual, we’ll start with a test.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69a96fbc9caac8d5a112d96b54cb8be5/href">https://medium.com/media/69a96fbc9caac8d5a112d96b54cb8be5/href</a></iframe><p>As shown in the official documentation, <em>combineReducers</em> takes an object with its keys as parts in the state tree and it’s values their reducers. It then returns a single reducer, ready to pass into <em>createStore. </em>We’ll test this functionality by combining two reducers and passing through one action to see if the state updates correctly.</p><p>Let’s take a look at an implementation of this function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/452fbab56902df8b05cec07843b30a30/href">https://medium.com/media/452fbab56902df8b05cec07843b30a30/href</a></iframe><p>What we’ve created here is essentially a higher-order reducer. We’ll return a new reducer, that when called, loops through all passed in reducers and calls them with the current state and action. These results are bundled in an object with each key, the key of the reducer in the initially passed in object, aka, our state tree!</p><h3>Wrapping up</h3><p>I find learning through recreation produces a far deeper understanding for myself and I hope it did for you too. Behind the now countless libraries and middlewares built on top of it, Redux is quite a simple library, but a powerful idea and architecture.</p><p><a href="https://github.com/Nicktho/recreating-redux">The source for the complete project is available on GitHub.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c21edd542c9c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>