<?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 Bill Tuan on Medium]]></title>
        <description><![CDATA[Stories by Bill Tuan on Medium]]></description>
        <link>https://medium.com/@billtuan?source=rss-4a0c5be09dfe------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*46zKYr3X8XWcREN3.</url>
            <title>Stories by Bill Tuan on Medium</title>
            <link>https://medium.com/@billtuan?source=rss-4a0c5be09dfe------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 16:58:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@billtuan/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[Reactive programming]]></title>
            <link>https://medium.com/@billtuan/reactive-programming-a0a09fb05896?source=rss-4a0c5be09dfe------2</link>
            <guid isPermaLink="false">https://medium.com/p/a0a09fb05896</guid>
            <dc:creator><![CDATA[Bill Tuan]]></dc:creator>
            <pubDate>Sun, 25 Aug 2019 11:28:41 GMT</pubDate>
            <atom:updated>2019-08-25T11:28:41.069Z</atom:updated>
            <content:encoded><![CDATA[<h4>What is Reactive programming? Why? How to use it with React?</h4><p>Maybe you’ve seen it used a few places but you’re still a little confused and would like some clarifications. In this blog, you will have an overview of basic concepts of <strong>Reactive Programming</strong>, we are going to have some examples to clarify the idea and have an introduction to<strong> RxJs</strong> in React application development.</p><p>So, first let’s understand what is Reactive?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*ndTZFg2oTJB8PCCRfYYbHw.png" /><figcaption>Reactive?</figcaption></figure><p><strong>Reactive</strong> is a word that talks about something that <strong>actively reacts</strong> with changes from outside. In programming, it can be present for function or feature that executing after others function or feature has finished executing. Instance, the <strong>callback</strong> in javascript is a reactive, or cells that calculated on others cells in the <strong>spreadsheets</strong>.</p><p>Let’s take another example in real life.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lg33c_woPKfuqAsOtgzpdw.png" /><figcaption>Update invoice feature</figcaption></figure><p>You usually have faced this kind of feature in an eCommerce application. We have 2 instances: <strong>Cart</strong> and<strong> Invoice</strong> when a new product is added to Cart then update the total amount charged in the Invoice.</p><p>What is your idea about this feature?</p><p>The common solution is that you can create an update amount function in Invoice instance and use it in Cart when updating a product, in this way you are using <strong>Passive Programming</strong> pattern.</p><p><strong>Hmm…</strong>What if we have to change the function name of the Invoice, that will cause an error when calling the update amount.</p><p>A more effective solution is that <strong><em>emitting an event</em></strong> in Cart when adding a product and have a <strong><em>listener</em></strong> in Invoice to listen on that event to update the total amount, in this way, Cart assume is broadcasting (send info to outside), Invoice assume a reactive role listening event and change itself.</p><p><strong>Reactive</strong> is whenever you have <strong><em>self-updates </em></strong>according to <strong><em>events.</em></strong></p><h3>What is Reactive Programming?</h3><p><em>According to Wikipedia:</em></p><blockquote>Reactive programming is a programming paradigm oriented around data flows and the propagation of change. This means that it should be possible to express static or dynamic data flows with ease in the programming languages used and that the underlying execution model will automatically propagate changes through the data flow.</blockquote><p>In simple words, <strong>Reactive programming</strong> is programming with asynchronous <strong><em>data streams</em></strong>.</p><h3>RxJS</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lh8nx_MI-lB3dK4fz2LY7A.png" /><figcaption>RxJS</figcaption></figure><p>Reactive Extensions for JavaScript (RxJS) is a reactive streams library that allows you to work with <em>asynchronous data streams (events)</em>. RxJS can be used both in the browser or in the server-side using Node.js.</p><p>Let’s take an example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c6c00f1ab5be20dc7eb2f429228c6166/href">https://medium.com/media/c6c00f1ab5be20dc7eb2f429228c6166/href</a></iframe><p>And with RxJs library:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9132fd19419ad8dcaef57095f064b4fe/href">https://medium.com/media/9132fd19419ad8dcaef57095f064b4fe/href</a></iframe><blockquote>A different syntax with the same result? So why I have to switch to Reactive Programming?</blockquote><p>Ok, one beautiful day, your boss says that users were spamming the form by click on the submit button. How can you resolve this problem? Very simple with RxJs:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/82195b2ad4d661ee15f6192d49e55e66/href">https://medium.com/media/82195b2ad4d661ee15f6192d49e55e66/href</a></iframe><p>Let’s visualize the code above:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rkqGV_4uz4VzPAywziKbag.png" /><figcaption>Every click event will debounce 500ms before hitting the server</figcaption></figure><p><strong><em>Observable</em></strong> is a collection that arrives <strong>over time, </strong>it presents a<strong> <em>stream</em>. </strong>In this example, we used an <strong><em>Observable </em></strong>followed by a chain of <strong>operators</strong> ending with a call to <strong>subscribe</strong>.</p><h3>Integration with React</h3><p>RxJS plays well with React but instead of writing your own helper functions to bridge the two.</p><p>Some simple examples that you can apply RxJS into your code base:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fq85m60461w&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fq85m60461w&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fq85m60461w%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/6be0bc484cdb2061f5b8bd808bbb3f73/href">https://medium.com/media/6be0bc484cdb2061f5b8bd808bbb3f73/href</a></iframe><p>Or config with <a href="https://github.com/acdlite/recompose/blob/master/docs/API.md#observable-utilities">recompose</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Flx832l7k4z%3Fautoresize%3D1&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Flx832l7k4z%3Fautoresize%3D1&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Flx832l7k4z%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/14a33da6c6d978891a9e0bec2a4a6063/href">https://medium.com/media/14a33da6c6d978891a9e0bec2a4a6063/href</a></iframe><p>Reuse with render props</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2F6qkx5oovk%3Fautoresize%3D1&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2F6qkx5oovk%3Fautoresize%3D1&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2F6qkx5oovk%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e9c2be9f38e2d16e83e884ea0e6a4b2/href">https://medium.com/media/2e9c2be9f38e2d16e83e884ea0e6a4b2/href</a></iframe><h3>Redux-Observable</h3><p>Let’s go over a scenario that is simple enough to digest (hopefully) in a blog like this.</p><p>Imagine you are working on a drawing app. In this app, you have access to very granular drawing events, and by drawing a simple curve you end up with about 100 events. This is exaggerated, but I’m making it a high number to make our example a bit clearer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DDToLXOQNsKbxeuTNKDF_w.jpeg" /><figcaption>Fire actions from drawing component</figcaption></figure><p>Now, in this drawing app of yours, an action gets dispatched for each drawing event with from and to coordinates. Keep in mind that in our example, drawing a simple curve will result in about 100 events, resulting in 100 actions getting dispatched. And after use lifts up their finger or mouse, the array of lines will send to the server.</p><p>With redux-thunk:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a34f6673269f0f405922df952aedf0ce/href">https://medium.com/media/a34f6673269f0f405922df952aedf0ce/href</a></iframe><p>The disadvantages of using redux-thunk in the scenario:</p><p>You have to store the lines value in your Redux store, because you’re collecting them using one action and sending it up to the server using another.</p><p><strong>Why is this a problem? </strong>We did not need the lines array on state, we just put it there, we are storing stuff on state, which would call components to be re-rendered, even though they won’t need to.</p><h4><a href="https://redux-observable.js.org/"><strong>Redux-observable</strong></a> is another middleware option that allows you to handle asynchronous logic.</h4><p>Although it solves the same problem and has some similarity, it works quite differently to redux-thunk.</p><p>With the code from above, all we have is a different syntax than redux-thunk. But if you look carefully, you might spot how redux-observable solves the issues in the more complex example that we had earlier in the redux-thunk section.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f1edb5fcf1dcb19ec8616eb3ae970fce/href">https://medium.com/media/f1edb5fcf1dcb19ec8616eb3ae970fce/href</a></iframe><p>You’ll notice that it solves the problem identified when we used redux-thunk to solve the same problem. We do not need to store the lines on state anymore, so state just reflects what we want components to use.</p><p>We do not have the batching and sending coupled to the DRAWING_MOUSE_UP action anymore. It’s only used as a signal to stop batching. We’ve effectively gone from an imperative relationship between the action and batching to a reactive relationship where it becomes a signal.</p><h3>Conclusion</h3><p>Reactive programming isn’t just another trend but rather <em>the paradigm</em> for modern software developers to learn.</p><p>While RxJs might look a bit weird to you, it really starts to shine when you plug it into applications with relatively complex async logic.</p><p>For more depth understanding of RxJS checkout provided reference links.</p><h3>References</h3><ol><li><a href="https://rxjs-dev.firebaseapp.com/">RxJS official website</a></li><li><a href="https://www.learnrxjs.io/">LearnRxJS</a></li><li><a href="https://egghead.io/lessons/rxjs-what-is-rxjs">What is RxJS?</a></li><li><a href="https://toddmotto.com/rxjs-observables-observers-operators">RxJS: Observables, Observers and Operators Introduction</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a0a09fb05896" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>