<?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 Tim Kolberger on Medium]]></title>
        <description><![CDATA[Stories by Tim Kolberger on Medium]]></description>
        <link>https://medium.com/@TimKolb?source=rss-84bf417268d7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*wBh_0FYCdrV2Q3tIv1Q-Mg.jpeg</url>
            <title>Stories by Tim Kolberger on Medium</title>
            <link>https://medium.com/@TimKolb?source=rss-84bf417268d7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 17:03:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@TimKolb/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[Creating a reusable React Query component for your REST API]]></title>
            <link>https://medium.com/@TimKolb/creating-a-reusable-react-query-component-for-your-rest-api-1aa91ef2551b?source=rss-84bf417268d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/1aa91ef2551b</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[composite-components]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-architecture]]></category>
            <dc:creator><![CDATA[Tim Kolberger]]></dc:creator>
            <pubDate>Sun, 30 Sep 2018 17:35:54 GMT</pubDate>
            <atom:updated>2018-10-02T19:04:45.991Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*AVi4Wvv_YZ7qSpAd" /><figcaption>“person standing on top of mountain” by <a href="https://unsplash.com/@alessandroerbetta?utm_source=medium&amp;utm_medium=referral">Alessandro Erbetta</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>When implementing a new UI component I always start with the layouting and mock the server commucation by providing <em>defaultProps </em>and empty click listeners for user interactions. After completing the UI components I replace the mocked functions and props with the real thing.</p><p>I reinvented the wheel over and over again in every component which consumes data from a server. Using and configuring fetch with HTTP headers, deserialization logic and handling success, error and loading states add many and often duplicate lines to your codebase.</p><p>Wouldn’t it be nice to outsource the communication logic to a <strong>reusable component</strong>?</p><h4>💪 Just do it.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2Yr-Q1TTzwMgP-kCBniF4Q.png" /></figure><p>Working on a React application which consumes an API you have to deal with a bunch of difficulties. For every request you need to handle a loading, error and success state.</p><p>Integrating these functionalities in an existing component will increase the complexity of it and does not leverage the component based approach of React.</p><p>Computer Science isn’t a new thing anymore and we are rediscovering some rules and tools from the old days. One of them is the rule of separation of concerns.</p><blockquote>Write programs that do one thing and do it well — Doug McIlroy</blockquote><p>Let’s transfer this quote by the inventor of the Unix pipe to React components, where a component in a React application is the corresponding program in a unix system. Provide props to control the behavior of a component and use an universal interface for your output. In JavaScript our universal interface is the function type — this leads us to <strong>Composite Components</strong>.</p><h4>🔌 Provide reusable functionality with Composite Components</h4><p>The composite component pattern is getting promoted by <a href="https://medium.com/u/db72389e89d8">Kent C. Dodds </a>which leverages. another design principle in addition to the one-thing-principle: the inversion of control, which shifts the non-core functionalities to another component.</p><p>Using a Query component gives you the ability to fetch a url — not no less. The decision which jsx-elements to render, based on the query result, is shifted to the using side. The Query component has no strong coupling to any component, not to the url nor any other prerequisite.</p><p>It is fully customizable wherever it is used. You can provide a custom deserialize function to deal with different response types like <em>json, text </em>or<em> xml</em>, check for the response status codes and alter the default behavior in place. A state reducer lets you intercept the state updates to change the Query component behavior based on the response results.</p><p>The Query component is a Composite Component and additionally renders a React Context Provider which wraps its children. This enables the using side to use <strong>Compound Components</strong>. Let’s see this in action:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2-SmVcs3FaXAh4PuXZFiZg.png" /><figcaption>Query component usage with Compound Components</figcaption></figure><p>If you have dipped your toes into the GraphQL universe you may have seen one of the Query, Mutation or Subscription components provided by the awesome <a href="https://www.apollographql.com/docs/react/">react-apollo</a> library. These components provide a straight forward API for integrating server communication logic in your React components — even for REST APIs with <a href="https://www.apollographql.com/docs/link/links/rest.html">apollo-link-rest</a>. I am totally into good developer experience when it comes to coding. But there are situations where you do not want to pull in an extra library as dependency. So let’s try to recreate a comparable developer experience on our own for a REST API.</p><p>Let’s take a look at a query component which passes additional information, like the loading and error states of the request, to the consuming child component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WBq3Ij1QRuoH01MI_Jor6w.png" /><figcaption>Query component implementation</figcaption></figure><h4>⚙️ Customize its behavior</h4><p>The Query component is basically a small component which provides the capabilities of <em>fetch </em>as a component. Drop it somewhere in your React tree where you need data from a server and your code stays readable.</p><p>Server communication does not consist just of the consuming GET request. Often you want to trigger a request on user interaction to create, update or delete an entity.</p><p>We can alter the behavior of the Query component on user interaction. Basically we can alter every fetch option to be able use different HTTP methods like POST or DELETE or change the URL.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iLz0lkMNUxBe8VoWxRoucQ.png" /><figcaption>Using the Query component with url query parameters on user interaction</figcaption></figure><h4>🛍 Takeaway</h4><p>Integrating server communication into your components can clutter your code. Extract recurring request logic to a composite component to be able to reuse it in your application. This approach helps you to keep your code DRY and leverages the component based approach of React and the separation of concerns.</p><h4>✍️ Try it on CodeSandbox</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2F92n5zmoq2y%3Fmoduleview%3D1&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2F92n5zmoq2y%3Fmoduleview%3D1&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2F92n5zmoq2y%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/1374adcfb40180bd03e7431ec09f9609/href">https://medium.com/media/1374adcfb40180bd03e7431ec09f9609/href</a></iframe><p>👋 Hi! I am Tim Kolberger. I work at <a href="https://www.incloud.de/">Incloud</a> in Darmstadt, Germany, as a full stack web developer. I ❤️ React, GraphQL and JavaScript.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1aa91ef2551b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>