<?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 Elham Fadel Elshami on Medium]]></title>
        <description><![CDATA[Stories by Elham Fadel Elshami on Medium]]></description>
        <link>https://medium.com/@elhamelshami.dev?source=rss-3f1ba51f7567------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Pf60uS-rUi5ZuIyqLaB2hw.jpeg</url>
            <title>Stories by Elham Fadel Elshami on Medium</title>
            <link>https://medium.com/@elhamelshami.dev?source=rss-3f1ba51f7567------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 08:13:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@elhamelshami.dev/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[What is the Prop Getters Pattern?]]></title>
            <link>https://medium.com/@elhamelshami.dev/what-is-the-prop-getters-pattern-26349e5637df?source=rss-3f1ba51f7567------2</link>
            <guid isPermaLink="false">https://medium.com/p/26349e5637df</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[patterns]]></category>
            <category><![CDATA[hooks]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[reactjs]]></category>
            <dc:creator><![CDATA[Elham Fadel Elshami]]></dc:creator>
            <pubDate>Tue, 20 Sep 2022 19:03:26 GMT</pubDate>
            <atom:updated>2022-09-21T10:00:55.105Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PAIHpmi0VePxxzWxplzonQ.jpeg" /></figure><h4>One of the advanced React patterns.</h4><p>Like every React developer who tends to improve productivity and write code that can be extended more than modified, I’d like to explain <strong><em>prop Getters Pattern</em></strong>.</p><p>But Because the majority of React developers prefer<em> </em><strong>functional components</strong>, we will write React Component in this style.</p><p>So Let’s Go …</p><h3>What is the Prop Getters Pattern?</h3><p>Well, we all know that in Javascript all functions are objects. However, functions are built to be more customizable and reusable.</p><p>So I’ll try to rely on a simple example which is <strong>toggle button</strong>, but before we dive into it, let’s make sure we have the right information about first line.</p><p>Consider the following:</p><pre>const object = {name: &quot;Muhammad&quot;}</pre><p>We create one object with a constant value for the name property, but if we want to customize this object and create another we are forced to do that:</p><pre>const createObject = value =&gt; ({name:value});<br>// now function can be called multiple times to create other object<br>const name2 = createObject(&quot;Adam&quot;);<br>const name3 = createObject(&quot;Youssef&quot;);</pre><p>So, What about our pattern?</p><p><strong>Prop getter pattern</strong> <strong>design</strong> is based on function, as mentioned previously functions can be customized therefore, using a prop getter allows for more interesting use cases.</p><p>Let’s see the code …</p><p>We have a custom hook called <a href="https://gist.github.com/ElhamFadel/825d57a5cbf88e19b67ddda2100c499f"><strong>useToggle</strong></a><strong>:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f5ee9081579763a805285a3b6fc58ac/href">https://medium.com/media/7f5ee9081579763a805285a3b6fc58ac/href</a></iframe><p>This hook will return an object containing isTogglestate, toggle(function to handle click event), and togglerProps which is an object that collects all props that I want to pass in the button component.</p><p>Then I import useToggle in the App.js and destruct the object:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/26f7021e0121f8db286c440bc2d41cdf/href">https://medium.com/media/26f7021e0121f8db286c440bc2d41cdf/href</a></iframe><p>We need isToggle boolean to make a conditional render<strong> </strong>that can control toggle of elements display.</p><p>If you notice I use spreadsyntax on line 7 to pass togglerProps object as props into the button.</p><p>Finally, I click on this button. I get:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/1*lS1wJW0zPPsb27IGTxKKmg.png" /><figcaption>Result after one click</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/886/1*szN6WvDJygCnL-vhSIYRrQ.png" /><figcaption>Render element into the DOM</figcaption></figure><p>So How we can improve above code to be more powerful and customizable as you said?</p><p>If we convert togglerProps object to be function and called it getTogglerProps just for meaningful:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/409f2ada06c1d8db4f64208ccb39e4de/href">https://medium.com/media/409f2ada06c1d8db4f64208ccb39e4de/href</a></iframe><p>Now, we’ll call this function like a regular function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4197e1317dce28d970e682609697760b/href">https://medium.com/media/4197e1317dce28d970e682609697760b/href</a></iframe><p>And that’s it! The user’s app works just like before.</p><h4>Wait Wait Wait … “the user’s app works like before!!”</h4><p>So why do I bother myself??? Well, come with me!</p><p>For example, If we have this code from one of your teammates:</p><pre>&lt;button id=&quot;button_1&quot; {...getTogglerProps()}&gt;Click here&lt;/button&gt;</pre><p>Isn’t the IDpart of the <strong>button’s props</strong>?</p><p>Also, Look at this disaster 👇</p><pre>&lt;button id=&quot;button_1&quot; {...getTogglerProps()}&gt;Click here&lt;/button&gt;<br>&lt;button id=&quot;button_2&quot; {...getTogglerProps()}&gt;Click meee&lt;/button&gt;<br>&lt;button id=&quot;button_3&quot; {...getTogglerProps()}&gt;Click I see 😠&lt;/button&gt;</pre><h4>So, How can give freedom and flexibility to add Additional props to the default props collection??</h4><p>Well, we can pass all values as arguments:</p><pre>&lt;button {...getTogglerProps({id:&quot;button_1&quot;})}&gt;Click here&lt;/button&gt;</pre><p>Then, let’s update getTogglerPropsfunction to cater to new props:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6df244c89ccdd9edcf688c064e56198e/href">https://medium.com/media/6df244c89ccdd9edcf688c064e56198e/href</a></iframe><p>If you see we pass props as a parameter that holds all the custom props</p><p>Then spread all custom props into collection props.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/1*dqCyfytViWvz-ErEaszSzA.png" /><figcaption>Additional client props</figcaption></figure><p>Wow!! Did you see it? The IDpassed!!</p><p>One step away…</p><p>Now, what if the client wanted to pass in a custom onClick prop?</p><p>Like this:</p><pre>&lt;button<br>{...getTogglerProps({<br>id: &quot;button_1&quot;,<br>onClick: () =&gt; alert(&quot;hello inside button 1&quot;)<br>})}<br>&gt;<br>Click here<br>&lt;/button&gt;</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FmqOnK7XHTyQA38o2Bj%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FmqOnK7XHTyQA38o2Bj%2Fgiphy.mp4&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FmqOnK7XHTyQA38o2Bj%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="303" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f5f004288b6b62bf5585b1f16d8ffa0/href">https://medium.com/media/8f5f004288b6b62bf5585b1f16d8ffa0/href</a></iframe><p>ًWhat happened? Isn’t the onClickhandler being updated?</p><p>Yes, when you do this you completely override the defaultonClick handler</p><p>because using spread operator will do updates for all togglerprops</p><p>Hmm, what we can do?</p><p>Actually to solve this problem we want to compose the default handler with the custom onClick handler so that both of these functions are called.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/33f238c634d21342275302594b47ed48/href">https://medium.com/media/33f238c634d21342275302594b47ed48/href</a></iframe><p>Look above ☝️ we create excuteFunctionsfunction that accepts any number of functions then to avoid neglecting the passed arguments we use this spread syntax (...args) .</p><p>So the result:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fmh4NICeOGKNonB7w3a%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fmh4NICeOGKNonB7w3a%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2Fmh4NICeOGKNonB7w3a%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="258" frameborder="0" scrolling="no"><a href="https://medium.com/media/16cb706cfe97a2bd7810fff75791386f/href">https://medium.com/media/16cb706cfe97a2bd7810fff75791386f/href</a></iframe><p>Yay, we did it 👏 👏 👏.</p><h3>Conclusion</h3><p>There are numerous basic and advanced patterns that can be used to create robust, reusable, and adaptable components.</p><p>This article was mainly inspired by the amazing work of <a href="https://medium.com/u/db72389e89d8?source=post_page-----a6b7624267a6--------------------------------">Kent C. Dodds</a>, who is a famous developer within the React community(<a href="https://kentcdodds.com/">https://kentcdodds.com/</a>).</p><p>Thank you for reading.</p><h3>References</h3><p><a href="https://kentcdodds.com/blog/how-to-give-rendering-control-to-users-with-prop-getters">How to give rendering control to users with prop getters</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FgBQtUp3xwQc%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DgBQtUp3xwQc&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FgBQtUp3xwQc%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e2ca318d3953f5105246b80ee05a491/href">https://medium.com/media/4e2ca318d3953f5105246b80ee05a491/href</a></iframe><p><a href="https://javascriptbit.com/react-design-patterns-prop-getters-render-props/">React Design Patterns — Prop Getters &amp; Render Props — JavaScriptBit</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=26349e5637df" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>