<?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 Quirino Lo Russo on Medium]]></title>
        <description><![CDATA[Stories by Quirino Lo Russo on Medium]]></description>
        <link>https://medium.com/@quirinolorusso?source=rss-5dd18b142a8f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*2-N368VRlWXeLBVa.</url>
            <title>Stories by Quirino Lo Russo on Medium</title>
            <link>https://medium.com/@quirinolorusso?source=rss-5dd18b142a8f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 23:39:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@quirinolorusso/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[Design Declarative React Components]]></title>
            <link>https://medium.com/@quirinolorusso/design-declarative-react-components-6b71c9cfeecb?source=rss-5dd18b142a8f------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b71c9cfeecb</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[components]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Quirino Lo Russo]]></dc:creator>
            <pubDate>Fri, 14 Feb 2020 20:58:00 GMT</pubDate>
            <atom:updated>2020-02-15T09:40:21.481Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*t68q-vfbH2eLpJxxh38PTA.png" /></figure><p>In the last years, I’ve been working in quite big companies and in quite big projects, where all the times in addition to deliver business features, my tasks are also including to create a dedicated module with reusable UI components to be shared across the applications (monorepos or not, that’s another topic).</p><p>In a lot of PRs I review, I usually see a bad practice: react components designed in a way which makes them hard to be used, usually following an imperative approach.</p><p>The first SPA framework I’ve been using was angularJS, when React was not available yet, and at that time it looked awesome and I really enjoyed it.</p><p>When I tried React the first time, one of the things I liked the most (and I still do fortunately!) is its Declarative philosophy and approach (and yeah, I am one of the folks who did not really understand why JSX, and then loved it).</p><p>In this post, I am going to focus on the design of a <strong><em>ButtonGroup </em></strong>component, for showing a group of buttons, with in mind the fact that you are working on creating a reusable UI components library. <br>With this assumption, the users who will use your “product”, are not the end users or the customers, but the developers, who are going to use our component in their modules.</p><p>Although providing code snippets, the focus is purely on the design and not the best code practices to follow (tons of articles already cover it enough).</p><p>If you have ever had this kind of tasks, or if you will ever have, keep in mind this principle before to start any code implementation or design: <strong>what will be the best, easiest, cleanest and most flexible way, to use the component by other developers?</strong></p><p>This question looks simple, but it is not.<br>It is actually the same question that you ask yourself when delivering business feature, focusing usually on the UX, but being your users other developers, you must focus on providing the best DX instead.</p><p>I reviewed a lot of PRs during these years, and a lot of developers end up with an implementation for our ButtonGroup component, which will be possible to use the component with a code similar to the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb46e1d75201c0d3e815b7013744e501/href">https://medium.com/media/bb46e1d75201c0d3e815b7013744e501/href</a></iframe><p>This usage usually corresponds to an implementation like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/71bba9cb570f5c6447a6e187d4d6b604/href">https://medium.com/media/71bba9cb570f5c6447a6e187d4d6b604/href</a></iframe><p>We can immediately see a first issue: the button elements must provide a key prop (do not use the <em>index</em> of the loop!).</p><p>What’s one of the most common solutions I see? <br>Add to the button items another property (a unique id), to be used as <em>key </em>of the button elements:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c558374206a0771b1df1700114c00668/href">https://medium.com/media/c558374206a0771b1df1700114c00668/href</a></iframe><p>Resulting into an usage like the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a77ab75d32e65fdae525ab789fba76e1/href">https://medium.com/media/a77ab75d32e65fdae525ab789fba76e1/href</a></iframe><p>Good…the issue is gone and now all is good? <strong>No!</strong></p><p>The first DX issue of this approach is that the developers need to provide an id to each button, which is a property only related to our implementation details, and not needed for the usage of the component itself.</p><p>Plus, we start to have objects with properties all over, breaking the declarative approach of React.</p><p>The situation becomes even worse when adding extra features to our component.</p><p>A colleague comes to us and ask: “<em>I need to have the buttons of our component which are shown or hidden depending on some condition. Does it support it?</em>”<br>And of course the reply is: “<em>Not yet, but no worries, it will take a second!</em>”</p><p>What is the solution that all of us have already in mind? “<em>Let’s add a shouldRender property to our button items!</em>”</p><p>Omitting the implementation, we will end up with a code that needs to be used in the following way:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/019e0557ca973373651dfb2bd574c15f/href">https://medium.com/media/019e0557ca973373651dfb2bd574c15f/href</a></iframe><p>This situation will get more complicated over and over, when new features will be required by other developers (e.g. I do need to have active status for buttons, or disabled/enable them based on conditions, etc), and we will end up changing our implementation of the component over and over, plus enforcing its usage through lot of objects and props.</p><p>Let’s now completely forget the implementation above, and let’s start with in mind the following question: how would I like to use this component once ready, in order to be React friendly and flexible?</p><p>“Talking Reactish”, the answer is usually one: <strong>declarative</strong>!</p><p>And to achieve it, we use what the React team highlights as the key feature of React: <strong>composition. </strong>(<em>“Composition: The key feature of React is composition of components.” — </em><a href="https://reactjs.org/docs/design-principles.html">https://reactjs.org/docs/design-principles.html</a><em>)</em></p><p>Let’s see if we can have this component in order to achieve the following points all together:</p><ol><li>Declarative usage</li><li>Easy to be used by developers, without providing props required only by the internal implementation</li><li>Have out of the box the button functionalities (hide/show, enable/disable)</li><li>Change less code as possible in the component implementation</li></ol><p>These points, might be reflected to an usage like the one below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40e75911c3485ae0f4b3a024b6bae947/href">https://medium.com/media/40e75911c3485ae0f4b3a024b6bae947/href</a></iframe><p>Pretty nicer and cleaner, isn’t it?</p><p>And this approach, well designed, will do something more than just being declarative, clean and nice to see and structure: <strong>it will give to the developers to use all the power of React for having basic features</strong>!</p><p>The same colleague comes and he asks the same question: “<em>I need to have the buttons of our component which are shown or hidden depending on some condition. Does it support it?</em>”<br>To the same question, we can now reply with a total different reply: “<em>Yes ofc, React already offers this out of box, then use React</em>!”</p><p>This will be the usage, <strong>without changing a single line in our component implementation</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/21625a5ef95e8849a77dba2a779e76e3/href">https://medium.com/media/21625a5ef95e8849a77dba2a779e76e3/href</a></iframe><p>Let’s go a bit further. The same colleague comes again, after few days, asking: “I need to have some of the buttons disabled based on a condition now, does it support it?”<br>And as per magic, the reply will be again the same: “Yes ofc, React already offers it this out of the box!”</p><p>Again, without changing a single line of code in our component implementation, the usage for achieving this will be something like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/02b5566c3b548f66006c2e72c1258d12/href">https://medium.com/media/02b5566c3b548f66006c2e72c1258d12/href</a></iframe><p>If now we show the implementation of our ButtonGroup component which allows this usage, we will be surprised to see how much it is smaller and cleaner, providing in addition more features and a better DX.</p><p>Our <em>ButtonGroup </em>implementation now is simply:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc5a23db3630eb26a09812bed2ef4b9a/href">https://medium.com/media/cc5a23db3630eb26a09812bed2ef4b9a/href</a></iframe><p>All “the magic” happens thanks to the <strong>composition</strong>, through the creation of the <em>ButtonGroupItem </em>component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38fbd9af732c9a40ab6b3c8cca11331f/href">https://medium.com/media/38fbd9af732c9a40ab6b3c8cca11331f/href</a></iframe><p>And yes, this is all the real implementation of the sample above!</p><p>Simply providing the props to the <em>HTMLButtonElement, </em>we have out of the box all the functionalities offered by a button in React. <br>(p.s. <em>I could also keep all the props instead of destructuring, cause children it is actually just a prop, but I prefer the above way for showing that we do care about the children here and we pass it as content</em>).</p><p>Using <em>TypeScript (or PropTypes), </em><strong><em>we </em>can also make the user aware of the allowed props and the children type</strong>, offering a better DX. <br>For instance, we can define the following types as props of the components:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/585576bca0bb613681fc2c80c0605f8d/href">https://medium.com/media/585576bca0bb613681fc2c80c0605f8d/href</a></iframe><p>As conclusion, I would like to show another use case we may have for a component like the one we are designing, which will take us through some interesting features which are offered by React.</p><p>Again the same colleague, and this time he asks: “<em>Does the ButtonGroup component allows a prop to the container which disables all the inner buttons</em>?”</p><p>Using some React Top Level API (<a href="https://reactjs.org/docs/react-api.html">https://reactjs.org/docs/react-api.html</a>), we can change the <em>ButtonGroup </em>implementation like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/52dd8754178f4285d761dc2de81ff45c/href">https://medium.com/media/52dd8754178f4285d761dc2de81ff45c/href</a></iframe><p>As you can see, we use two React API: <em>children </em>and <em>cloneElement</em> in order to loop over the children, and clone each child adding also a disabled prop, based on the <em>ButtonGroup </em>disabled prop value.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b71c9cfeecb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>