<?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 Eric Nagy on Medium]]></title>
        <description><![CDATA[Stories by Eric Nagy on Medium]]></description>
        <link>https://medium.com/@enagy27?source=rss-222e40df307a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*05jYJmLk3Iwr99UYXuKYZQ.jpeg</url>
            <title>Stories by Eric Nagy on Medium</title>
            <link>https://medium.com/@enagy27?source=rss-222e40df307a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 27 May 2026 23:11:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@enagy27/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[Demystifying Redux middleware: Async code and side-effects]]></title>
            <link>https://medium.com/@enagy27/demystifying-redux-middleware-async-code-and-side-effects-8155d973331e?source=rss-222e40df307a------2</link>
            <guid isPermaLink="false">https://medium.com/p/8155d973331e</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[code]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Eric Nagy]]></dc:creator>
            <pubDate>Sun, 06 May 2018 01:29:31 GMT</pubDate>
            <atom:updated>2018-05-07T00:23:36.739Z</atom:updated>
            <content:encoded><![CDATA[<p>There are a lot of articles out there talking about why you should use Redux <a href="https://redux.js.org/advanced/middleware"><strong>middleware</strong></a> more often. And they are 100% right. <strong>Middleware</strong> can be used to do all sorts of useful things like managing asynchronous calls and handling side-effects.</p><p>But while <strong>middleware</strong> is easy enough to consume, a functional understanding of them is commonly overlooked when starting out with Redux. Inevitably this leads to more confusion, and limits the utility of Redux.</p><p>This article is divided into two main parts:</p><ol><li>We will discuss what <strong>middleware</strong> is and how it gets hooked up to the <strong>store</strong>, and we’ll go through an example of how Redux Thunk is implemented and consumed.</li><li><em>(Optional) </em>We will look more closely at how <strong>middleware</strong> is wired up and consumed internally by Redux.</li></ol><p>So let’s dive in and see what <strong>middleware</strong> really is under the hood.</p><h3>Part One: What is middleware?</h3><p>Redux <strong>middleware</strong> is a function that sits between a <strong>dispatch</strong> and a <strong>reducer</strong> function. It does this by wrapping and <strong>enhancing </strong>the <strong>dispatch</strong> function.</p><p><strong>Middleware</strong> is passed into to the store on creation using <strong>applyMiddleware(…middlewares)</strong>. This will be discussed in more detail later on</p><blockquote>Note: <strong>…middlewares</strong> refers to the use of the <strong>spread operator</strong>, which accepts any number of inputs and interprets them as an array of inputs.</blockquote><p><strong>Middleware</strong> are written to be composable, meaning that multiple <strong>middleware</strong> functions can be run one after the other, where the output of one <strong>middleware</strong> is forwarded to the <strong>next</strong>. At the end of this chain, an <strong>action</strong> must be produced to be sent to the <strong>reducer</strong>.</p><p><strong>Middleware</strong> can do a few things:</p><ul><li>Transform a <strong>dispatched</strong> <strong>action</strong> into another <strong>action</strong></li><li>Forward an <strong>action</strong> to the <strong>next</strong> <strong>middleware</strong></li><li>Call <strong>dispatch</strong> with a new <strong>action</strong></li><li>Run some code, such as logging</li></ul><h3>Example: The simplest middleware</h3><p>The simplest possible <strong>middleware</strong> is one that simply receives an <strong>action</strong> and forwards it to the <strong>next</strong> <strong>middleware</strong>. This is written as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3630b6fbaa9105793a54764ef7d4320f/href">https://medium.com/media/3630b6fbaa9105793a54764ef7d4320f/href</a></iframe><p>Before you run screaming, let’s talk about what’s happening here. The intimidating part here is called <strong>currying</strong>. This is a functional programming concept where a function returns another function. Here there is a function which returns a function, which returns a function. It’s a lot to look at in so little code, so let’s break it down.</p><h4>Next(action)</h4><p>We’re familiar with <strong>store</strong> and <strong>action</strong>, but what is <strong>next</strong>? As we’ve discussed, <strong>next</strong> is a function that forwards an <strong>action</strong> to the <strong>next</strong> <strong>middleware</strong>. Notice its function signature is the same as <strong>dispatch</strong>.</p><p>We can see this simply as a <strong>dispatch</strong> function that <strong>dispatches</strong> to our <strong>next middleware</strong> as opposed to going back to our top level <strong>store</strong>, which would bring it back through our <strong>middlewares</strong>.</p><p>In practice this means that applying a <strong>middleware</strong> is an <strong>enhancer</strong> on a <strong>dispatch</strong> call.</p><h3>Example: Applying our simple middleware</h3><p>To apply our <strong>middleware</strong>, Redux provides <strong>applyMiddleware(…middlewares)</strong>. We will discuss what’s happening here under the hood in a bit. Consider the following application, using the <strong>middleware</strong> defined above:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4cc75f3bf06238f370ccfe84b6b6c847/href">https://medium.com/media/4cc75f3bf06238f370ccfe84b6b6c847/href</a></iframe><h3>Redux Thunk: decomposed</h3><p>Let’s take a look at another simple, but widely used, piece of <strong>middleware</strong>, <a href="https://github.com/gaearon/redux-thunk"><strong>Redux Thunk</strong></a>.</p><p><strong>Redux Thunk</strong> is simple: it allows us to <strong>dispatch</strong> a function as opposed to <strong>dispatching</strong> an <strong>action</strong>. This is useful as it gives us the ability to do two main things:</p><ul><li>Execute asynchronous code. Whereas the rest of the Redux workflow is entirely synchronous, functions executed inside <strong>middleware</strong> may be asynchronous</li><li><strong>Dispatch</strong> side-effect <strong>actions</strong>. <strong>Redux Thunk</strong> provides the functions passed to it a reference to <strong>dispatch(action)</strong> and <strong>getState()</strong></li></ul><p>Let’s take a look at a simplified implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a8f95698b4452a9db724040e11c6cc80/href">https://medium.com/media/a8f95698b4452a9db724040e11c6cc80/href</a></iframe><p>That’s it! All <strong>Redux Thunk middleware</strong> does is:</p><ol><li>Check whether it is passed an <strong>action</strong> or a function</li><li>If it receives a function, that function is invoked</li><li>If it receives an <strong>action</strong>, it does nothing and forwards it to the <strong>next middleware</strong></li></ol><h3>Example: Consuming Redux Thunk</h3><blockquote>Note: This example uses <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function">async/await</a></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6438d7e2a3a06d229a977073a856dd3e/href">https://medium.com/media/6438d7e2a3a06d229a977073a856dd3e/href</a></iframe><p>In this example, we are making a call to a fake Reddit API for posts. The following occurs:</p><ol><li><strong>getPosts(subreddit)</strong> creates a function that will make our API call</li><li>This function is passed to <strong>dispatch(action)</strong></li><li>Because this is a function, it is invoked by <strong>Redux Thunk</strong></li><li>The API call is executed</li><li>Success or failure <strong>actions</strong> are <strong>dispatched</strong></li></ol><p>This is the power of <strong>middleware</strong>. With relative ease, we are able to invoke an API call which is executed asynchronously, and which <strong>dispatches</strong> side-effect <strong>actions</strong> automatically depending on whether it succeeds or fails.</p><p>At this point, you know everything you need in order to get started building and consuming your own <strong>middleware</strong>! In the rest of this article, we’ll dive into some of the details of the Redux framework and see how Redux consumes our <strong>middlewares</strong>.</p><h3>Part Two: How exactly does middleware enhance our dispatch?</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1e7191e5440e1fb637cd1ee22c112f6/href">https://medium.com/media/e1e7191e5440e1fb637cd1ee22c112f6/href</a></iframe><p>This is equivalent to:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/02783af52959ba4c24a1a96bba74b87a/href">https://medium.com/media/02783af52959ba4c24a1a96bba74b87a/href</a></iframe><p>This is the underlying concept of how Redux uses <strong>middleware</strong> to wrap or <strong>enhance</strong> <strong>dispatch</strong>.</p><h3>Example: Multiple middlewares</h3><p>Now that we’ve got one <strong>middleware</strong> in place, it’s useful to look at a composition of <strong>middlewares</strong>. Consider the following <strong>middlewares</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b398bfc994c283ce3d60812d7ed9298a/href">https://medium.com/media/b398bfc994c283ce3d60812d7ed9298a/href</a></iframe><p>Let’s try a naïve attempt at applying these <strong>middlewares</strong> to transform our <strong>dispatch</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2352d4cffd0c182977b1a129639e3c17/href">https://medium.com/media/2352d4cffd0c182977b1a129639e3c17/href</a></iframe><p>Which can be expanded into the equivalent:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb6e697f903cb6e17c53cebadf8a6317/href">https://medium.com/media/eb6e697f903cb6e17c53cebadf8a6317/href</a></iframe><p>Ok, that’s still not exactly digestible. Let’s look at how it would work when called:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/603c857d4ea950fd942a2d6df7c872cb/href">https://medium.com/media/603c857d4ea950fd942a2d6df7c872cb/href</a></iframe><p>Can be expanded into the equivalent:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c54d840fbbdb5f04579d5a4b31b392f2/href">https://medium.com/media/c54d840fbbdb5f04579d5a4b31b392f2/href</a></iframe><p>As it turns out, our <strong>middlewares </strong>are being called in the opposite order from which they are applied. This isn’t exactly what we want.</p><p>This of course is in line with the recursive structure of applying this middleware, which can be viewed as pushing each middleware onto a stack.</p><p>The solution is simple: reverse the order we are applying our <strong>middleware</strong>.</p><h3>How does middleware application work?</h3><p>Now that we have an idea of how we could manually apply our <strong>middleware</strong>, we can take a look at the <strong>applyMiddleware(…middlewares)</strong> function provided by Redux. Here is a simplified example of that function from the Redux docs:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e10a995d49ae8d9de4d43942d240936/href">https://medium.com/media/2e10a995d49ae8d9de4d43942d240936/href</a></iframe><p>As you can see, this is doing precisely what we did above manually. First, the <strong>middlewares</strong> are shallow-copied with <strong>slice()</strong>, and are <strong>reversed()</strong> to preserve desired ordering. Then <strong>dispatch</strong> is reassigned as the output of the <strong>middlewares</strong>.</p><p>This is functionally all we need to understand in order to write our own <strong>middleware</strong>, but there are a few distinct differences between our implementation of <strong>applyMiddleware(…middlewares)</strong> and the internal implementation, as discussed in the Redux docs:</p><ul><li><strong>applyMiddleware(…middlewares)</strong> returns an <strong>enhancer</strong> function in order to ensure that <strong>middleware</strong> can only be applied once</li><li>It exposes only <strong>getState()</strong> and <strong>dispatch(action)</strong> from the <strong>store</strong></li><li><strong>store.dispatch(action)</strong> is not overridden, so we may still call it from within our <strong>middleware</strong>, which is very useful for actions which cause other side-effect actions (more on this later)</li></ul><p>The implementation of the <strong>enhancer</strong> is not important for our purposes, but if you’re curious the signature is:</p><pre><strong>const enhancer(...middlewares) =&gt; (createStore) =&gt; createStore;</strong></pre><h3>Creating your store with applied middleware</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93296fe02f72a84aaa3b27ff272329c8/href">https://medium.com/media/93296fe02f72a84aaa3b27ff272329c8/href</a></iframe><p>So there you have it! You can now apply and create your own <strong>middleware</strong> to accomplish all manner of things from making API calls to enhancing your application logging.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8155d973331e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Redux made easy]]></title>
            <link>https://medium.com/@enagy27/redux-made-easy-d939f27db377?source=rss-222e40df307a------2</link>
            <guid isPermaLink="false">https://medium.com/p/d939f27db377</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[beginner]]></category>
            <category><![CDATA[introduction]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Eric Nagy]]></dc:creator>
            <pubDate>Sat, 05 May 2018 08:30:52 GMT</pubDate>
            <atom:updated>2018-05-07T00:21:51.978Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’re brand new to <a href="https://redux.js.org/">Redux</a>, or maybe have never done web development, it can be a pretty confusing place. Actions, Reducers, Middleware, Pure Functions, Store, Dispatch, and the list goes on- buzzwords abound.</p><p>I feel your pain. A couple years ago I made the switch from C#/.NET to the world of React/Redux, and I was excited to get started. React was tough at first, but there was a wealth of resources for all skill levels. Then came Redux, and looking back it’s hard to imagine getting started.</p><p>So, in this article I’m going to break down Redux into the simplest terms I can. In fact, we won’t be using any React. Let’s get started!</p><h3>What is Redux?</h3><p>Redux is a place to <strong>store</strong> your application state. That’s pretty much it! At the end of the day, Redux is a wrapper for an object that represents your current application state. This wrapper encapsulates your state to ensure that it is only altered or read a certain way.</p><h3>Terms</h3><h4><a href="https://redux.js.org/basics/actions">Action</a></h4><p>A plain object with a <strong>type</strong> string and a <strong>payload</strong>. The payload can be whatever you like, but is typically and object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1524f1ed31473b28f79168501c6c3de6/href">https://medium.com/media/1524f1ed31473b28f79168501c6c3de6/href</a></iframe><h4><a href="https://redux.js.org/basics/store"><strong>Store</strong></a></h4><p>An encapsulation of your application state, which provides three main functions: <strong>getState()</strong>, <strong>dispatch(action)</strong>, and <strong>subscribe(listener)</strong>.</p><p><strong>getState()</strong> is used to read the current state of the <strong>store.</strong></p><p><strong>dispatch(action)</strong> is used to change the state of the <strong>store</strong> using an <strong>action</strong>.</p><p><strong>subscribe(listener)</strong> is used to listen for changes to the <strong>store</strong>. The listener is a callback with no arguments and no return value. <strong>subscribe(listener)</strong> returns an <strong>unsubscribe()</strong> function that can be used to remove the <strong>listener</strong>.</p><p>In order to determine how the state of the store will be changed by an <strong>action</strong>, the store is constructed with a <strong>reducer</strong> function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8aec56f10e18921407ce125fb52f956/href">https://medium.com/media/b8aec56f10e18921407ce125fb52f956/href</a></iframe><h4><a href="https://redux.js.org/basics/reducers"><strong>Reducer</strong></a></h4><p>A function that specifies how an <strong>action</strong> that is <strong>dispatched</strong> to the <strong>store</strong> will modify the application state. This function takes an input state and <strong>action</strong> and returns a new state. This function must be completely synchronous.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1504bcd522faf973a158f2bb00c9a26/href">https://medium.com/media/e1504bcd522faf973a158f2bb00c9a26/href</a></iframe><h4><strong>Important</strong></h4><p>Notice that in the example above the <strong>reducer</strong> function does not modify its input state (or <strong>action</strong>), and instead returns a copy. This is crucial in Redux, and illustrates two functional programming concepts:</p><ol><li>Immutability: the state is not modified, but instead is replaced by an entirely new object. This makes checking for updates much easier as only a reference check is required.</li><li>Pure function: the <strong>reducer</strong> function produces no side-effects (unintended changes to external state).</li></ol><p>Another thing to note is that a <strong>reducer</strong> may receive a state of <strong>undefined</strong>. In order to handle this, we can use default value assignment:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95198ea9475a3aae034fdff2edc54f80/href">https://medium.com/media/95198ea9475a3aae034fdff2edc54f80/href</a></iframe><p>In this case, when the <strong>reducer</strong> above is called with a state of <strong>undefined</strong>, the <strong>reducer</strong> will instead interpret the state as an empty object.</p><h3>Example - Putting it all together</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b6d2ef9f0695493f0b78137e27244d9/href">https://medium.com/media/5b6d2ef9f0695493f0b78137e27244d9/href</a></iframe><p>Ok, great! At this point you should have a good baseline for Redux, but as with most frameworks, sometimes conventions add confusion. Let’s talk about a few conventions that can make working in Redux more elegant.</p><h3>Action Creators</h3><p>These are functions that return an <strong>action</strong>. It really is that simple- a function that returns an object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/81ff6059f9c078667b8bbc32af0262f1/href">https://medium.com/media/81ff6059f9c078667b8bbc32af0262f1/href</a></iframe><h3>Combining Reducers</h3><p>So far our examples have been with a shallow store, making our <strong>reducers</strong> simple and straight-forward. This does not work for bigger applications, where we might want to track our state by a composition of sub-states. Take this <strong>store</strong> shape, for example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6bf29e0f675877255194483fda36af57/href">https://medium.com/media/6bf29e0f675877255194483fda36af57/href</a></iframe><p>We could, of course, write the logic to handle updates to this state, but it would be easier to manage the properties of this state with their own individual <strong>reducers</strong>. Luckily, we can do exactly that. In the following example we will create a <strong>fredReducer</strong> and a <strong>georgeReducer</strong>, and we will write them completely in isolation of one another.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1fe92b633ef0454dd82708bb30736faf/href">https://medium.com/media/1fe92b633ef0454dd82708bb30736faf/href</a></iframe><p>Now, all that’s left to do is combine them. Lucky for us there’s a function <strong>combineReducers</strong> provided in Redux. With it, we can provide an object with the keys we would like to use in our state, and whose values are our corresponding <strong>reducer</strong> functions. From <strong>combineReducers</strong>, we will get a function which we can provide to <strong>createStore(reducer, initialState)</strong> as our top level <strong>reducer</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3774b58bed949c5430265403ba27b500/href">https://medium.com/media/3774b58bed949c5430265403ba27b500/href</a></iframe><p>Well, there you have it! Those are the basics of Redux. I hope this has been helpful in removing some of the confusion that clouds this framework, because I think it’s a truly great paradigm.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d939f27db377" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>