<?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 Codegem on Medium]]></title>
        <description><![CDATA[Stories by Codegem on Medium]]></description>
        <link>https://medium.com/@codegem?source=rss-19ba6c50a25f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*OrVCNjzGeixMi06kMobHLQ.png</url>
            <title>Stories by Codegem on Medium</title>
            <link>https://medium.com/@codegem?source=rss-19ba6c50a25f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 15 May 2026 14:24:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@codegem/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 side bar (but sexy?)]]></title>
            <link>https://codegem.medium.com/react-side-bar-but-sexy-3ea0e5cde991?source=rss-19ba6c50a25f------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ea0e5cde991</guid>
            <category><![CDATA[design]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[sidebar]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Codegem]]></dc:creator>
            <pubDate>Sun, 21 Feb 2021 16:40:42 GMT</pubDate>
            <atom:updated>2021-02-21T16:40:42.543Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Nh4WgXmJsm586H80WZwkTA.png" /></figure><h3>Sidebar component</h3><p>For the impatient, the source code is <a href="https://github.com/code-gem/side_bar">here</a>.</p><p>Let’s being by laying our <strong>sidebar</strong> component. The structure is pretty straight forward:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3dd768da042e914966b07ed63f68de66/href">https://medium.com/media/3dd768da042e914966b07ed63f68de66/href</a></iframe><p>Believe it or not but designers call the three horizontal bars as a <a href="https://en.wikipedia.org/wiki/Hamburger_button">hamburger button</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/264/1*xoSYvKRDUE03Q1Eiddozcg.png" /><figcaption><strong>Note:</strong> We are sorry for the designer who had such strange looking hamburgers</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zvcE2wCIuqI0nP3Np40AKQ.png" /></figure><p>We have the hamburger button separated from the actual side bar since it will be laid out on a nav bar, unlike the sidebar which will have a fixed position.</p><p>Inside the sidebar we have a close button and the content, which can be anything you want.</p><h3>Adding State</h3><p>We add <strong>isOpen</strong> state to the side bar, a function <strong>toggleSideBar</strong> which when called will switch <strong>isOpen</strong> from true to false and vice versa.</p><p><strong>isOpen</strong> will also influence the css class we assign to the side bar.</p><ul><li>If true we assign <strong>.SideBar.Active</strong> (open)</li><li>If false we simply assign <strong>.SideBar</strong> (closed)</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/974bba5e5d66ff3fd7fcd148bba902bb/href">https://medium.com/media/974bba5e5d66ff3fd7fcd148bba902bb/href</a></iframe><h3>The CSS</h3><p>Make the <strong>.SideBar</strong> position fixed and its height of 100 view port height. Make sure the <strong>z-index</strong> is 3 or more so the side bar is not buried under any elements on the page.</p><p>The <strong>transition</strong> attribute is what will make css animate the position from left: -100% to left: 0%, when we change the class (see css class <strong>.Sidebar.Active</strong> below):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dcc082de2407b2420ba31d8b2ec64cb2/href">https://medium.com/media/dcc082de2407b2420ba31d8b2ec64cb2/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3Ef9eSUeYdDv9hKWUTAFhw.png" /></figure><p>When the sidebar is active, its left position is 0% meaning its open:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8428a524ccb3161cbc6e42fe6e0a9b04/href">https://medium.com/media/8428a524ccb3161cbc6e42fe6e0a9b04/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*Kbej-aQj-LUjQfHZ1pfISw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5g-cTB0MCACH-Ghzs8FYYw.png" /><figcaption>How the css class change influence the position of the sidebar</figcaption></figure><p>When we change the class, css triggers an animation so the change is smooth (all thanks to the <strong>transition</strong> attribute)</p><p><em>Bonus round:</em> You can also make your close button stick to the top left side of your sidebar so it is aligned with the hamburger button. We also give it a nice scaling animation when we hover over it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d0d895d6d9980ec8f323f8a4f752ca2/href">https://medium.com/media/8d0d895d6d9980ec8f323f8a4f752ca2/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*gZnuXD-IOMKN01Z19XlqeA.gif" /></figure><h3>Result</h3><p>Look at what we’ve created together, isn’t this beautiful?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*6vCpFZ3yJxm1pCqLdD0jGA.gif" /></figure><p>Can anything be improved? Did we miss anything? Give us your thoughts in the comments!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ea0e5cde991" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to create a toggle in React using css]]></title>
            <link>https://codegem.medium.com/how-to-create-a-toggle-in-react-using-css-2f3e47e091be?source=rss-19ba6c50a25f------2</link>
            <guid isPermaLink="false">https://medium.com/p/2f3e47e091be</guid>
            <category><![CDATA[toggle]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[html]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Codegem]]></dc:creator>
            <pubDate>Sun, 21 Feb 2021 03:58:41 GMT</pubDate>
            <atom:updated>2021-02-21T03:58:41.914Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1jg-jUqhLYcB3GzkbNInbg.png" /></figure><p>In this article, we are going to show you how to create a toggle switch in react using css.</p><p>Let’s begin by creating a <strong>Toggle</strong> component in react:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/02d2ace928dbf60d1603ea501a8baa61/href">https://medium.com/media/02d2ace928dbf60d1603ea501a8baa61/href</a></iframe><ul><li>The Toggle component has an input element with the type “checkbox”. We also give it an <strong>id</strong> so we can associate it with the label below.</li><li>The label takes in a <strong>htmlFor</strong> attribute which corresponds to the id of the checkbox.</li><li>Clicking on the label will also toggle the checkbox because of <strong>htmlFor</strong></li></ul><h3>CSS Styles</h3><p>For the css style we will hide the checkbox. We still keep it in the DOM since we can use it in the css to check its state (on/off) using the <strong>:checked</strong> selector:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/543/1*LuM1n5EhGB6SuwwbpUoxng.png" /><figcaption>We hide the default checkbox</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bfa38b5ed7a25688ade3ec0023dca533/href">https://medium.com/media/bfa38b5ed7a25688ade3ec0023dca533/href</a></iframe><p>The <strong>.Toggle</strong> class styles the label, and the <strong>.Circle</strong> class styles the span:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/441/1*hfgszOrOE_sC0UaxRN4Oow.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a547fb641491a8003a77b61b35361ea1/href">https://medium.com/media/a547fb641491a8003a77b61b35361ea1/href</a></iframe><p>The <strong>.Circle</strong> class is made round by keeping the width, height and border-radius the same size. The transition attribute will animate the change in position when we switch css classes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/427/1*IburMVx-ubCcf5p8VHvpbA.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41c8098cbaccbef968d4ccafd48195f0/href">https://medium.com/media/41c8098cbaccbef968d4ccafd48195f0/href</a></iframe><p>And the most important part is the style below. When the hidden <strong>.CheckBox</strong> is checked (on) css will look from the next html tag in DOM with the <strong>.Circle </strong>class and style it as below. This style will move the circle to the right, using `left` <strong>attribute</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2287c75b596d2fa0777257d66f503cc/href">https://medium.com/media/c2287c75b596d2fa0777257d66f503cc/href</a></iframe><p>When the <strong>.CheckBox</strong> is not checked, css will simply use the <strong>.Circle</strong> class and this will trigger the transition attribute to animate the change.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/968/1*-2Js9flQbE49ww_67RMr_w.png" /></figure><h3>Result</h3><p>We have it working 🎉</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*ft63gPQDCFGfLm4AJFIMOg.gif" /></figure><p>For the full code checkout our <a href="https://github.com/code-gem/switch_toggle">github page</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f3e47e091be" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create global state in React as simply as local state]]></title>
            <link>https://codegem.medium.com/create-global-state-in-react-as-simply-as-local-state-7b917bb19490?source=rss-19ba6c50a25f------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b917bb19490</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[state-management]]></category>
            <dc:creator><![CDATA[Codegem]]></dc:creator>
            <pubDate>Wed, 20 Jan 2021 01:02:50 GMT</pubDate>
            <atom:updated>2021-01-20T01:07:55.726Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, we will show you how to create global state using createGlobalState, a function we built. If you are interested in how this function is implemented in more detail go to <a href="https://codegem.medium.com/how-to-create-global-state-in-react-without-redux-typescript-928cb6fcd6ba">this article</a>.</p><h3>Intro</h3><p>In some cases, two components that are not located close to each other in your react component tree need to <strong>share</strong> and <strong>modify</strong> data. For example Layout and UserPosts:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/996/1*iCR8bpjhepXeA-PjkvWQfg.png" /></figure><p>This component tree is simple enough to pass components down via props, but what if the components are not directly connected in a straight line? Like the SidePanel and UserPosts:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/1*EJv6Fgx3nyqqkxhLJn42uw.png" /></figure><p>Redux is a possible fix to this problem but it has plenty of boilerplate and has a steep learning curve <em>especially</em> when not familiar with functional programming. In this article, we discuss how to implement a global state that can be both shared and modified.</p><h3>Create Global State</h3><p>Create a new file named globalState.tsx and copy the code below (implementation details can be found <a href="https://codegem.medium.com/how-to-create-global-state-in-react-without-redux-typescript-928cb6fcd6ba">here</a>):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a5ea9b7b1ed2ae0f9c5dcdd090b0f954/href">https://medium.com/media/a5ea9b7b1ed2ae0f9c5dcdd090b0f954/href</a></iframe><p>The code above creates a context and a context provider wrapper that lets us create a state object that can be modified and shared.</p><p>Let’s say you want to modify user posts in one part of your app and you want to display them in another. Here is a react component tree example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1008/1*abyfqddTG5g8yYV25zRfhA.png" /></figure><p>First, we need a new file to store the global state. Let’s call it userPostsState.ts. To create a state, we call createGlobalState which returns a <strong>pair</strong>. The first value is a react context (which we can use in a component by calling useContext(...)) and the second value is a context provider which is used to wrap a react component, in this case the Layoutcomponent. createGlobalState takes in a type (in brackets) and a default value, in this case the type is a UserPostsModel array and the default value is an empty array. Export both UserPostsContext and userPostsProvider, which are the only two things we need:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/258123c457d6e9a8fecf1c59437e95de/href">https://medium.com/media/258123c457d6e9a8fecf1c59437e95de/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4duWJtd3awHP2N9blqqJJA.png" /></figure><p>Find the top most component that is a parent to both AddUserPost and UserPosts (see diagram above). In this case, it is the Layout component, and wrap it withuserPostsProvider (so its children have access to the state):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b13ee5b1d7dcd5a73d36818e49039f3/href">https://medium.com/media/1b13ee5b1d7dcd5a73d36818e49039f3/href</a></iframe><p>The component AddUserPosts has a button that adds posts. We can use UserPostsContext with useContext like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/588d29fea54b8a54b12bd254d69dc884/href">https://medium.com/media/588d29fea54b8a54b12bd254d69dc884/href</a></iframe><p>Same thing goes for UserPosts, we also use UserPostsContext to display posts:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4439ea14d3041caaf793d4abdac370ac/href">https://medium.com/media/4439ea14d3041caaf793d4abdac370ac/href</a></iframe><p>And we are done! Any time a post is added in AddUserPost it automatically updates UserPosts to reflect that.</p><h3>Summary</h3><p>To create global state you have to follow those steps:</p><ol><li>Use createGlobalState with the default value and export the tuples</li><li>Wrap the topmost component, that is a parent of the components that need access to the data, with the provider</li><li>Use the context returned by createGlobalState with useContext in a component you need to get and modify the state.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b917bb19490" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Create Global State in React without Redux (TypeScript)]]></title>
            <link>https://codegem.medium.com/how-to-create-global-state-in-react-without-redux-typescript-928cb6fcd6ba?source=rss-19ba6c50a25f------2</link>
            <guid isPermaLink="false">https://medium.com/p/928cb6fcd6ba</guid>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[state-management]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Codegem]]></dc:creator>
            <pubDate>Wed, 20 Jan 2021 00:56:40 GMT</pubDate>
            <atom:updated>2021-01-20T01:03:43.415Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, we will show you <em>how</em> to build a reusable function that lets you create a global state. If you are only interested in how to use it and not the implementation details, jump over to <a href="https://codegem.medium.com/create-global-state-in-react-as-simply-as-local-state-7b917bb19490">this article</a>.</p><h3>Intro</h3><p>In some cases, two components that are not located close to each other in your react component tree need to <strong>share</strong> and <strong>modify</strong> data. For example Layout and UserPosts:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*25gm3tIYhJDi-3gE0MdOtw.png" /></figure><p>This component tree is simple enough to pass components down via props, but what if the components are not directly connected in a straight line? Like the SidePanel and UserPosts:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/1*9Qy99k_6QQySzFmi7DfHPg.png" /></figure><p>Redux is a possible fix to this problem. Although it has plenty of boilerplate and has a steep learning curve <em>especially</em> when not familiar with functional programming. In this article, we discuss how to implement a global state that can be both shared and modified.</p><blockquote>This tutorial includes Typescript, Higher Order Components, Functional Components, Generics and React Hooks.</blockquote><h3>App Overview</h3><p>We start with a simple web app that has a header, a side panel, and the main content area with user posts:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yGLmdmIlisGl3ACibdOaKQ.png" /></figure><p>You can find the source code <a href="https://github.com/code-gem/global_context">here</a>. There are three different branches between which you can switch:</p><ul><li><strong>master</strong>: this branch contains the app without any state</li><li><strong>1_simple_context</strong>: a simple implementation of the context and its provider wrapper (see Part 1 below)</li><li><strong>2_bidirectional_context</strong>: the provider wrapper holds state and can pass it down to its children to modify (see Part 2)</li><li><strong>3_generalized_context</strong>: refactored version of 2_bidirectional_context to be more generic and reusable (see Part 3)</li></ul><p>The side panel has a toggle (show blue posts) that hides or shows blue posts in the main content. The toggle has to communicate with the main content but this might be messy since the structure of the component tree looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ziMBeBJoAMbs0hMW3i7DlQ.png" /></figure><h3>Part 1 — Simple One-way Context</h3><p>The most common ancestor between the SidePanel and the Content is the component Layout. We can wrap Layout in a context provider that will pass down a boolean value to all its children:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XHoFZTAprkSP4GONufcdew.png" /></figure><p>First, we create the context using <a href="https://reactjs.org/docs/context.html">React’s API</a> nothing too fancy yet:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5be30d3d20c5bddb439e7a62cc9f9815/href">https://medium.com/media/5be30d3d20c5bddb439e7a62cc9f9815/href</a></iframe><p>Next is the context provider wrapper, which is a <strong>Higher Order Component</strong> that simply wraps any Component we pass to it with BluePostsContext:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/35addc01480ae4acee1a321c2c6f056a/href">https://medium.com/media/35addc01480ae4acee1a321c2c6f056a/href</a></iframe><p>We connect the context to the Layout component by simply wrapping its export with bluePostsProvider (notice here we have to provide the prop type of Layout since typescript doesn&#39;t recognize it):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6e4aca3d5cb67e4d3f928e189b00217a/href">https://medium.com/media/6e4aca3d5cb67e4d3f928e189b00217a/href</a></iframe><p>The SidePanel and the UserPosts can have access to data in the context using the <a href="https://reactjs.org/docs/hooks-reference.html#usecontext">useContext</a> hook API:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a8161c6da3f19bed5ea72a537710aa2/href">https://medium.com/media/3a8161c6da3f19bed5ea72a537710aa2/href</a></iframe><p>Components nested in the Layout component have access to this boolean value but none of them can modify it. In the second part, we show how to allow nested components to modify the context value by storing state in the provider.</p><h3>Part 2 — Bidirectional state</h3><p>For the children of Layout to modify the boolean value, the provider has to hold state:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IrqySCUvGyDojgbIANHsXw.png" /></figure><p>To add state to the provider we simply use useState. The context now should hold the value returned by useState and pass it down to all children. Keep in mind we are now passing around a boolean value wrapped in <em>react state</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b0e7c458c5cc45b9e98668f2fd4c761/href">https://medium.com/media/0b0e7c458c5cc45b9e98668f2fd4c761/href</a></iframe><p>The type returned by useState(true) is a pair [boolean, React.Dispatch&lt;React.SetStateAction&lt;boolean&gt;&gt;], and it is exactly the type the Context is holding. Since createContext is generic it needs to know about this type by passing it into angle brackets (&lt;&gt;). The default value can be anything since it is just a placeholder until the provider initializes useState. But in order to type-check we simply use [true, value =&gt; { }] as the default value:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/77470a1b93e2f7d14085faa6abca27e2/href">https://medium.com/media/77470a1b93e2f7d14085faa6abca27e2/href</a></iframe><p>The Layout component stays the same, but Panel.tsx and UserPosts.tsx change, since the context now returns a react state and not just a boolean variable. The only change here is to the left-hand side, it is now a pair much like what useState returns:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ff5abd8ecf1aafa07c0b82d35a6a12b/href">https://medium.com/media/0ff5abd8ecf1aafa07c0b82d35a6a12b/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79c28cb2cd02e4b227828eaecc9ecfdb/href">https://medium.com/media/79c28cb2cd02e4b227828eaecc9ecfdb/href</a></iframe><p>Here is the result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*Dac8HOD_LtfuKGiyK6APRA.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NAoUI3MPOYSwdY3VVW7Asg.png" /></figure><p>In a sense now useContext(BluePostsContext) behaves like a simple useState(true) but on a global scale! We can still improve upon this design since now you might be thinking what if I want to send a string instead of a boolean, or an array, or an object? We would have to copy the <em>context</em> and the <em>provider wrapper</em> again. To avoid that we make the context code generic and reusable in Part 3.</p><h3>Part 3 — A) Generalize global state</h3><p>Let’s recap, now our context.tsx looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b11f2a27a14dd3f3bf255f8544ed0c8f/href">https://medium.com/media/b11f2a27a14dd3f3bf255f8544ed0c8f/href</a></iframe><p>It is time to make this code reusable, so we will copy it over to a new file named globalState.tsx and combine both of these functions into another function called createGlobalState:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e716cd468dcf6f580738671fc3c7f4d5/href">https://medium.com/media/e716cd468dcf6f580738671fc3c7f4d5/href</a></iframe><p>We change the name of BluePostsContext to GlobalContext and bluePostsProvider to globalProvider:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc28559b37d02cf3a9d7dfa47316fde7/href">https://medium.com/media/cc28559b37d02cf3a9d7dfa47316fde7/href</a></iframe><p>createGlobalStateneeds to return GlobalContext and globalProvider, we can return them both as a tuple. If we simply return return [GlobalContext, globalProvider], Typescript will think that we are returning an array of two elements. To fix this issue we add a helper method called Pair and wrap the tuple with it to typecheck:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0cb5e7187e955e353496351417e24ea2/href">https://medium.com/media/0cb5e7187e955e353496351417e24ea2/href</a></iframe><p>The boolean type is hardcoded above so we need to make it generic by replacing it with a generic type parameter T:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe21f1b232a7b7ce5beb2f6841304f58/href">https://medium.com/media/fe21f1b232a7b7ce5beb2f6841304f58/href</a></iframe><p>Instead of injecting the default value through the provider function, we can inject it right away into the createGlobalState function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a5ea9b7b1ed2ae0f9c5dcdd090b0f954/href">https://medium.com/media/a5ea9b7b1ed2ae0f9c5dcdd090b0f954/href</a></iframe><p>And we are done! This is the function that is going to be reused anytime we need a global state.</p><h3>Part 3 — B) Using the new code</h3><p>Since we have the new generic code in globalState.tsx we do not need to ever modify that function, we only need to import it whenever we need to create a global state. The contextBluePostsContext in context.tsx simplifies to a one-liner:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a889b36366b787d40492f72659bd4034/href">https://medium.com/media/a889b36366b787d40492f72659bd4034/href</a></iframe><p>Now any time you need a global state it is as easy as calling createGlobalState with a default value! It returns a regular React context that can be used in useContext and a provider that wraps the top component.</p><h3>Conclusion</h3><p>Now a word about state management. Overusing this method might lead to some complex state logic that will be hard to debug especially with larger codebases and teams. It is by no means a replacement for Redux or other state management libraries so use it sparingly throughout your codebase. And remember, whenever you can use local state, do so!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=928cb6fcd6ba" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>