<?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 Teddy Cleveland on Medium]]></title>
        <description><![CDATA[Stories by Teddy Cleveland on Medium]]></description>
        <link>https://medium.com/@tcclevela?source=rss-1b02512490d1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*OpxKGMkYcZj6h0DR.jpeg</url>
            <title>Stories by Teddy Cleveland on Medium</title>
            <link>https://medium.com/@tcclevela?source=rss-1b02512490d1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 04:48:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tcclevela/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[Building a simple redux library]]></title>
            <link>https://medium.com/@tcclevela/building-a-simple-redux-library-44ce4f004822?source=rss-1b02512490d1------2</link>
            <guid isPermaLink="false">https://medium.com/p/44ce4f004822</guid>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Teddy Cleveland]]></dc:creator>
            <pubDate>Tue, 01 Aug 2017 03:01:06 GMT</pubDate>
            <atom:updated>2017-08-01T03:11:02.177Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LkhRPeVcXy4BV_vPoPts0g.jpeg" /><figcaption>Just as these pipes are composed within one another, so too will you elegantly compose your reducers.</figcaption></figure><p>A few months ago I kept running into a problem over and over again: how can I abstract away request-state logic and build an simple react-component interface for managing request-related UI. In essence, this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5910a3e1b98841bedf4bfa4abb39d57f/href">https://medium.com/media/5910a3e1b98841bedf4bfa4abb39d57f/href</a></iframe><p>The application I was writing already used redux, so I figured an abstraction at the reducer level would be a good starting point. eventually I found myself managing an excessive amount of code dealing with middleware, intermediary actions, and a large, overly complex reducer. It turns out that the solution to my problem was much simpler than I initially thought 😥. Here are the main takeaways I got from this little project.</p><h4>1. Skip the middleware (you probably can)</h4><p>Unless your redux library needs to interact with a third party tool or do some sort of async stuff, you really won’t need middleware. My first mistake was assuming that middlewares were the foundation of any good redux library. I was thinking, “But wait! The application is dispatching actions containing different data than what my library’s reducer is expecting.” But because reducers are functions, you can pretty much do any and all necessary transformation (note,<strong> not side effects</strong>) before committing that data to the store. One easy way to do this is to write a configurable reducer creator, which allows the application to define what actions the library sees, and deal with mapping those actions to a format the library expects. This is essentially a reducer-returning API.</p><p>In the case of my example, the reducer creator takes a function that filters actions, and then maps them to objects containing a type, id, and error, and uses those data to update the internal state (this is similar to what a lot of people use middlewares for when they map actions to other actions that their reducer expects). On top of this, you can build higher-level reducers that do what a middleware might have done, like extract, transform, and add information to incoming actions. Who likes mixing in middlewares anyway…</p><h4>2. Keep your abstractions to yourself</h4><p>I’m a fanboy for selectors (:obligatory-grain-of-salt:), but for good reason (I think). There isn’t really any point in building a redux-library if you’re just going to force the consumer application to know how to fetch data from the library’s state or make the application put the library state in a specific part of its own store, so build a selector interface.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d742dd01cea9187ca796d89112a9ada/href">https://medium.com/media/9d742dd01cea9187ca796d89112a9ada/href</a></iframe><p>The idea here is to allow the application to mix in the library reducer wherever it wants, as long as it tells the library how to get to it. This kills two birds with one stone, because the library doesn’t break if the application puts its store somewhere unexpected, and it protects whatever abstractions it makes by defining an API for accessing its state. You could use the above example like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e64ba1fdd46fa7d158ccee2ca0dc54d4/href">https://medium.com/media/e64ba1fdd46fa7d158ccee2ca0dc54d4/href</a></iframe><p>Given createSelector, the application can now get whatever information the library has promised to provide when it connects to the store:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d56944bf019dffc63135b3dddfa7ff72/href">https://medium.com/media/d56944bf019dffc63135b3dddfa7ff72/href</a></iframe><p>Not only is the library easy to integrate, but it’s also easy to use, because the application implementor doesn’t have to think about how the library’s internal state works, she basically just has to host the library state and can do so wherever she wants. This example may seem specific, but this principle really extends to any redux library. Libraries should be exporting extensible selector infrastructure, PropTypes for whatever the final selectors return, and any reasonable interfaces to prevent leaky abstractions.</p><h4>3. (which is kind of the same as 1.) KISS</h4><p><a href="https://en.wikipedia.org/wiki/KISS_principle">https://en.wikipedia.org/wiki/KISS_principle</a></p><p>I get that it’s a hackneyed reference at this point, but there’s nothing wrong with a super-small library. The beauty of reducers is that they’re just functions, which means they’re easy to compose and reuse if designed well. A redux library should really just do one thing, and keeping things simple makes it easier to drop excessive dependencies.</p><p>Anyway, these are just a few of my favorite things I found to be useful when working with redux, and a lot of these ideas are just plain useful for coding in general. If you think I’m wrong or disagree with this stuff, let me know!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=44ce4f004822" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>