<?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 Jon-Collin Chinje on Medium]]></title>
        <description><![CDATA[Stories by Jon-Collin Chinje on Medium]]></description>
        <link>https://medium.com/@j.chinje?source=rss-d11683ddadc4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*0l35BR9HT7O34jNgHL7Vdg.jpeg</url>
            <title>Stories by Jon-Collin Chinje on Medium</title>
            <link>https://medium.com/@j.chinje?source=rss-d11683ddadc4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 11:36:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@j.chinje/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[A Useful Approach to Light / Dark Mode in your React App]]></title>
            <link>https://towardsdev.com/a-useful-approach-to-light-dark-mode-in-your-react-app-6d08ab6c5b4?source=rss-d11683ddadc4------2</link>
            <guid isPermaLink="false">https://medium.com/p/6d08ab6c5b4</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[tutorial]]></category>
            <dc:creator><![CDATA[Jon-Collin Chinje]]></dc:creator>
            <pubDate>Thu, 21 Apr 2022 11:55:58 GMT</pubDate>
            <atom:updated>2022-04-26T11:51:35.497Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*1GA3G_rzIDhaKKeQ" /><figcaption><a href="https://unsplash.com/@tegethoff">https://unsplash.com/@tegethoff</a></figcaption></figure><p>There are many ways to provide the users of your app with the ability to switch up the color theme on command. I’d like to talk about a method I found while building my most recent project, which relies on the useLocalStorage hook and CSS custom properties, grâce à<em> </em><a href="https://css-tricks.com/easy-dark-mode-and-multiple-color-themes-in-react/"><strong>Abram Thau</strong></a> — let’s get right into it.</p><p>Assuming you have your project set up, the first thing you’ll want to do is install the use-local-storage package; this will give you access to the useLocalStorage hook, which in our case will be used to persist our current theme beyond refresh (a very powerful tool —see another sample use <a href="https://usehooks.com/useLocalStorage/"><strong>here</strong></a>). The installation command is as follows (using your package manager of choice):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/690/1*QU0MFvzP5LwfF__LPO-KdQ.png" /></figure><p>Now that we have the hook, let’s use it. First, we need to import useLocalStorage from the package we just installed. Perform the following steps in your highest level component:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*ZjSj1ugRDLthuWJp7IzDOA.png" /></figure><p>Next, we’ll want to set up the hook to track the selected theme, as well as respect the default theme for returning users:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Uzcf0PVf7h__8ICQdmkCNQ.png" /></figure><p>This bit of code is checking to see if a color scheme preference exists in the browser settings, and then a local variable is being created. useLocalStorage looks and operates similarly to useState — but the value is persisted after refreshing. If defaultDark turns out to be true, then useLocalStorage rightly evaluates to theme: “dark”, otherwise it will initially be set to theme: “light”. From that initial value, the theme will toggle between the two when setTheme is called. Next, we’ll add a bit of code to the highest level element in our return:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/922/1*4E_DtNnTjUT90Bktjiy7yQ.png" /></figure><p>This bit of code is what communicates the theme stored by local storage to our app. Now that the app is listening for it, let’s give it some themes!</p><p>As pictured below — in your CSS file, you’ll want to add rules for <strong>root:</strong>, as well as <strong>[data-theme=‘light’] (</strong> note: you can add any number of themes using this syntax, as long as your app knows to watch for them).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1mqFQ0xw-omC6lIXN20vGw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1008/1*bhdjdqFAgw-AXdS1xKjblw.png" /></figure><p>This is all the initial setup you need! These CSS variables you’ve created can be used anywhere in your app, and the properties of all the elements assigned one of these variables will update each time {theme} gets updated. I particularly like this system because it helped me settle on a color palette for each theme; since all of my colors were in one place, I was able to make changes easily and see the effect everywhere, rather than one element at a time. That said, here is how you use these variables:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*4vaN1TojKId6QFJdP1MW6w.png" /></figure><p>Note the use of transition; this will give all transitions this element may go through (i.e. size, color) a short delay, which looks much more smooth than if the change were instant. Now, when {theme} gets toggled, the app background will toggle between the value assigned to root, and the value assigned to my light theme. Nice!</p><p>There are lots of ways you can implement this basic setup, and these variables don’t have to be used for just colors either, they can hold any CSS property. For some ideas, check out the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties"><strong>documentation</strong></a><strong>. </strong>Thanks to<strong> </strong>Abram Thau for sharing this method — to see it in practice, check out his <a href="https://css-tricks.com/author/abramthau/"><strong>blog</strong></a> for a live example, or check out the project I used it for —<strong> </strong><a href="https://tonebook.herokuapp.com/"><strong>ToneBook</strong></a>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6d08ab6c5b4" width="1" height="1" alt=""><hr><p><a href="https://towardsdev.com/a-useful-approach-to-light-dark-mode-in-your-react-app-6d08ab6c5b4">A Useful Approach to Light / Dark Mode in your React App</a> was originally published in <a href="https://towardsdev.com/">Towards Dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Site Navigation with the useHistory() Hook]]></title>
            <link>https://medium.com/@j.chinje/quick-guide-to-site-navigation-with-the-usehistory-hook-eceef7cbc6fa?source=rss-d11683ddadc4------2</link>
            <guid isPermaLink="false">https://medium.com/p/eceef7cbc6fa</guid>
            <category><![CDATA[react-router]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Jon-Collin Chinje]]></dc:creator>
            <pubDate>Thu, 31 Mar 2022 14:24:17 GMT</pubDate>
            <atom:updated>2022-03-31T22:22:08.075Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*V0R9TnYKo50vN__-" /><figcaption><a href="https://unsplash.com/@andrewtneel">https://unsplash.com/@andrewtneel</a></figcaption></figure><p>The useHistory() hook allows you to access the <strong>history</strong> instance via React Router — and it’s a popular hook for good reason. Using the history instance, you can easily redirect users to a page of your choosing from within any functional component. All you need to do to leverage this tool is import useHistory from react-router-dom:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/523/1*3UoaeB15w9CyiV-nS58Qcw.png" /></figure><p>Then declare a variable and set it equal to the hook:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*HJogerFiVOMkQBbT_MmwgA.png" /></figure><p>This history variable now gives you access to React’s record of where the user has been, and it enables you to stipulate where they will go next. The latter can be achieved using <strong>history.push. </strong>For example, upon a successful login, you may want to navigate to the home page, or the user’s profile — this is as easy as providing the new address as a parameter for history.push:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/496/1*Q7NZbZQdmnFJCDRBKqXBpA.png" /></figure><p>What’s literally happening here is the new path gets added to the history object that React is keeping track of. Because it is now the <em>most recent object in</em> <em>history</em>, React interprets it as the current destination and renders that page.</p><p>There are other abilities that the <strong>history stack</strong> — or the React’s store of all the history instances — gives you access to. While the most recent instance might often be the most useful, you can actually access any instance in the history stack.</p><p>Some other useful methods you can call on the history variable are <strong><em>go, goBack, goForward</em></strong><em>, </em><strong>and</strong> <strong><em>block</em></strong><em>.</em></p><p>These all perform the functions you might expect. <em>History.go()</em> will take in a number, and navigate to the object at that position in the stack. <em>History.goBack()</em> will call <em>history.go(-1)</em>, and <em>history.goForward</em> will call <em>history.go(1)</em>. <em>History.block()</em> is an interesting one. This will block navigation, and run a callback which it expects as a parameter (this callback could in itself be another redirect, conditionally bringing the user back to the login page if they aren’t signed in, for example).</p><p>As you can see, the useHistory() hook is a useful one to have in your React arsenal, and makes redirecting in response to user input a breeze. There are a number of more complex methods out there to accomplish the above tasks — if you find implementing them to be a struggle, give useHistory() a try!</p><p><em>For more information on useHistory as well as other useful hooks, check out </em><a href="https://www.geeksforgeeks.org/react-router-hooks/"><em>https://www.geeksforgeeks.org/react-router-hooks/</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eceef7cbc6fa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Here, There, and Everywhere: An Introduction to JavaScript Scope]]></title>
            <link>https://medium.com/@j.chinje/here-there-or-everywhere-an-introduction-to-javascript-scope-11c486670749?source=rss-d11683ddadc4------2</link>
            <guid isPermaLink="false">https://medium.com/p/11c486670749</guid>
            <category><![CDATA[scope-chains]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Jon-Collin Chinje]]></dc:creator>
            <pubDate>Thu, 10 Mar 2022 03:56:05 GMT</pubDate>
            <atom:updated>2024-03-01T03:20:31.584Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*ELMulY2uUbMNldsX" /><figcaption><a href="https://unsplash.com/@shanehauser">https://unsplash.com/@shanehauser</a></figcaption></figure><p>One of the very first concepts I was introduced to as part of learning JavaScript was <strong>scope</strong>. This concept is foundational to writing code, and important not only to understand, but to pay careful attention to over the course of a project.</p><p>Put simply, JavaScript scope is what determines accessibility to variables. When a new variable is created, its scope will depend on <em>where</em> it has been created — if it is <em>in scope</em> relative to where it is being used, there will be no issues; but if it is <em>out of scope</em>, it will not be available for use, and attempting to do so will cause an error. So, how is scope defined?</p><p>To understand that, it might be helpful to illustrate the idea. Per MDN Web Docs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DnDzi_8-6kDOQfUSUnnqHA.png" /><figcaption><a href="https://developer.mozilla.org/en-US/docs/Glossary/Scope">https://developer.mozilla.org/en-US/docs/Glossary/Scope</a></figcaption></figure><p>In this case, <strong>exampleFunction</strong> creates a new scope. Any variables declared inside of this function will be inaccessible outside of it. Logging the variable ‘x’ inside of exampleFunction will result in the log you might expect - “declared inside function”. Logging ‘x’ from outside of that function, or <em>scope,</em> will result in an error, because in that other scope, <em>x has not been defined.</em></p><p>You might ask which scope that is, exactly. The most general scope in which a variable can be defined is called <strong>global scope</strong>. The benefit of global scope is that variables defined there can be accessed from everywhere else in the code. A useful analogy might be to think of global scope as a hotel lobby: variables defined here are accessible to all of the functions staying at Code Hotel. Variables defined in a private room, however, are only for, and known to, the occupants of that room. To extend this idea, the private rooms also do not communicate with one another; variables defined in one function will show up as undefined in another.</p><p>So, why does any of this matter — <strong>why not just place <em>everything</em> in global scope? </strong>The short answer comes down to best practice. To keep your code clean and easy to follow, variables should generally be defined in the most specific scope in which they are needed. A global variable should need to be accessed globally, and a variable used only in one function should be defined inside of that function. To go back to our analogy — you <strong><em>could</em></strong><em> </em>unpack your suitcase in the hotel lobby if you wanted to, but you would probably draw some confused looks from any onlookers.</p><p><strong>Unless you suspect some of the other guests might want to borrow your new <em>const x = “pantaloons”</em>, it would make much more sense to unpack in your room.</strong></p><p>Another scenario to consider: how does scope work when a function is created <em>inside </em>another function? Here, we can introduce the idea of the <strong>scope chain</strong>. Without naming it explicitly, we’ve already explored how it works; the reason global variables are accessible everywhere else is due to the scope chain. Let’s look at an example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FK62XR_LPljlvVg82yM0jQ.png" /></figure><p><strong>X </strong>has been defined globally, and is accessed from within the function <strong>getDressed. </strong>The JavaScript engine works by first looking for the variable definition in its current scope, or <strong><em>local scope </em></strong><em>— </em>in this case, the function getDressed (and in our analogy, the hotel room). When it can’t find it there, it checks global scope (the hotel lobby), and finds it. No problem! Imagine the reverse scenario:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NdxBsVPgPb3ZgyDoOEQidw.png" /></figure><p>As we saw before, this will result in an error. We are trying to access <strong>x</strong> from the lobby, when it exists in a private room! <strong>The scope chain only works in one direction</strong>: if you can’t find your wallet inside your room, you might leave your <em>local scope </em>and check the hotel lobby for it. Thanks to the scope chain, this will not work in reverse. Someone in the lobby can’t enter your room looking for <em>your </em>wallet. JavaScript’s security measures are top notch.</p><p>Now that we understand the scope chain, lets go back to our question of a function within a function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DK0-X1tubJihVKRiBW0Bzw.png" /></figure><p>We’ve already established that the scope chain only works in one direction, from most specific to most general. Functions can look ‘up’ into the global scope for any variable definitions, but a variable used in the global scope cannot look ‘down’ into functions. The same thing applies in the example above. When <strong>outerFunction</strong> can’t find a definition for <strong>x</strong> within local scope, it looks ‘up’ into global scope before successfully logging x. Similarly, <strong>innerFunction </strong>checks local scope first, then looks ‘up’, into <strong>outerFunction </strong>— and finally into global scope, before successfully logging x. In the reverse scenario, however, things don’t go so smoothly:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l1Im4rPksonlsUmkXevkRA.png" /></figure><p>In this case, global scope has nowhere to look but itself, where <strong>x</strong> is not defined, so an error is returned right away. When <strong>outerFunction</strong> fails to find <strong>x </strong>in local scope<strong>, </strong>it looks ‘up’ into <strong>global scope,</strong> where it finds no definition and returns an error. Neither of the first two console-logs are allowed to look ‘down’ the chain, into <strong>innerFunction, </strong>because <strong>the scope chain only works in one direction.</strong></p><p>So, those are the basics of scope in JavaScript. You can delve much deeper into how scope works, and how to manipulate it to suit your needs, but the above information is a good base level to be on when first approaching the subject.</p><p>To sum up; scope determines access to variables. <strong>Global scope is the largest scope</strong>, and can be accessed from everywhere else in the code. Functions have their own scope which can’t be accessed from outside the function, and functions within functions have <em>their </em>own scope, which similarly cannot be accessed from outside, including by their associated outer function. All of this is thanks to the scope chain, which only flows in <strong>one direction, </strong>from most specific (the current, local scope), to most general (the global scope)<strong>.</strong></p><p><em>For further reading, check out the MDN documents on scope and closures:</em></p><p><a href="https://developer.mozilla.org/en-US/docs/Glossary/Scope"><em>https://developer.mozilla.org/en-US/docs/Glossary/Scope</em></a></p><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures"><em>https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=11c486670749" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>