<?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: rossonevan</title>
    <description>The latest articles on Forem by rossonevan (@rossonevan).</description>
    <link>https://forem.com/rossonevan</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%2F883462%2Fbd45aa76-dcd7-4124-b4ba-36e30ae9d7ab.png</url>
      <title>Forem: rossonevan</title>
      <link>https://forem.com/rossonevan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rossonevan"/>
    <language>en</language>
    <item>
      <title>My Bootcamp Experience</title>
      <dc:creator>rossonevan</dc:creator>
      <pubDate>Mon, 03 Oct 2022 15:15:28 +0000</pubDate>
      <link>https://forem.com/rossonevan/my-bootcamp-experience-45nc</link>
      <guid>https://forem.com/rossonevan/my-bootcamp-experience-45nc</guid>
      <description>&lt;p&gt;For the last 4 months, I have been attending the Flatiron bootcamp in the Software Engineering course. With just one more week to go, I thought I'd write down my experience throughout the course. &lt;/p&gt;

&lt;p&gt;When I first started the bootcamp, I was feeling super nervous and overwhelmed with the amount of information we would be learning in such a short amount of time. There were four phases, each phase was 3 weeks of: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;learning something new the first week&lt;/li&gt;
&lt;li&gt;taking a coding challenge on this new information the second week&lt;/li&gt;
&lt;li&gt;creating an application using this information for the third week.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The fifth and final phase was creating an application on your own, utilizing all the skills learned throughout the program. &lt;/p&gt;

&lt;p&gt;For someone who only had a little experience in coding, the first couple weeks of learning vanilla JavaScript was the toughest hurdle. There was so much information to understand and implement, easily overwhelming me to reconsider doing this course. But with enough perseverance and help from my peers, I managed to pull through and pass the phase. &lt;/p&gt;

&lt;p&gt;As the phases went on, I started to become more comfortable with the scheduling and structure of the bootcamp and less overwhelmed. Even though we were still learning new skills for each phase, I felt I was getting better at learning new skills in the short amount of time and implementing them into my applications. Each code challenge was starting to become easier and easier as I started to understand the material better.&lt;/p&gt;

&lt;p&gt;As I was in my first phase, I remember seeing students' applications for their last phase and could never see myself ever accomplishing what they have. But now that I am in my last phase, I can confidently say that I have proven myself wrong. I have acquired the skills those students learned as they went through this program and hopefully will inspire other students with what I have achieved through my own project. &lt;/p&gt;

&lt;p&gt;But the most important part of the bootcamp is the amazing people I have met that have helped me get through the toughest of times. All of us were going through the same struggles and were always there for one another. I never felt like I was alone and for that I am truly thankful for.&lt;/p&gt;

&lt;p&gt;All in all, I am grateful that I considered this bootcamp and never gave up, even though there were times I felt like I couldn't get through it. The skills I have picked up will stick with me for the rest of my life and I couldn't recommend going through this program enough!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Generate more detailed errors in controllers using 'rescue_from' in Ruby on Rails</title>
      <dc:creator>rossonevan</dc:creator>
      <pubDate>Mon, 12 Sep 2022 22:06:22 +0000</pubDate>
      <link>https://forem.com/rossonevan/generate-more-detailed-errors-in-controllers-using-rescuefrom-in-ruby-on-rails-12i0</link>
      <guid>https://forem.com/rossonevan/generate-more-detailed-errors-in-controllers-using-rescuefrom-in-ruby-on-rails-12i0</guid>
      <description>&lt;p&gt;The most important parts of a web page application is returning information to the user while providing errors in order for a better understanding of why a problem is occurring when a user is interacting throughout the app. Through the application controllers, CRUD(Create, Read, Update, Destroy) methods are developed in order for the front-end to interact with the database created in the back-end.&lt;/p&gt;

&lt;h2&gt;
  
  
  .find vs .find_by
&lt;/h2&gt;

&lt;p&gt;When creating the &lt;code&gt;create&lt;/code&gt;, &lt;code&gt;show&lt;/code&gt;, and &lt;code&gt;update&lt;/code&gt; routes, the id of the data being displayed must be found. If the id cannot be found, an error will occur. For example, lets say we have a show methods in a movie controller:&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;MoviesController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="nx"&gt;def&lt;/span&gt; &lt;span class="nx"&gt;show&lt;/span&gt;
    &lt;span class="nx"&gt;movie&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Movie&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;find_by&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;movie&lt;/span&gt;
      &lt;span class="nx"&gt;render&lt;/span&gt; &lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;movie&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
      &lt;span class="nx"&gt;render&lt;/span&gt; &lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Movie not found&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;not_found&lt;/span&gt;
    &lt;span class="nx"&gt;end&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;
 &lt;span class="nx"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using &lt;code&gt;.find_by&lt;/code&gt;, the return value is &lt;code&gt;nil&lt;/code&gt; if the movie cannot be found. Therefore, an &lt;code&gt;if/else&lt;/code&gt; statement needs to be formulated in order to generate an error that will be used to display to the user.&lt;/p&gt;

&lt;p&gt;We can also use a &lt;code&gt;rescue&lt;/code&gt; that ruby provides that can take the exception and use it as the error, replacing the &lt;code&gt;if/else&lt;/code&gt; statement for a more DRY approach:&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;MoviesController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="nx"&gt;def&lt;/span&gt; &lt;span class="nx"&gt;show&lt;/span&gt;
    &lt;span class="nx"&gt;movie&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Movie&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;find_by&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="nx"&gt;render&lt;/span&gt; &lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;movie&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;
&lt;span class="nx"&gt;rescue&lt;/span&gt; &lt;span class="nx"&gt;ActiveRecord&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nx"&gt;RecordNotFound&lt;/span&gt;
  &lt;span class="nx"&gt;render&lt;/span&gt; &lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Movie not found&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;not_found&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;
 &lt;span class="nx"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But there is an even DRY-er way for handling errors. If we use the &lt;code&gt;.find&lt;/code&gt; method, ActiveRecord returns an exception that contains a more detailed error. For example, if we make a fetch request to &lt;code&gt;localhost:3000/movies/99&lt;/code&gt;, the exception will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ActiveRecord::RecordNotFound (Couldn't find Movie with 'id'=99)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use this exception to generate our error and apply it to every controller with a &lt;code&gt;RecordNotFound&lt;/code&gt; error but writing our rescue in the &lt;code&gt;ApplicationController&lt;/code&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;ApplicationController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;ActionController&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nx"&gt;API&lt;/span&gt;
  &lt;span class="nx"&gt;rescue_from&lt;/span&gt; &lt;span class="nx"&gt;ActiveRecord&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nx"&gt;RecordNotFound&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;record_not_found&lt;/span&gt;
  &lt;span class="kr"&gt;private&lt;/span&gt;
  &lt;span class="nx"&gt;def&lt;/span&gt; &lt;span class="nx"&gt;record_not_found&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;render&lt;/span&gt; &lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;not_found&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;
&lt;span class="nx"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the error is being handled inside our "top" controller, the methods in the &lt;code&gt;MoviesController&lt;/code&gt; do not need to contain errors and therefore create a more DRY form:&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;MoviesController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="nx"&gt;def&lt;/span&gt; &lt;span class="nx"&gt;show&lt;/span&gt;
    &lt;span class="nx"&gt;movie&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Movie&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="nx"&gt;render&lt;/span&gt; &lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;movie&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;
 &lt;span class="nx"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>ruby</category>
      <category>rails</category>
      <category>programming</category>
    </item>
    <item>
      <title>Ruby: SELF in instance and class methods</title>
      <dc:creator>rossonevan</dc:creator>
      <pubDate>Tue, 23 Aug 2022 14:15:00 +0000</pubDate>
      <link>https://forem.com/rossonevan/ruby-self-in-instance-and-class-methods-4j3g</link>
      <guid>https://forem.com/rossonevan/ruby-self-in-instance-and-class-methods-4j3g</guid>
      <description>&lt;p&gt;After learning about the front-end of web development through JavaScript and React, it was time to venture to the back-end, Ruby. &lt;a href="https://en.wikipedia.org/wiki/Object-oriented_programming"&gt;Object-Oriented Programming&lt;/a&gt; (OOP) is a type of programming based on the concept of "objects", which can contain data, in the form of fields (often known as attributes or properties) and code, in the form of procedures (also known as methods). OOP allows web developers to structure code so that its functionality can be shared throughout the application. &lt;/p&gt;

&lt;h2&gt;
  
  
  Self in Instance Methods
&lt;/h2&gt;

&lt;p&gt;Ruby, because of OOP, uses classes to identify their objects. A class is like a template, or a blueprint, for creating objects with similar characteristics. When creating a class, each new instance of that class is called an object. This &lt;em&gt;object&lt;/em&gt; is a bundle of code that contains both data and behaviors. Inside each class, developers can create numerous instance methods.&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;Game&lt;/span&gt;

  &lt;span class="nx"&gt;attr_accessor&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;

  &lt;span class="nx"&gt;def&lt;/span&gt; &lt;span class="nx"&gt;initialize&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;

  &lt;span class="nx"&gt;def&lt;/span&gt; &lt;span class="nx"&gt;developer_of_game&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;developer_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="nx"&gt;developer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;developer_name&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;
&lt;span class="nx"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, we use the &lt;code&gt;attr_accessor&lt;/code&gt; macro in order to make changes to the &lt;code&gt;name&lt;/code&gt; attribute. The &lt;code&gt;#developer_of_game&lt;/code&gt; instance method takes in an object of &lt;code&gt;Game&lt;/code&gt; and assigns a name to that instance. But there is an easier, DRY-er, way we can get the same result: by using &lt;code&gt;self&lt;/code&gt;. &lt;code&gt;self&lt;/code&gt; refers to whatever instance the method is called on.&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;Game&lt;/span&gt;

  &lt;span class="nx"&gt;attr_accessor&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;

  &lt;span class="nx"&gt;def&lt;/span&gt; &lt;span class="nx"&gt;initialize&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;

  &lt;span class="nx"&gt;def&lt;/span&gt; &lt;span class="nx"&gt;developer_of_game&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;developer_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;developer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;developer_name&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;
&lt;span class="nx"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;elden_ring = Game.new('Elden Ring')
elden_ring.developer_of_game('FromSoftware')
elden_ring.developer
# =&amp;gt; 'FromSoftware'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By placing &lt;code&gt;self&lt;/code&gt; inside the instance method, the method takes the specific object that we created and applies it to the method. By printing out &lt;code&gt;self&lt;/code&gt;, we can see that it is an instance of the class and it's own unique object:&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;Game&lt;/span&gt;

  &lt;span class="nx"&gt;attr_accessor&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;

  &lt;span class="nx"&gt;def&lt;/span&gt; &lt;span class="nx"&gt;initialize&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;

  &lt;span class="nx"&gt;def&lt;/span&gt; &lt;span class="nx"&gt;print_out_self&lt;/span&gt;
    &lt;span class="nx"&gt;puts&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;
&lt;span class="nx"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;elden_ring = Game.new('Elden Ring')
elden_ring.print_out_self
# =&amp;gt; #&amp;lt;Game:0x00007fd9910c45a0 @name='Elden Ring'&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Self in Class Methods
&lt;/h2&gt;

&lt;p&gt;We can use &lt;code&gt;self&lt;/code&gt; on class methods as well. From our last example, we can create a class method by using &lt;code&gt;self&lt;/code&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;Game&lt;/span&gt;

  &lt;span class="nx"&gt;attr_accessor&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="nd"&gt;all&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

  &lt;span class="nx"&gt;def&lt;/span&gt; &lt;span class="nx"&gt;initialize&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;
    &lt;span class="p"&gt;@@&lt;/span&gt;&lt;span class="nd"&gt;all&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;

  &lt;span class="nx"&gt;def&lt;/span&gt; &lt;span class="nx"&gt;developer_of_game&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;developer_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;developer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;developer_name&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;

  &lt;span class="nx"&gt;def&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;all&lt;/span&gt;
    &lt;span class="p"&gt;@@&lt;/span&gt;&lt;span class="nd"&gt;all&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;
&lt;span class="nx"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;elden_ring = Game.new('Elden Ring')
dead_space = Game.new('Dead Space')
Game.all
# =&amp;gt; [#&amp;lt;Game:0x00007fd9910c45a0 @name='Elden Ring'&amp;gt;, #&amp;lt;Game:0x00007fd9900dba58 @name='Dead Space'&amp;gt;]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; The &lt;code&gt;self&lt;/code&gt; inside the &lt;code&gt;#developer_of_game&lt;/code&gt; method is &lt;em&gt;&lt;strong&gt;NOT&lt;/strong&gt;&lt;/em&gt; the same as the &lt;code&gt;self&lt;/code&gt; in the &lt;code&gt;self.all&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;Game.all&lt;/code&gt; produces an array of all the &lt;code&gt;Game&lt;/code&gt;. The &lt;code&gt;@@all&lt;/code&gt; is a class variable that is initially set as an empty array &lt;code&gt;[]&lt;/code&gt;. In our initialize method, each individual instance's &lt;code&gt;self&lt;/code&gt; is inserted into the &lt;code&gt;@@all&lt;/code&gt; variable. Once the &lt;code&gt;self.all&lt;/code&gt; class method is called, that array will contain all of the instances of the &lt;code&gt;Game&lt;/code&gt; class that were created because the method is being called on the &lt;em&gt;entire&lt;/em&gt; class rather than one instance of the class. Finally, we have to call the &lt;code&gt;@@all&lt;/code&gt; class variable inside our &lt;code&gt;self.all&lt;/code&gt; to be able to read it. If we wanted to only get the names of each game, we can &lt;code&gt;.each&lt;/code&gt; the &lt;code&gt;self.all&lt;/code&gt; class method we just defined inside another class method:&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="nx"&gt;def&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;print_all_game_names&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;all&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nx"&gt;game&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="nx"&gt;puts&lt;/span&gt; &lt;span class="nx"&gt;game&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;
    &lt;span class="nx"&gt;end&lt;/span&gt;
&lt;span class="nx"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;The most important thing to remember when it comes to &lt;code&gt;self&lt;/code&gt;: When using &lt;code&gt;self&lt;/code&gt; &lt;em&gt;inside&lt;/em&gt; an instance method, it is one object of that class that the method is called upon. When using &lt;code&gt;self&lt;/code&gt; on a class method, it is the entire class itself. &lt;/p&gt;

</description>
      <category>ruby</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>React: JSX, Components, and Props</title>
      <dc:creator>rossonevan</dc:creator>
      <pubDate>Sun, 31 Jul 2022 20:25:00 +0000</pubDate>
      <link>https://forem.com/rossonevan/react-jsx-components-and-props-ndo</link>
      <guid>https://forem.com/rossonevan/react-jsx-components-and-props-ndo</guid>
      <description>&lt;p&gt;Switching to React from vanilla JavaScript, many programmers will start to understand why many companies today use React (Facebook, Airbnb, etc.). The process of coding in React makes an easier understanding of a programmer's code and better layout of that code, especially in the HTML of a webpage. One of the first things you'll learn are JSX, Components, and Props. &lt;/p&gt;

&lt;h2&gt;
  
  
  JSX
&lt;/h2&gt;

&lt;p&gt;JavaScript XML, also known as JSX, is a syntax extension that is read through Babel in React that allows programmers to write HTML inside their JavaScript code. Babel recognizes JSX and knows that it is not HTML. Let's say we have an App component that contains a header and paragraph element:&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;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;World&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Hope&lt;/span&gt; &lt;span class="nx"&gt;you&lt;/span&gt; &lt;span class="nx"&gt;have&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;good&lt;/span&gt; &lt;span class="nx"&gt;day&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&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;Whenever the webpage loads, JSX knows exactly what we want to show on our webpage. This process allows our code to become declarative instead of imperative. Instead of writing out exactly &lt;em&gt;how&lt;/em&gt; our code is done in detail (imperative),  we can say &lt;em&gt;what&lt;/em&gt; we would like to do (declarative). So instead of writing &lt;code&gt;document.createElement()&lt;/code&gt; and &lt;code&gt;document.quereySelector()&lt;/code&gt; like in vanilla JS, we can simply write our HTML elements inside our JS files!&lt;/p&gt;

&lt;h2&gt;
  
  
  Components
&lt;/h2&gt;

&lt;p&gt;As shown in the example above, components are one of the most essential parts of React that is different from vanilla JavaScript. Components help show where certain parts our code goes. By using components, it helps create a cleaner and easy to follow layout of our code, as well as the layout of our webpage. A component is a function that returns JSX. This function tells React what the properties of the component should look like once applied to the DOM. A component's function &lt;strong&gt;must always&lt;/strong&gt; start with a capital letter. It also can only return &lt;strong&gt;one&lt;/strong&gt; HTML element. For example:&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;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;World&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Goodbye&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&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;Since there are two &lt;code&gt;div&lt;/code&gt; elements being returned, React will throw back an error saying that it can only return one element. An easy fix to this kind of situation is putting &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;/&amp;gt;&lt;/code&gt; outside of the two &lt;code&gt;div&lt;/code&gt; elements so that, in React's eyes, the component returns only one element.&lt;/p&gt;

&lt;h2&gt;
  
  
  Props
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--V2fI8HR7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vz6w0gh7qx0bw6d9epj9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--V2fI8HR7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vz6w0gh7qx0bw6d9epj9.png" alt="Opra Meme" width="625" height="468"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The arguments that components take in are known as props. In most applications, there is a &lt;em&gt;parent&lt;/em&gt; component consisting of multiple &lt;em&gt;child&lt;/em&gt; components. A parent component is the &lt;strong&gt;top-level&lt;/strong&gt; component that contains pathways to other components(child components).&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;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;AnimalDetails&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Comments&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&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;From the example above, &lt;code&gt;App&lt;/code&gt; is the parent component of the child components &lt;code&gt;AnimalDetails&lt;/code&gt; and &lt;code&gt;Comments&lt;/code&gt;. If we wanted to pass information into our child components from the parent component, we can assign props to each of them:&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;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;AnimalDetails&lt;/span&gt; 
        &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Max&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 
        &lt;span class="nx"&gt;species&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 
        &lt;span class="nx"&gt;personality&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Loves to go on walks!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Comments&lt;/span&gt; &lt;span class="nx"&gt;commentsText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Max is a really good boy!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&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;Now that our child components have props with information we want to pass down, we can assign these props to different elements to be displayed on the webpage. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; Props cannot be passed between child components, only from a parent component.&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;function&lt;/span&gt; &lt;span class="nx"&gt;AnimalDetails&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h3&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;species&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h3&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;personality&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&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;Whenever we want to call our props into our JSX, we must contain them inside &lt;code&gt;{}&lt;/code&gt; then use &lt;code&gt;props.&lt;/code&gt; before the key name to access them. &lt;/p&gt;

&lt;h2&gt;
  
  
  De-structuring Props
&lt;/h2&gt;

&lt;p&gt;As you can see from our previous example, if there are multiple props being passed down, our code starts to look very cluttered with &lt;code&gt;prop.&lt;/code&gt;. An easy solution to make our code cleaner and easier to read, we can use destructuring. From our last example, instead of passing &lt;code&gt;props&lt;/code&gt; as an argument of our &lt;code&gt;AnimalDetails&lt;/code&gt; component, we can pass down the key names we assigned our information to:&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;function&lt;/span&gt; &lt;span class="nx"&gt;AnimalDetails&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;species&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;personality&lt;/span&gt;&lt;span class="p"&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h3&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;species&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h3&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;personality&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&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;Whenever destructuring props, the key names being passed down must be contained inside &lt;code&gt;{}&lt;/code&gt;. Now, we can just pass the keys and have clean and concise code that is easy to read!&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>JavaScript Submit Event Listeners for Dummies.</title>
      <dc:creator>rossonevan</dc:creator>
      <pubDate>Mon, 11 Jul 2022 17:43:00 +0000</pubDate>
      <link>https://forem.com/rossonevan/javascript-submit-event-listeners-for-dummies-5f99</link>
      <guid>https://forem.com/rossonevan/javascript-submit-event-listeners-for-dummies-5f99</guid>
      <description>&lt;p&gt;JavaScript has the ability to &lt;strong&gt;listen&lt;/strong&gt; for an event in the browser that will let it know, for example, whether a user clicked on a button with their mouse or pressed a certain key on their keyboard. These "events" are known as event listeners. Here are a list of common event listeners: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Events#standard_events"&gt;https://developer.mozilla.org/en-US/docs/Web/Events#standard_events&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Submit Event
&lt;/h2&gt;

&lt;p&gt;The submit event listener is one of the main event listeners developers will use in their work. This type of event listener will &lt;strong&gt;listen&lt;/strong&gt; for when the user clicks the submit button on the webpage. For example:&lt;/p&gt;

&lt;p&gt;HTML Code :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;body&amp;gt;
    &amp;lt;div&amp;gt;
        &amp;lt;form id="new-name"&amp;gt;
            &amp;lt;label for="firstname"&amp;gt;First Name: &amp;lt;/label&amp;gt;
            &amp;lt;input type="text" name="firstname" id="new-firstname" /&amp;gt;
            &amp;lt;label for="lastname"&amp;gt;Last Name: &amp;lt;/label&amp;gt;
            &amp;lt;input type="text" name="lastname" id="new-lastname" /&amp;gt;
            &amp;lt;input type="submit" value="Create Name" /&amp;gt;
        &amp;lt;/form&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;br&amp;gt;
    &amp;lt;p&amp;gt;Full Name Here:&amp;lt;/p&amp;gt;
    &amp;lt;p id="name-result"&amp;gt;&amp;lt;/p&amp;gt;
  &amp;lt;/body&amp;gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's say we have this form that is asking the user to input their first and last name. In JavaScript, we first want to grab the id/class from the html element we want to add the event listener to. In order to do that, we need to create a &lt;code&gt;const&lt;/code&gt; that will be the html element we want to grab. In this case, we want to grab the form's id &lt;code&gt;new-name&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const form = document.querySelector('#new-name')&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;After we grab the &lt;code&gt;new-name&lt;/code&gt; id and set it equal to a variable, we can then add our submit event listener to the form:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;form.addEventListener('submit', () =&amp;gt; {})&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Our next step is preventing the default feature of the submit event listener, which would refresh the webpage every time the submit button is clicked. In order to prevent this from occurring we write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;form.addEventListener('submit', (event) =&amp;gt; {
event.preventDefault();
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that our page is not refreshing every time we click our submit button, we can go ahead and get the values of the inputs we get when the user inputs their name. Just like how we grabbed the form id, we need to grab each inputs id and set them into new variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const newFirstName = document.querySelector('#new-firstname').value;
const newLastName = document.querySelector('#new-lastname').value;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are first grabbing the ids of the html element that the user inputs lie in. &lt;em&gt;Then&lt;/em&gt; we need to get the &lt;code&gt;.value&lt;/code&gt; of the input in order to take the information the user types into the text box to store it for later use. We can then check to make sure we grabbed the correct values by console logging our new values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(newFirstName);
console.log(newLastName);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The text that the user inputs should now appear in the console when clicking the &lt;code&gt;Create Name&lt;/code&gt; button. Perfect! Now, we can insert these input values into a &lt;code&gt;p&lt;/code&gt; element with the id of &lt;code&gt;name-result&lt;/code&gt; for the user to see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const result = document.querySelector('#name-result');
result.innerText = `${newFirstName} ${newLastName}`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use &lt;code&gt;.innerText&lt;/code&gt; to change the text inside the &lt;code&gt;p&lt;/code&gt; element. We then use string interpolation to input the input values the user submits. The name the user inputs should now be displayed on the webpage! Congratulations, you just made a submit event listener!!&lt;/p&gt;

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