<?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>Forem: Brenden Olding</title>
    <description>The latest articles on Forem by Brenden Olding (@oldingb21).</description>
    <link>https://forem.com/oldingb21</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%2F1116493%2Fd276e23a-c65a-446f-826e-be49a719f71c.jpg</url>
      <title>Forem: Brenden Olding</title>
      <link>https://forem.com/oldingb21</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/oldingb21"/>
    <language>en</language>
    <item>
      <title>React Component Composition - Why does it matter?</title>
      <dc:creator>Brenden Olding</dc:creator>
      <pubDate>Sun, 08 Oct 2023 04:24:51 +0000</pubDate>
      <link>https://forem.com/oldingb21/react-component-composition-why-does-it-matter-4lk2</link>
      <guid>https://forem.com/oldingb21/react-component-composition-why-does-it-matter-4lk2</guid>
      <description>&lt;p&gt;In React there are a lot of flashy tools (hooks) that can do a lot of things for you. &lt;code&gt;useContext()&lt;/code&gt; has seen a lot of love as a way to avoid 'prop drilling', but what if I told you there was another way?&lt;/p&gt;

&lt;p&gt;If you are simply trying to avoid 'prop drilling' in React there is an incredible prop you can use to avoid that. The &lt;code&gt;children&lt;/code&gt; prop is under used, but incredibly powerful. &lt;/p&gt;

&lt;h2&gt;
  
  
  Children Prop - What is it?
&lt;/h2&gt;

&lt;p&gt;The children prop is &lt;em&gt;almost&lt;/em&gt; self-explanatory. You use the prop in your component as such:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Home&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Welcome to My Blog&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It may not seem like it is doing much in this instance, but it can open up a world of opportunities to avoid that pesky 'prop drilling'. In this example Home component, you can display nested components inside the Home component and go multiple layers deep without passing props through each layer. Say you have a component 3 nested components deep; you can pass the &lt;code&gt;children&lt;/code&gt; component to each of the 'parent' components and render each nested component as a child of the 'parent'. Here is what your App component could look like that in this instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Component1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Component2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;                                                
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DeepestComponent&lt;/span&gt; &lt;span class="na"&gt;neededProps&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/*whatever                      
      variables, functions, or state you need*/&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
     &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Component2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Component1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example instead of rendering Component1 inside the Home component, and Component 2 inside Component1, and the DeepestComponent inside of Component2, you render the &lt;code&gt;children&lt;/code&gt; prop inside each parent and use composition to your advantage. So, in this example you can avoid props drilling from App all the way down through Home, Component1, Component2, and finally the component that needs it, DeepestComponent. Instead, you just pass the props straight to the DeepestComponent from the App Component.&lt;/p&gt;

&lt;p&gt;There are of course drawbacks to this method, like "What if I need to pass props from Component1 to DeepestComponent?" or any other issues where state is necessary at multiple levels. In these cases, use &lt;code&gt;useContext()&lt;/code&gt;, but if you aren't encountering an issue like that, you can use Component composition to your advantage.&lt;/p&gt;

&lt;p&gt;Sources Referenced:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=3XaXKiXtNjw"&gt;Using Composition in React to Avoid "Prop Drilling"&lt;/a&gt;&lt;br&gt;
Flatiron School Curriculum on React Context and Children Props&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>frontend</category>
      <category>context</category>
    </item>
    <item>
      <title>JavaScipt Classes, What Are They?</title>
      <dc:creator>Brenden Olding</dc:creator>
      <pubDate>Sun, 16 Jul 2023 04:38:51 +0000</pubDate>
      <link>https://forem.com/oldingb21/javascipt-classes-what-are-they-173k</link>
      <guid>https://forem.com/oldingb21/javascipt-classes-what-are-they-173k</guid>
      <description>&lt;p&gt;Let's start with what a &lt;em&gt;class&lt;/em&gt; is in JavaScript. A &lt;em&gt;class&lt;/em&gt; is a function that allows you to create similar objects that adhere to the same structure. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Classes&lt;/em&gt; can be a really helpful tool in JavaScript. Here are a few examples of scenarios to consider using JavaScript &lt;em&gt;classes&lt;/em&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You need to create multiple objects that will hold    similar data, ex. a website comment, a website user, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you need to create functions to act on the data stored in similar objects, you can create methods on the &lt;em&gt;class&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You have a set of functions that will do similar things. &lt;br&gt;
This would act similarly to how the global Math object works. You would call the methods on the &lt;em&gt;class&lt;/em&gt; itself.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You want to organize your code to remove potentially redundant code.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to Create a Class.
&lt;/h2&gt;

&lt;p&gt;There are two ways you can define a &lt;em&gt;class&lt;/em&gt;, a &lt;em&gt;class declaration&lt;/em&gt; and a &lt;em&gt;class expression&lt;/em&gt;. Unlike functions, using a &lt;em&gt;class declaration&lt;/em&gt; acts more akin to variable declarations/expressions using const and let. In simpler terms, a &lt;em&gt;class&lt;/em&gt; does not get "hoisted" like a function using the function keyword.&lt;/p&gt;

&lt;p&gt;You will use the built in &lt;em&gt;constructor&lt;/em&gt; method to create the object key:value pairs. You pass parameters into the constructor method that represent the data you want to be stored. You then use the &lt;em&gt;this&lt;/em&gt; keyword to refer to the object you will be creating, with each instance of the &lt;em&gt;class&lt;/em&gt;, and create your keys using dot notation. &lt;/p&gt;

&lt;p&gt;Below is an example of how you might create a user &lt;em&gt;class&lt;/em&gt; for your program, with an example of both a declaration and expression.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Class Declaration&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//Class Expression&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to Create and Use Class Methods.
&lt;/h2&gt;

&lt;p&gt;Now you might be wondering how you create a method that acts on the date stored in an instance of a &lt;em&gt;class&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You would write the method outside of the constructor method in the &lt;em&gt;class&lt;/em&gt;. The syntax is similar to creating a function with the function keyword. The only difference is you omit the function keyword in a &lt;em&gt;class&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="c1"&gt;//below is the class method&lt;/span&gt;
   &lt;span class="nx"&gt;sayHello&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, welcome to the site!`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cm"&gt;/* If you want to invoke the method above, you have to create an instance of the class. */&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;david&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;David&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;27&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Chicago&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="cm"&gt;/* Using the variable representing the new User instance, you use dot notation to access the method. */&lt;/span&gt;

&lt;span class="nx"&gt;david&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;//=&amp;gt; 'Hello David, welcome to the site!'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you wanted to create a &lt;em&gt;class&lt;/em&gt; that was only built in methods, you wouldn't need to create new instances of the &lt;em&gt;class&lt;/em&gt;. Instead, you would call the methods on the &lt;em&gt;class&lt;/em&gt; itself. This is typically used to create math functions that are not in the global Math object. You can get creative and use it for other things as well. &lt;/p&gt;

&lt;p&gt;In summary, &lt;em&gt;classes&lt;/em&gt; are a powerful tool in JavaScript. They enable you to have an Object Oriented Programming mindset, focusing your code around cells of data rather than isolated bits of data. It can add structure and organization to your code. It can create easy ways for you to interact with an object's data. It also helps keep relevant data together. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Classes&lt;/em&gt; are an incredible tool to keep in your programming toolbox.&lt;/p&gt;

&lt;p&gt;Referenced Resources:&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"&gt;MDN- Classes - JavaScript&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.w3schools.com/Js/js_classes.asp"&gt;W3Schools - JavaScript Classes&lt;/a&gt;&lt;br&gt;
Flatiron School's Curriculum on Classes and OOP&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>productivity</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
