<?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 Tom Klotz on Medium]]></title>
        <description><![CDATA[Stories by Tom Klotz on Medium]]></description>
        <link>https://medium.com/@tomklotz.pro?source=rss-fe6abb922693------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*4SLUWSVGKkREeUMRSEDZYA.jpeg</url>
            <title>Stories by Tom Klotz on Medium</title>
            <link>https://medium.com/@tomklotz.pro?source=rss-fe6abb922693------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 08:37:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tomklotz.pro/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 State management with Providers]]></title>
            <link>https://medium.com/@tomklotz.pro/react-state-management-with-providers-67dd2bd95c6d?source=rss-fe6abb922693------2</link>
            <guid isPermaLink="false">https://medium.com/p/67dd2bd95c6d</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[state-management]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Tom Klotz]]></dc:creator>
            <pubDate>Fri, 27 May 2022 08:22:15 GMT</pubDate>
            <atom:updated>2022-05-27T10:12:39.899Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*CzUib_ipMDK4cNNp1E92tA.jpeg" /></figure><p>Handling state in react is comparable to a double-edged sword, you can either use them wisely or properly, but you may also start to create technical debt if you use them badly.</p><p>If the scope of your component is limited, don’t hesitate to use a minimal approach.</p><h3>Minimalistic approach</h3><p>Native hooks are new to React since version 16.8. This article will talk mainly about how we should use them.</p><p>I don’t want to be exhaustive and write about functional and class components. I will focus only on functional components for all our examples.</p><h4>Use State</h4><p>As an example let’s imagine that you want to implement a counter. And you want your counter to be as simple as it is. Your counter will just be a button that will increment the count when you click on the button. For this, you need to keep the information about the count and you need a setter to update the value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6669a7775cb323c2c3db9434c869fe4d/href">https://medium.com/media/6669a7775cb323c2c3db9434c869fe4d/href</a></iframe><p>That’s it! Do you see? It’s minimal because here we just need one state to handle one behavior.</p><p>However, our world is not always as simple as it seems. Sometimes you’ll need to craft complex components and complex flows to build amazing features.</p><p>Imagine you want to have a switch be on if the counter reaches a value, and if you click on the toggle it set the value of the counter to 0.</p><p>It starts to be more complex but let’s do it only with<strong> useState, useEffect, </strong>and<strong> useCallback</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54da240f4c4f6f558e1334767eadda08/href">https://medium.com/media/54da240f4c4f6f558e1334767eadda08/href</a></iframe><p>First of all, I’ve also used two other hooks here <strong>useCallback</strong> and <strong>useEffect</strong>. I’ll not go into too many details about these hooks however here is a little explanation.</p><ul><li><strong>UseEffect</strong>: The default behavior for effects is to fire the effect after every completed render. That way an effect is always recreated if one of its dependencies changes. In our example, UseEffect will be fired only if the count changes.</li><li><strong>UseCallBack</strong>: This hook is similar to a callback, the function will be fired by the component Switch when we click on it. It set the counter to 0 as expected.</li></ul><p>You can already see that our counter component contains too much logic, let’s split them!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cacc5c5bc8a8ec03c2a84f595407ebf8/href">https://medium.com/media/cacc5c5bc8a8ec03c2a84f595407ebf8/href</a></iframe><p>This is much better, we reduce the logic by components and it reduces the code, of course, you can also create dedicated files for each component to increase the readability and separate properly everything with a good hierarchy.</p><p>Nevertheless, we are still not satisfied. We need props to handle the behavior of the count in the custom switch, imagine that we need to go deeper in the hierarchy, we will require to provide down the counter and the setter. This is not optimal. There is a better way to access the state without providing everything in the props.</p><h3>Optimal approach</h3><h4>Context Provider</h4><p>In general, a provider is a good way to share a state, callback, or a constant in a hierarchy of components. When you spot a variable that is passed to all the props this means it could be avoided by having a provider.</p><p>However, it’s also useful when you need to have a shared state between components.</p><p>The provider is an architecture that allows us to centralize logic and data in a single point.</p><p>Here is the architecture</p><ul><li>_.tsx (Provider component)</li><li>constants (shared constants and default value)</li><li>Context (a variable that creates and initiates the context)</li><li>types (files that hold the types if you are using typescript)</li><li>useProvider ( the hook to retrieve shared callback/state etc .. )</li></ul><p>The first file I’ll explain is constants.ts</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af624a1ff0c987d105635d77cbba9a30/href">https://medium.com/media/af624a1ff0c987d105635d77cbba9a30/href</a></iframe><p>This is the default value for the initiation of the context provider.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e086daf3148cd47025ee831bea34d8a3/href">https://medium.com/media/e086daf3148cd47025ee831bea34d8a3/href</a></iframe><p>No secret hiding here, just the type of the value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c209ac732f1014f6fd478acaad986f9b/href">https://medium.com/media/c209ac732f1014f6fd478acaad986f9b/href</a></iframe><p>Now, it become more complex, what we are doing here is to create the context with the method <strong>createContext</strong> from React. This is a react component that will hold the value and the logic!</p><p>Now we want to wrap our logic and our return value around this component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/04690d55c53d4b7c7476ecd4a17ed7ad/href">https://medium.com/media/04690d55c53d4b7c7476ecd4a17ed7ad/href</a></iframe><p>As you can see, we are returning to the <strong>Context.Provider</strong> component with our default value.</p><p>Now that we have our component, we can create a dedicated hook to return our context each time we need a state or a callback.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/22fe149975703998b6deb2028bb11e2f/href">https://medium.com/media/22fe149975703998b6deb2028bb11e2f/href</a></iframe><p>We use the hook <strong>useContext</strong> which will return our Context.</p><ul><li><strong>UseContext</strong></li></ul><blockquote>Accepts a context object (the value returned from <em>React.createContext</em>) and returns the current context value for that context. The current context value is determined by the <em>value</em> prop of the nearest <em>&lt;MyContext.Provider&gt;</em> above the calling component in the tree. ( <a href="https://reactjs.org/docs/hooks-reference.html#usecontext">https://reactjs.org/docs/hooks-reference.html#usecontext</a> )</blockquote><p>Now we have everything, we are ready to reduce our code!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/34a1decc961cfbc87543c7edff07baec/href">https://medium.com/media/34a1decc961cfbc87543c7edff07baec/href</a></iframe><p>We created a little wrapper component that encapsulates our Counter component to allow it to call our Provider hook to retrieve the state and the callback it needs.</p><p>As you see, we reduce our logic inside the component and we don’t need to provide any props to our <strong>CustomSwitch </strong>component<strong>.</strong></p><p>Here is what our <strong>CustomSwitch</strong> component looks like.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb72f2eabef76c4504310307c545c79b/href">https://medium.com/media/bb72f2eabef76c4504310307c545c79b/href</a></iframe><p>Our readability increased a lot, furthermore, we separate our logic from our component, plus we are now able to share callback or state all across our component three. This is the main goal of a provider. We don’t want to provide the responsibility to all the component three, we want to put it in a single point: The Provider.</p><p>To conclude, we can all say there are many ways of handling logic for our react component, and this kind of architecture is one of many answers. Don’t forget that you should use this wisely with your needs, you don’t require a full-provider architecture for a little state. As I like to say:</p><blockquote>With great provider comes great responsibilities.</blockquote><p>Thank you for reading! 🙌</p><p>Tom Klotz, Software Engineer at Castor.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=67dd2bd95c6d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>