<?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 Anton Shanyuk on Medium]]></title>
        <description><![CDATA[Stories by Anton Shanyuk on Medium]]></description>
        <link>https://medium.com/@antonshanyuk?source=rss-5aea158c11e0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*LYomaR98leVvs8Yv.</url>
            <title>Stories by Anton Shanyuk on Medium</title>
            <link>https://medium.com/@antonshanyuk?source=rss-5aea158c11e0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 21:27:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@antonshanyuk/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[PubSub as an alternative of Redux: making state-management as easy as possible]]></title>
            <link>https://medium.com/@antonshanyuk/pubsub-as-an-alternative-of-redux-making-state-management-as-easy-as-possible-43216ccc83f1?source=rss-5aea158c11e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/43216ccc83f1</guid>
            <category><![CDATA[pub-sub]]></category>
            <category><![CDATA[state-management]]></category>
            <category><![CDATA[typescript-with-react]]></category>
            <category><![CDATA[redux]]></category>
            <dc:creator><![CDATA[Anton Shanyuk]]></dc:creator>
            <pubDate>Thu, 02 Nov 2023 13:10:06 GMT</pubDate>
            <atom:updated>2023-11-02T13:10:06.057Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve stopped using Redux on my projects a couple of years ago, instead I’ve implemented a tiny RxJS-based library <a href="https://github.com/AntonShanyuk/pubsub-hooks">pubsub-hooks</a>. Here I’m going to explain the motivation and share some code samples.</p><h3>So what’s wrong with Redux?</h3><p>Really, nothing is wrong with it — its a great tool and its popularity speaks for itself. It works great for data-heavy applications and there is no good alternative if you need to handle big amount of data with complex relations between entities.</p><p>However for the easier cases Redux becomes obsolete and may bring some unreasonable complexity.</p><p>If you go to the <a href="https://redux.js.org/tutorials/quick-start">Quick Start section of the Redux documentation</a> — you will find out that the minimal setup consists of the following:</p><ol><li>Setup a Provider on a top level</li><li>Setup a store/slice of a store</li><li>Implement a reducer for each action</li><li>Dispatch action to modify state on some event</li><li>Subscribe for state updates</li></ol><p>Steps #3, #4 and #5 are decoupled by design of Redux — this is exactly the part which brings benefits for data-heavy apps but brings unreasonable complexity for easier cases.</p><p>If you think “out of box”, in the end you only need two things to manage the state of you application:</p><ol><li>Trigger a state update</li><li>Subscribe for state updates</li></ol><p>There is obviously step#0 — define the initial structure of your application state.</p><h3>Should we reinvent the wheel?</h3><p>Not really. What I’ve described as “two things of state-management” is known in software engineering for decades as <a href="https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern">PubSub pattern</a>. Moreover, in the world of micro-services there is a whole class of software know as <a href="https://en.wikipedia.org/wiki/Message_broker">Message Brokers</a> — RabbitMQ, Apache Kafka etc.</p><h3>Lets write some code: basic example</h3><p>So here is how you do pubsub with pubsub-hooks:</p><pre>import { createPubsub } from &#39;pubsub-hooks&#39;;<br><br>const pubsub = createPubsub({ counter: 0 });<br><br>const Publisher = () =&gt; {<br>  const increment = pubsub.usePub(&#39;counter&#39;, x =&gt; x + 1);<br><br>  return (<br>    &lt;button onClick={increment}&gt;Increment&lt;/button&gt;<br>  );<br>}<br><br>const Subscriber = () =&gt; {<br>  const counter = pubsub.useSub(&#39;counter&#39;);<br><br>  return (<br>    &lt;div&gt;{counter}&lt;/div&gt;<br>  );<br>}</pre><p>In the real-world apps I’d recommend to have a shared pubsub instance which is used accross the app:</p><pre>// pubsub.ts<br>import { createPubsub } from &#39;pubsub-hooks&#39;;<br><br>export const pubSub = createPubsub({ counter: 0 });</pre><pre>// someComponent.tsx<br>import { pubsub } from &#39;./pubsub&#39;;</pre><h3>Enum as a type for keys</h3><p>The code above shows the simplest way to setup and use pubsub-hooks, however in the real life you may need more than that, so lets have a look at more examples.</p><p>First of all, in the real apps there is more than one property in the store, there may be dozens or even hundreds of them. Also you need to avoid naming conflicts. For such cases you may use enum with store keys and set it explicitly for createPubsub:</p><pre>enum StateKeys {<br>  Counter = &#39;counter&#39;,<br>  ShowCounter = &#39;showCounter&#39;,<br>  DataFromApi = &#39;dataFromApi&#39;<br>};<br><br>const pubSub = createPubsub&lt;typeof initialState, StateKeys&gt;(initialState);</pre><p>This will force you to use this enum for usePub and useSub hooks:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*BkFMor0rtj3viUOah11ypw.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*yidZ34NCr1Hpg7OocqlO0Q.gif" /><figcaption>As you may notice, all the types are inferred out of box</figcaption></figure><h3>Explicit type for the storage</h3><p>In the previous example we used typeof initialState as a first type parameter for createPubsub, however often you need to specify the exact type you want to work with. In order to do this, you will have to define a type for your storage :</p><pre>const initialState = { counter: 0, showCounter: false, dataFromApi: {} };<br>enum StateKeys {<br>  Counter = &#39;counter&#39;,<br>  ShowCounter = &#39;showCounter&#39;,<br>  DataFromApi = &#39;dataFromApi&#39;<br>};<br><br>type State = {<br>  counter: number,<br>  showCounter: boolean,<br>  dataFromApi: {<br>    success: boolean,<br>  }<br>};<br><br>const pubSub = createPubsub&lt;State, StateKeys&gt;(initialState);</pre><p>In the example above you’ll get an error from typescript because we don’t have success property in dataFromApi initial state:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/809/1*P1Fzr4lgcMa3CTkmjV_qfg.png" /></figure><h3>Overload of usePub</h3><p>In the basic example we used usePub overload which accepts a callback as a second argument, this callback defines the change which you want to make in the storage. However sometimes its handy to get a callback which is bound to the specific property in your storage:</p><pre>const UpdateWithCallback =  () =&gt; {<br>  const update = pubSub.usePub(StateKeys.Counter);<br><br>  return (<br>    &lt;button onClick={() =&gt; {<br>      update(34);<br>    }}&gt;Set 34&lt;/button&gt;<br>  );<br>};</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*0tDvNlgm9aWbKf8ATkSFjw.gif" /><figcaption>Demonstration of type inference</figcaption></figure><h3>Debugging</h3><p>When using a pubsub instance in your app you may want to check the state of your app. You may achive that by defining a global variable and passing it as an optional argument of createPubsub:</p><pre>import { createPubsub, SubjectsStorage } from &#39;pubsub-hooks&#39;;<br><br>const initialState = { counter: 0 };<br><br>// extending window interface<br>declare global {<br>  interface Window {<br>    pubsub: Record&lt;string, SubjectsStorage&lt;typeof initialState&gt;&gt;;<br>  }<br>}<br><br>window.pubsub = {};<br><br>const pubSub = createPubsub&lt;typeof initialState&gt;(initialState, window.pubsub);</pre><h3>Feedback</h3><p>Feel free to message me directly or <a href="https://github.com/AntonShanyuk">on github</a>. Pleas share and write the comments, I’ll be happy to answer your questions :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=43216ccc83f1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>