<?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 Akhil Nair on Medium]]></title>
        <description><![CDATA[Stories by Akhil Nair on Medium]]></description>
        <link>https://medium.com/@4khiln4ir?source=rss-532d23da8908------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*MSvn43NH_U-bBsaW</url>
            <title>Stories by Akhil Nair on Medium</title>
            <link>https://medium.com/@4khiln4ir?source=rss-532d23da8908------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 09:58:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@4khiln4ir/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 Query — The easy state manager]]></title>
            <link>https://tech.fampay.in/react-query-the-easy-state-manager-2d71e97745?source=rss-532d23da8908------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d71e97745</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[query-optimization]]></category>
            <category><![CDATA[react-query]]></category>
            <dc:creator><![CDATA[Akhil Nair]]></dc:creator>
            <pubDate>Mon, 14 Feb 2022 08:38:51 GMT</pubDate>
            <atom:updated>2022-02-14T08:38:51.489Z</atom:updated>
            <content:encoded><![CDATA[<h3>React Query — The easy state manager</h3><p><a href="https://react-query.tanstack.com/">React Query</a> is a library that manages server data on your client. In this article, I will walk you through what it is, what it does, and how it helped us manage data and maintain a clean code-base here at FamPay.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oCtVScfVUOQj5p7qYK4ChA.png" /></figure><p>Hi there, I am Akhil Nair, a Frontend Engineer here at FamPay. And my job is to ensure that our users have the smoothest experience when interacting with our services. And we’re going to look into something that helps us do exactly that in this blog. So let’s get right into it!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vD4mY6iEQ34HTcAYyPTlnw.png" /></figure><h4>What is React Query?</h4><p>React Query is a small library that helps us handle the data we fetch from the server on the client. Alright, What does that mean? Can’t we use Redux or other state management libraries to store server data in a global state and manage them? Yeah sure, but let me take you through both the journies and show you how one is so simple while the other is a mess.</p><p><strong>Scenario</strong></p><p>Imagine you are building Facebook. You’re working with hundreds of routes and thousands of components. Suppose you need the logged-in users display_name at 15 routes. Will you make 15 separate API calls for this?</p><p><strong><em>Using State Management ( Redux, MobX )</em></strong></p><p>With Redux, you’ll wrap your entire App with Provider and call your API at the top level of the app, and then consume the display_name at all the places required.</p><p>Here’s the messy part, you have to manage display_name manually throughout the app, you also have to update display_name whenever you call a change name API, leading to a lot of async-awaits, reducer calls, loading states, error states, etc.</p><p>You may even miss updating this manually after an API call. There is also a chance that your client data is not in sync with your server data.</p><p>Here we’re only talking about one data field. Imagine managing a dozen data fields like wallet balance, online status, profile picture, etc.</p><p><strong><em>With Query Management ( React Query )</em></strong></p><p>Now let us simplify the data management with React Query. You’ll need to call the API inside the 15 components where you need display_name.</p><p>However, the API will not be called all 15 times as the data will be cached after the 1st API call, all subsequent calls will use a cached response. React Query can do this because the API call to get `display_name` is a single function with a unique key.</p><p>The unique key in React Query is the most important part here. The key is the control of the API. With it, you can in-validate the query after you call the update display_name API. Once a query is invalidated, it will be re-fetched and updated at all the places where the display_name is used. It will also control the loading and error state, something you would have to do manually with Redux.</p><h4>The Migration Here At FamPay</h4><p>We at FamPay recently shifted from MobX (State Manager) to React-Query (Query Manager) and our Frontend Codebase has never been cleaner.</p><p><strong>What problems did we face?</strong></p><p>Firstly, we wanted a cleaner way to write our components, we never liked writing isLoading, isError, data and manually managing them with writing state. The problems only grew if the API was paginated.</p><p>Now with React-Query, we can manage the component in a much cleaner and readable way with also handling loading and error states.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/866/1*6Ue_c6rKjsATQth8_o90ag.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/963/1*TKQc6oKgWI2Tizr9xOg4XA.png" /></figure><p>Now, we won’t re-write the entire app just for clean and less code, so let&#39;s see the bigger data management problems we had.</p><p>We are an App first company and we wanted to implement a pull to refresh in our web-view which would refresh all the API calls on the given route.</p><p>In the image below, we needed to update Coin Balance, My Winnings list, Offers, Giveaways and Spinners. All of these are in their own components with their own API endpoints. Also, the Clubs Section is in a separate tab which needs to be refreshed as well because the pull-to-refresh was at the top of the whole page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/413/1*7PKgmvz6DV536gTKnyaoyw.png" /></figure><p>With the traditional Redux Method, this would be a really difficult thing to manage in the long run. We have 8–10 API calls on the home page, and with state management, we would have had to call each API and wait for all their responses and then set their data in the global state while also storing their loading and error states.</p><p>This is how this whole process would look in React Query:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/572/1*LKGqYNGsCmIHeFBQAgE-1A.png" /></figure><p>That’s it, that&#39;s the code to refresh/re-fetch all the API calls in a given route, along with auto managing the isLoading and isError states.</p><p>APIs that are on the current route are considered active, so all we do is pass the active as true in the options of queryClient call, and only the APIs on this route will be prefetched, allowing us to limit the API calls to only those which are required. The data could be 10 levels deep, and it would still be re-fetched as expected.</p><p>Now, here’s one of my favourite react-query features: <strong>Caching</strong>.</p><p>How does it work? Here’s an actual example :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/405/1*YCOSIOXC1QBkncvCT8WR5A.png" /></figure><p>In My Winnings, there are many icons like Cash, Discounts, Referral, etc. Each of them opens a new route and calls a different API. Generally, our users constantly tap on them and go back, and we were calling an API to fetch the data on that route each time it opened.</p><p><strong>Problem?</strong> It doesn&#39;t make sense to fetch the API again since the data will not change unless the user performs an action like claiming a Cash Reward.</p><p><strong>Solution?</strong> Cache the data.</p><p>But manual caching is one of the hardest things to implement, however, react-query makes this a little too easy. We can even specify how long API data will be cached. Now whenever a user claims a reward, all we do is invalidate the cache and the data will be re-fetched.</p><p>on any related API success we can call:</p><pre>queryClient.invalidateQueries(key);</pre><p>This key here is the unique key of the API to invalidate.</p><h4>My Favourite React Query Features</h4><ul><li>Caching</li><li>Background Updates</li><li>API Invalidation</li><li>Mutations</li><li>Custom/Selective Response</li><li>Pagination /Incremental Loading</li><li>Clean Code</li></ul><h3>Conclusion</h3><p>Server data is hard to manage on the client-side as it may not always be in sync and is very messy to implement.</p><p>However, React Query makes it extremely easy to do so. It simplifies server data management without the need for a state manager. It can cache your data, invalidate it and even handle updating data in all the places it’s used.</p><p>If you are still not convinced or felt this article was too long, here’s something to get you onboard:</p><ul><li>imagine writing a component that shows your current balance</li><li>using this component in 5 different places</li><li>then adding a refresh button to a different component on the other side of the app.</li><li>Then being able to click it and get the updated balance in all 5 components, with a single API call.</li></ul><p>That&#39;s it, folks, that&#39;s the magic.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d71e97745" width="1" height="1" alt=""><hr><p><a href="https://tech.fampay.in/react-query-the-easy-state-manager-2d71e97745">React Query — The easy state manager</a> was originally published in <a href="https://tech.fampay.in/">FamPay Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>