<?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 Ilja Satchok on Medium]]></title>
        <description><![CDATA[Stories by Ilja Satchok on Medium]]></description>
        <link>https://medium.com/@ifours?source=rss-d95b581610b5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ZRbokGDTXe-2RIdKy0wu9w.jpeg</url>
            <title>Stories by Ilja Satchok on Medium</title>
            <link>https://medium.com/@ifours?source=rss-d95b581610b5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 10:48:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ifours/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[The elegance of decorated Redux]]></title>
            <link>https://medium.com/cloudboost/the-elegance-of-decorated-redux-b0bd7e2de16a?source=rss-d95b581610b5------2</link>
            <guid isPermaLink="false">https://medium.com/p/b0bd7e2de16a</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[components]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Ilja Satchok]]></dc:creator>
            <pubDate>Tue, 12 Dec 2017 09:55:52 GMT</pubDate>
            <atom:updated>2018-02-05T18:02:45.092Z</atom:updated>
            <content:encoded><![CDATA[<p>I’m excited to share with you one very simple but powerful pattern that I discovered for myself during the year.</p><p>The beauty of Redux is that we can separate state from component and get access to it at any part of your application. Be wise and <a href="https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367">don’t move everything to redux</a>.</p><p>You’d better know about different types of component. <a href="https://medium.com/u/a3a8af6addc1">Dan Abramov</a> had written great <a href="https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0">article</a> about Presentational and Container Components. Many developers call them in different ways but the concept is the same: <strong>presentational </strong>components are concerned with <em>how things look, </em><strong>container </strong>components are concerned with <em>how things work.</em></p><p>With the time passed you can discover more types of components like <a href="https://medium.com/u/3cb24422649f">Mike Grabowski</a> did with <strong>producers</strong> <a href="https://youtu.be/_awnyW-9aJ8?list=LLeUd_lk_quScO8L2qdWfZgg&amp;t=1843">here</a>. Usually they are all <strong>container </strong>components that solve specific problems well.</p><p>When you start to introduce redux to your project, the following code is pretty common to implement:</p><pre>// containers/CurrentUser.js</pre><pre>import { connect } from &#39;redux&#39;;</pre><pre>class UserContainer extends Component { … }</pre><pre>const mapStateToProps = (state, { userId }) =&gt; ({<br>  userName: getUserName(state, userId),<br>});<br>const mapDispatchToProps = () =&gt; {...}</pre><pre>const CurrentUserContainer= connect(<br>  mapStateToProps,<br>  mapDispatchToProps<br>)(UserContainer);<br><br>export default CurrentUserContainer;</pre><p>I prefer using decorators, because it allows me to write elegant and maintainable code.</p><pre>@connect({<br>  mapStateToProps,<br>  mapDispatchToProps<br>})<br>export default CurrentUserContainer extends Component { … }</pre><p>Here we have got one to one mapping between container component and mapStateToProps ,mapDispatchToProps functions.</p><p>However, CurrentUserContainer is not the only one component that will need userName, so in another container we can find same subset of redux state passing down to react.</p><pre>// containers/Playlist.js</pre><pre>const mapStateToProps = (state, { userId, playlistId }) =&gt; ({<br>  userName: getUserName(state, userId),<br>  tracks: getPlaylistTracks(state, playlistId),<br>});</pre><p>It’s common practice to move duplicate code to separate file and reuse it. So why not to reuse react-redux <strong>connects</strong>? They can be looked like so:</p><pre>// connects/user.js</pre><pre>import { connect } from &#39;react-redux&#39;;</pre><pre>const mapStateToProps = (state, { userId }) =&gt; ({<br>  userName: getUserName(state, userId),<br>});</pre><pre>const mapDispatchToProps = () =&gt; {...};</pre><pre>export default function use(Component) {<br>  return connect(<br>    mapStateToProps,<br>    mapDispatchToProps,<br>  )(Component);<br>};</pre><pre>// will need this later<br>use.mapStateToProps = mapStateToProps;<br>use.mapDispatchToProps = mapDispatchToProps;</pre><p>Now we can wrap any container that needs user data from redux store:</p><pre>import UseUser from &#39;connects/user&#39;;</pre><pre>@UseUser<br>export default class extends Component {...}</pre><p>The main benefit of such approach is even better separation of concerns. <strong>Connects</strong> provide the data to <strong>presentational</strong> and <strong>container</strong> components while container <strong>components</strong> now responsible only for application behavior.</p><p>The second benefit is better reusability. If you need the same set of data in different <strong>containers</strong> just reuse existing <strong>connect</strong>.</p><p>And the last but not least is testing. We can cover data layer of the application with separate unit tests.</p><h4>Combine connects</h4><p>It’s also possible to connect single container to several data points from redux store. Naively we will write:</p><pre>import UsePlaylist from &#39;connects/playlist&#39;<br>import UseUser from &#39;connects/user&#39;</pre><pre>@UsePlaylist<br>@UseUser<br>export default class extends Component {...}</pre><p>And get access to playlist and user data via props. It’s the easiest way to connect one container to several data point by reusing them. But there are two main issues: every <em>Use*</em> subscribe to redux store; if <em>user</em> entity has <em>name</em> property as well as <em>playlist</em>, the last connect will override all previous. In that case to avoid such behavior we can use combine function.</p><p>Combine will take an object with mapping and create higher order connect that will pass data from connected functions to corresponding nodes.</p><pre>@combine({ playlist: UsePlaylist, user: UseUser })<br>export default class extends Component {...}</pre><p>combine should create higher order connect based on config that you passed. In wrapped component you will be able to access data from redux in this.props.playlist and this.props.user as well as dispatch functions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/78fdc973af855f2b6e040c2a6c0f9161/href">https://medium.com/media/78fdc973af855f2b6e040c2a6c0f9161/href</a></iframe><p>It’s just a backbone, so feel free to implement more powerful combine function for your business needs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b0bd7e2de16a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cloudboost/the-elegance-of-decorated-redux-b0bd7e2de16a">The elegance of decorated Redux</a> was originally published in <a href="https://medium.com/cloudboost">CloudBoost</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>