<?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 Amit Dhaka on Medium]]></title>
        <description><![CDATA[Stories by Amit Dhaka on Medium]]></description>
        <link>https://medium.com/@amitdhaka_web?source=rss-c0563b4a1bcb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Hu6xGTDKHBkzxow68Zh88g.jpeg</url>
            <title>Stories by Amit Dhaka on Medium</title>
            <link>https://medium.com/@amitdhaka_web?source=rss-c0563b4a1bcb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 10:46:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amitdhaka_web/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[React UnStated — Aggregation of state between different state containers]]></title>
            <link>https://medium.com/justfrontendthings/react-unstated-aggregation-of-state-between-different-state-containers-6b37fade7563?source=rss-c0563b4a1bcb------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b37fade7563</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[unstated]]></category>
            <category><![CDATA[aggregation]]></category>
            <dc:creator><![CDATA[Amit Dhaka]]></dc:creator>
            <pubDate>Thu, 05 Apr 2018 10:32:24 GMT</pubDate>
            <atom:updated>2018-06-11T11:22:19.990Z</atom:updated>
            <content:encoded><![CDATA[<p>I recently started using react <a href="https://github.com/jamiebuilds/unstated">unstated</a>. I took help of this quick start <a href="https://medium.com/react-native-training/unstated-the-setstate-of-react-state-management-8ce47b240e6d">guide</a>. It took me a couple of readings to understand the wiring of unstated containers to React components and how to use it effectively to manage the state of a component. I put it to practical use in a personal project. If you are new to React or React-unstated, refer the guide below before proceeding further.</p><p><a href="https://medium.com/react-native-training/unstated-the-setstate-of-react-state-management-8ce47b240e6d">https://medium.com/react-native-training/unstated-the-setstate-of-react-state-management-8ce47b240e6d</a></p><p>React Unstated was a breeze to use and it simplified my components by pulling out all the state related logic into unstated containers. But soon, I realized changing state of one unstated container from another container was not straightforward.</p><p>I had kept auth state and database interactions in separate containers and wanted to make a database read call as soon as user auth was successful. To overcome this, I set a flag in auth state and made a check in my React Component to make the database read. But, I realized by making these checks I’ll end up writing state logic in my React components.</p><p>Aggregation was a problem for me now. After giving some thought, I realized that Unstated provides a way to inject the container objects. Using inject approach, I added a bind method to the container which can be used to influence the state of the component we bind with.</p><p>Here’s an example:</p><pre>import { Provider, Subscribe } from &#39;unstated&#39;;<br>import AuthContainer from &#39;./state/AuthContainer&#39;;<br>import DBContainer from &#39;./state/DBContainer&#39;;</pre><pre>// AuthContainer is written below</pre><pre>const authContainer = new AuthContainer();<br>const dbContainer = new DBContainer();</pre><pre>// aggregation of state<br>authContainer.bindDB(dbContainer);</pre><pre>export default class App extends Component {<br>  render() {<br>    return (<br>      &lt;Provider inject={[authContainer, dbContainer]}&gt;<br>        &lt;Subscribe to={[AuthContainer, DBContainer]}&gt;<br>        { (auth, db) =&gt; {<br>          return ( *Render your application*);<br>        }}}<br>        &lt;/Subscribe&gt;<br>      &lt;/Provider&gt;<br>    );<br>  }<br>}</pre><p>I used bindDb to bind DbContainer to AuthContainer. Now, we can call the database read from AuthContainer upon successful authentication.</p><p>Here is how →</p><pre>import { Container } from &#39;unstated&#39;;</pre><pre>export default class AuthContainer extends Container { <br>  authenticate() { <br>    // call onAuthSuccess on successful authentication<br>  }<br>  onAuthSuccess() { <br>    // database read calls<br>    this.db.getAllThings();<br>  }<br>  bindDb(db) { <br>    this.db = db;<br>  }<br>}</pre><p>That’s all for now. I’ll be writing a follow-up article if there is any new pattern that I come across. Please share your feedback in the comments and share your views if you’ve used it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b37fade7563" width="1" height="1" alt=""><hr><p><a href="https://medium.com/justfrontendthings/react-unstated-aggregation-of-state-between-different-state-containers-6b37fade7563">React UnStated — Aggregation of state between different state containers</a> was originally published in <a href="https://medium.com/justfrontendthings">JustFrontendThings</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>