<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Garrett Omi</title>
    <description>The latest articles on DEV Community by Garrett Omi (@garrettomi).</description>
    <link>https://dev.to/garrettomi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1151131%2F6655a0a5-c76f-4620-b026-034d2025a51e.jpeg</url>
      <title>DEV Community: Garrett Omi</title>
      <link>https://dev.to/garrettomi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/garrettomi"/>
    <language>en</language>
    <item>
      <title>Avoid Prop Drilling With useContext</title>
      <dc:creator>Garrett Omi</dc:creator>
      <pubDate>Thu, 23 Nov 2023 22:57:22 +0000</pubDate>
      <link>https://dev.to/garrettomi/avoid-prop-drilling-with-usecontext-f4e</link>
      <guid>https://dev.to/garrettomi/avoid-prop-drilling-with-usecontext-f4e</guid>
      <description>&lt;p&gt;Bothering the team with all that noise coming from your prop drill? Let’s pause and take a look at a better solution before you dig yourself a hole you can’t get out of: &lt;code&gt;useContext&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;When using React, sharing data across components is a give-in – it’s one of the finer features of React which allows for seamless data efficient management, especially when it comes to dealing with larger applications with multiple layers of components. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3io27dl58d1nqeakya15.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3io27dl58d1nqeakya15.jpg" alt="React graph" width="800" height="266"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For those unfamiliar to React or those just getting started, this is generally done through the downward passing of data, or “props”, from parent components to their children. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6inx672jrdrh6x4jir2t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6inx672jrdrh6x4jir2t.png" alt="Parent to child relationship React" width="243" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But what happens when you’re dealing with deeply nested components or when multiple components need to get the same data? &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foib81ry6ij2dvd99s12t.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foib81ry6ij2dvd99s12t.jpg" alt="Russian dolls" width="800" height="531"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is exactly where the infamous “prop drilling” can become deceptively appealing, where one would continuously pass down props component after component until the desired child receives the initial prop -- regardless of how many children the props get passed through.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54jjjari78hoiwppxkzc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54jjjari78hoiwppxkzc.png" alt="Diagram of Prop Drilling" width="800" height="516"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While this bruteforce solution can work, good luck trying to follow that trail back up if something, somewhere goes unexpected in the data transmission between components. The higher the volume of drilled components, the higher the difficulty becomes in maintaining and identifying expected behavior and outcomes throughout the nested levels.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkvqxutzaxt0dhsjqtt3j.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkvqxutzaxt0dhsjqtt3j.jpg" alt="Made in the Abyss anime scene" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  So why not use a better solution: useContext?
&lt;/h2&gt;

&lt;p&gt;By allowing better optimized and safe data sharing across multiple components, &lt;code&gt;useContext&lt;/code&gt; is absolutely a viable option to avoid a predicament where props are being passed down a dangerous shaft of varying decrees.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is useContext?
&lt;/h2&gt;

&lt;p&gt;In a nutshell, &lt;code&gt;useContext&lt;/code&gt; is a React Hook which, as implied in the name, allows you to &lt;strong&gt;read&lt;/strong&gt; and &lt;strong&gt;subscribe&lt;/strong&gt; to a specific &lt;em&gt;context&lt;/em&gt; which you’ve built to encompass your component tree. &lt;/p&gt;

&lt;p&gt;When called, the data will appear in a context object which returns the current context value for that context. &lt;/p&gt;

&lt;p&gt;Yes, I do realize that’s a lot of &lt;em&gt;context&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fldpqg2m8ze8ud2iwop5c.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fldpqg2m8ze8ud2iwop5c.jpg" alt="Buzz Lightyear Context Meme" width="375" height="285"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Put it this way – a &lt;em&gt;context&lt;/em&gt; is like &lt;strong&gt;a magic cloak&lt;/strong&gt; that surrounds your component and any of the children subscribed to that component. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff8thmeyyugl0hc4p464n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff8thmeyyugl0hc4p464n.png" alt="cloak example" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Any component falling beneath this magical cloak has been granted the ability to call the &lt;code&gt;useContext&lt;/code&gt; hook in order to get the predefined information written from the top at &lt;em&gt;any level&lt;/em&gt; of your component tree without having to manually prop pass. &lt;/p&gt;

&lt;p&gt;As a &lt;em&gt;Harry Potter&lt;/em&gt; fan, I kind of like to think of &lt;code&gt;useContext&lt;/code&gt; like the &lt;em&gt;Accio&lt;/em&gt; spell -- just like how the &lt;em&gt;Accio&lt;/em&gt; spell works in retrieving an item called from anywhere so long as its existence is within the vicinity, the &lt;code&gt;useContext&lt;/code&gt; hook can only call the information if it's been defined and wrapped around the appropriate components through a top level context.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feq5s1rhl5rbduj92yxbr.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feq5s1rhl5rbduj92yxbr.gif" alt="Harry Potter Accio example" width="400" height="165"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  How to use useContext
&lt;/h2&gt;

&lt;p&gt;Essentially, using &lt;code&gt;useContext&lt;/code&gt; comes down to three fundamental steps: &lt;strong&gt;Create&lt;/strong&gt;, &lt;strong&gt;Provide&lt;/strong&gt;, and &lt;strong&gt;Consume&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  0. [OPTIONAL] Set up your files:
&lt;/h2&gt;

&lt;p&gt;Okay I lied -- this is an additional step and while this isn't particularly mandatory, it's good practice to structure your contexts in a context folder at the same level as your components directly beneath the &lt;code&gt;src&lt;/code&gt; folder. For reference, this is what my file structure looks like at a basic level:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7zyzzbis9egwf1two3ri.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7zyzzbis9egwf1two3ri.png" alt="React file structure" width="336" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Create a Context:
&lt;/h2&gt;

&lt;p&gt;The first step is to &lt;em&gt;create&lt;/em&gt; your context which is simply done by using the &lt;code&gt;createContext&lt;/code&gt; function from React. &lt;/p&gt;

&lt;p&gt;In line with my comparison to the context being a magical cloak, I'm going to call my newly created context &lt;code&gt;MagicCloakContext&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgbcjx157ob4wmrrt3fsc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgbcjx157ob4wmrrt3fsc.png" alt="Create-Context code example" width="800" height="151"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Provide the Context:
&lt;/h2&gt;

&lt;p&gt;Now to &lt;em&gt;provide&lt;/em&gt; the context, you need to wrap the part of your component tree that should have access to the context in a new &lt;code&gt;Provider&lt;/code&gt; component. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;createContext&lt;/code&gt; is a function which returns an object with a &lt;code&gt;Provider&lt;/code&gt; component so in order to provide the context you created in step 1, append your context with a dot and add the &lt;code&gt;Provider&lt;/code&gt; key to the context you created in step 1:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MagicCloakContext.Provider&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And once you identify the component(s) you'd like to apply your context to, wrap, or &lt;em&gt;provide&lt;/em&gt;, your context around the targeted component(s) with your newly made &lt;code&gt;Provider&lt;/code&gt; component:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;MagicCloakContext.Provider&amp;gt;&lt;br&gt;
&amp;lt;ChildComponent /&amp;gt;&lt;br&gt;
&amp;lt;MagicCloakConext.Provider /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Provider&lt;/code&gt; will then take a &lt;code&gt;value&lt;/code&gt; prop which will be the current value of the context, or the desired information you want to retrieve, which you can then access in the component(s) and all of their affiliated children.&lt;/p&gt;

&lt;p&gt;As you can see in my example, in my &lt;code&gt;App&lt;/code&gt; component, I am defining a variable called &lt;code&gt;data&lt;/code&gt; with a string value of &lt;code&gt;"Accio data!"&lt;/code&gt; which is being passed into my &lt;code&gt;Provider&lt;/code&gt; component's &lt;code&gt;value&lt;/code&gt; prop and is the information which I'll want to retrieve throughout my application:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1zzec2n9o6pucppanmos.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1zzec2n9o6pucppanmos.png" alt="Provider context code example" width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Consume the Context
&lt;/h2&gt;

&lt;p&gt;Lastly, to access, or &lt;em&gt;consume&lt;/em&gt; the data set up by the context, this is where some paths diverge.&lt;/p&gt;

&lt;p&gt;While this may sound confusing, just like the &lt;code&gt;Provider&lt;/code&gt; component attached to &lt;code&gt;createContext&lt;/code&gt;, there's also a &lt;code&gt;Consumer&lt;/code&gt; component which is where step 3 "consume" comes from. But to not overly complicate things and for the purpose of this guide in following modern best practices up to this date, don't worry about the &lt;code&gt;Consumer&lt;/code&gt; as it was replaced with a more elegant and straightforward approach to consuming the context through the &lt;code&gt;useContext&lt;/code&gt; hook. If you are curious about the &lt;code&gt;Consumer&lt;/code&gt; component, please refer to the official React documentation on &lt;code&gt;createContext&lt;/code&gt; &lt;a href="https://react.dev/reference/react/createContext" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now that we've got that disclaimer out of the way, let's use the &lt;code&gt;useContext&lt;/code&gt; hook, which consumes the context object created from &lt;code&gt;createContext&lt;/code&gt; and returns its current value.&lt;/p&gt;

&lt;p&gt;In my code below, you can see that inside &lt;code&gt;ChildComponent1&lt;/code&gt;, we're defining a variable &lt;code&gt;data&lt;/code&gt; with the &lt;code&gt;MagicCloakContext&lt;/code&gt; being &lt;em&gt;consumed&lt;/em&gt; by the &lt;code&gt;useContext&lt;/code&gt; hook. I am then passing this &lt;code&gt;data&lt;/code&gt; variable into a &lt;code&gt;div&lt;/code&gt; block...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ywudci5sbxq2digr6ur.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ywudci5sbxq2digr6ur.png" alt="Consume context code example" width="800" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;...and since everything worked out, when we run our application, we now see the words "Child Component 1: Accio data!" on the browser:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0u59fie7rhhtk7b5vjbt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0u59fie7rhhtk7b5vjbt.png" alt="Browser example" width="618" height="238"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pretty cool stuff, huh? Let's go over a couple more examples to better visualize some other use cases of &lt;code&gt;useContext&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Multiple children example
&lt;/h2&gt;

&lt;p&gt;Here's an example where I wrap multiple components in my &lt;code&gt;MagicCloakContext&lt;/code&gt;: &lt;code&gt;ChildComponent1&lt;/code&gt;, &lt;code&gt;ChildComponent2&lt;/code&gt;, and &lt;code&gt;ChildComponent3&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Again, since all these components are wrapped in our &lt;code&gt;MagicCloakContext&lt;/code&gt;, they should be able to use &lt;code&gt;useContext&lt;/code&gt; in order to access our "Accio Data!" without any issues with prop passing like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcn7gugvvvvgkercsqjtq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcn7gugvvvvgkercsqjtq.png" alt="Multiple children in App code example" width="800" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcj3eb3h1a2qkrcvrp3zx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcj3eb3h1a2qkrcvrp3zx.png" alt="Child component 2 code example" width="800" height="399"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faez91s9i0cbonmfcpoyr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faez91s9i0cbonmfcpoyr.png" alt="Child component 3 code example" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F43l0tfkldr8agnt2n15u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F43l0tfkldr8agnt2n15u.png" alt="Browser example" width="672" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Nested Child example
&lt;/h2&gt;

&lt;p&gt;And if this isn't overkill enough, here's a final example of a situation with a triple nested child component inside &lt;code&gt;ChildComponent1&lt;/code&gt; where we once before would have considered prop drilling, but now don't have to think about it due to the power of &lt;code&gt;useContext&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6uxnt33u711uzqtm2gv9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6uxnt33u711uzqtm2gv9.png" alt="Child Component 1 code example" width="800" height="368"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmcpzd3hjztusy9pw7s0i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmcpzd3hjztusy9pw7s0i.png" alt="Nested Child Example" width="800" height="322"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fztqgfdz6pw8teherchrs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fztqgfdz6pw8teherchrs.png" alt="Nested Nested Child Example" width="800" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftoi3674g2lnkul5lyf47.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftoi3674g2lnkul5lyf47.png" alt="Nested Nested Nested Child Example" width="800" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F55t1ax8v9q1tggm97eye.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F55t1ax8v9q1tggm97eye.png" alt="Browser Example for Nested squared example" width="800" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  When To Use / Practical Use Cases
&lt;/h2&gt;

&lt;p&gt;So of course, the examples above only outline a rudimentary example of how to use and visualize &lt;code&gt;useContext&lt;/code&gt; through simple string data in contention to prop drilling. &lt;/p&gt;

&lt;p&gt;But if you're curious about other use cases to fully take advantage of &lt;code&gt;useContext&lt;/code&gt;, it's also really great for managing &lt;strong&gt;user authentication&lt;/strong&gt;, &lt;strong&gt;theme switching&lt;/strong&gt;, &lt;strong&gt;language localization&lt;/strong&gt;, &lt;strong&gt;sharing state between components&lt;/strong&gt; and &lt;strong&gt;styling&lt;/strong&gt; to name a few. &lt;/p&gt;

&lt;p&gt;Furthermore although there are some cases where Redux or other library solutions may work better for managing your global state on larger scale more complex applications, &lt;code&gt;useContext&lt;/code&gt; isn't a bad solution when you're looking for a simple and convenient way to share information and/or state with React. &lt;/p&gt;

&lt;p&gt;While all of these cases merit attention and exploration, for the sake of this article and sticking to the basics, if you're interested in learning more about any of the above points, I'd be more than happy to write a separate article in the future to further expand on these points.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;So hopefully this read was able to give you a good visual and better clarity on the basics of &lt;code&gt;useContext&lt;/code&gt;. At the very least, I hope that after reading this article, if you ever feel tempted to grab that prop drill, you'll be reminded of this article and are able to prevent yourself from a despaired life of endless digging. Thank you for checking out my article and let me know if you have any comments or questions at any time -- happy hacking!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>react</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Building the "Travel Tracker": My Homage to Instagram's Lost Map Feature</title>
      <dc:creator>Garrett Omi</dc:creator>
      <pubDate>Fri, 01 Sep 2023 04:25:40 +0000</pubDate>
      <link>https://dev.to/garrettomi/building-the-travel-tracker-my-homage-to-instagrams-lost-map-feature-k5o</link>
      <guid>https://dev.to/garrettomi/building-the-travel-tracker-my-homage-to-instagrams-lost-map-feature-k5o</guid>
      <description>&lt;h2&gt;
  
  
  The Spark of Inspiration
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wkuJChfs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kbutdtje2c2ft0nblnrz.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wkuJChfs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kbutdtje2c2ft0nblnrz.jpg" alt="Image description" width="787" height="809"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once upon a time, Instagram was my go-to app, not for its filters or stories, but for a single, simple feature - its interactive world map. As a world traveler who grew up with printed photos and tangible travel scrapbooks, Instagram's map feature was a digital revolution. It felt like having the entire world at my fingertips, with each pin on the map a memory, a story, a piece of my journey etched in the virtual world.&lt;/p&gt;

&lt;p&gt;However, like all good things, it came to an abrupt end. Whether it was due to privacy concerns or a shift towards interactive reels and bite-sized video snippets, the map feature was scrapped, and with it, my interest in Instagram. But the concept stuck with me, and today, it became the inspiration for my project, the "Travel Tracker".&lt;/p&gt;

&lt;h2&gt;
  
  
  The Birth of "Travel Tracker"
&lt;/h2&gt;

&lt;p&gt;Flash forward to the present, I found myself taking the Kintone’s REST API workshop at Code Chrysalis, where we were incorporating React and Kintone’S REST API to record countries, states, and cities. As I interacted with others in the workshop debating whether or not they should enter Kintone’s contest, a memory of Instagram's map feature resurfaced, and I was struck with a wave of nostalgia and inspiration. Thus, "Travel Tracker" was born - my modern rendition of the digital travel scrapbook.&lt;/p&gt;

&lt;p&gt;The goal was simple: create an application that allows users to mark locations they have visited on a map, upload photos, and curate their personal travel experiences, just like the good old days of Instagram's map feature.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WSi9RhFI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/910pad46q6g6sog420an.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WSi9RhFI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/910pad46q6g6sog420an.jpg" alt="Image description" width="800" height="423"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Technology Stack
&lt;/h2&gt;

&lt;p&gt;The "Travel Tracker" application features a diverse technology stack that ensures smooth operation of both the frontend and backend. For the frontend, I used React.js mainly, utilizing components for efficient organization and functionality. I employed CSS for styling to ensure a visually pleasing and user-friendly interface. I integrated the Mapbox API to facilitate interactive maps with dynamic markers, enhancing the user experience.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---vj4_3sR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8pqg67ze0y5jjibyyzc3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---vj4_3sR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8pqg67ze0y5jjibyyzc3.jpg" alt="Image description" width="800" height="417"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The geolocation aspect is powered by the Country-State-City React library, enabling seamless integration with Kintone's REST API for data communication. For user authentication and login functionality, I leveraged Firebase, ensuring secure access. Cloudinary serves as the image hosting solution, enabling users to upload and display images effortlessly. I deployed the frontend on Vercel, providing scalability and fast loading times.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8pHGmrBa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ea08lo1y07h3sb46n9m6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8pHGmrBa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ea08lo1y07h3sb46n9m6.jpg" alt="Image description" width="800" height="206"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the backend, I used Node.js and Express to create a robust foundation for handling data interactions. I harnessed the GET and POST functionalities of the Kintone REST API to manage data retrieval and storage. The backend is deployed on Render for reliable performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Overcoming Challenges
&lt;/h2&gt;

&lt;p&gt;Building "Travel Tracker" was a journey in itself, filled with a few challenges. One of them was linking relational data, such as user information and location data, in Kintone's nonrelational database. To address this, although I could have used Kintone's relational database plugin, I employed Firebase's serverside authentication database to store user data and utilized React's useContext to facilitate the retrieval and storage of user information upon location submission.&lt;/p&gt;

&lt;p&gt;Another hurdle was dealing with Kintone's database's limitations on data values that could be stored. For instance, I needed to store an array of geocoordinates as individual integer values. With a bit of problem-solving, I was able to overcome these challenges, resulting in an overall smoother user experience and a deployed application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Future Prospects
&lt;/h2&gt;

&lt;p&gt;As the "Travel Tracker" application takes its first steps, the journey ahead is still far. I envision future features such as user interaction capabilities, enabling users to follow and engage with each other's travel journeys. Enhanced functionalities such as tagging other users, bookmarking favorite photos, and uploading multiple images to a single location with descriptions could further enrich the user experience. I hope to see "Travel Tracker" transform into a thriving community of travel enthusiasts, sharing their adventures in a dynamic and interactive manner from both those who remember the good old days of Instagram’s past, to those who never got to grow up with it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Building the "Travel Tracker" application was an enjoyable and nostalgic trip down memory lane. Kintone's REST API was extremely user-friendly and easy to use, without the hassle of micromanaging migrations and rolling back databases, which could have affected overall application performance. I'd like to extend my gratitude to Kintone for providing the opportunity to utilize and participate in their contest. As the project evolves, I eagerly look forward to seeing the growth and expansion of a dynamic and thriving user community!&lt;/p&gt;

&lt;p&gt;Travel Tracker application: &lt;br&gt;
&lt;a href="https://kintone-travel-tracker.vercel.app/login"&gt;https://kintone-travel-tracker.vercel.app/login&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Linkedin:&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/garrett-omi/"&gt;https://www.linkedin.com/in/garrett-omi/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Github handle: &lt;br&gt;
&lt;a class="mentioned-user" href="https://dev.to/garrettomi"&gt;@garrettomi&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;_This post is part of the Kintone Customization Contest 2023.&lt;br&gt;&lt;br&gt;
Submitter: &lt;a href="https://forum.kintone.dev/u/Garrett"&gt;https://forum.kintone.dev/u/Garrett&lt;/a&gt;  _&lt;/p&gt;

</description>
      <category>kintone</category>
      <category>kintonecontest</category>
    </item>
  </channel>
</rss>
