<?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: YarixaR</title>
    <description>The latest articles on DEV Community by YarixaR (@yarixar).</description>
    <link>https://dev.to/yarixar</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%2F890023%2F17de2545-ff54-4cb4-9a0e-e021acb711dc.png</url>
      <title>DEV Community: YarixaR</title>
      <link>https://dev.to/yarixar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yarixar"/>
    <language>en</language>
    <item>
      <title>Before Bootcamp</title>
      <dc:creator>YarixaR</dc:creator>
      <pubDate>Sun, 02 Oct 2022 22:39:43 +0000</pubDate>
      <link>https://dev.to/yarixar/before-bootcamp-4dp9</link>
      <guid>https://dev.to/yarixar/before-bootcamp-4dp9</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1NhZOa4a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pozi8kl9xixtkongj8et.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1NhZOa4a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pozi8kl9xixtkongj8et.jpeg" alt="dentals" width="828" height="1108"&gt;&lt;/a&gt;&lt;br&gt;
 Before joining Flatiron, I worked in the field of veterinary medicine.  Being a vet technician was a career that started out as a passion, where I could focus my love and affection towards animals of all shapes and sizes in their rehabilitation and medical treatments. However, throughout six years of the industry, I had come to a point of dragging myself to work with a lack of motivation. Working ten hour days, six days a week, I realized I had lost my spark. Ultimately, I had to accept I was burnt out from always being understaffed, lack of recognition, low pay, and overall physical fatigue.  &lt;/p&gt;

&lt;p&gt;Since the pandemic years of 2020, there was always a seedling of thought for a career change. I thought of going into nursing, possibly starting a cooking channel on social media, or breaking into tech. I spent those late nights in bed looking up "How to switch careers" and "How to get into the tech industry". I didn't take it seriously. These initial thoughts had previously always been shot down and discouraged by those around me. I was always being told that the risk was too great for my age, and the grass was not always greener on the other side. I started to genuinely believe a person like me wouldn't be able to learn something new, completely out of my specialty. &lt;/p&gt;

&lt;p&gt;Despite my hesitations, my curiosity and interest towards software engineering continued to grow. After focusing my research on S.E., my stars began to align. I was meeting people who were software engineers, people who graduated from a Bootcamp and are now doing what they enjoy and living a healthy lifestyle. I knew I deserved that for myself too. I knew I was capable of doing the same thing as long as I set my heart to it. I decided I was not going to let another person tell me if I can or can't succeed in the tech world. I decided to enroll in Flatiron. &lt;/p&gt;

&lt;p&gt;Now I haven't proved the negative voices wrong yet, but what I can prove is that I have a new interest and drive. These past four months, were difficult beyond belief. Highlighted by early mornings and late nights, I constantly had to fight through bugs, error codes, and an ever-imposing sense of doubt. However, despite all of this, I'm now on the verge of completing the Flatiron course for software engineering. Along the way, I've met good people, created healthy habits, and most importantly, I've gained coding skills that will last a lifetime. This skill and experience that I've acquired, I will take and manifest it. It's up to me to continue to grow after Bootcamp. The next step won't be easy but I'm excited for what's to come. &lt;/p&gt;

</description>
      <category>beginners</category>
    </item>
    <item>
      <title>Serializers in Rails</title>
      <dc:creator>YarixaR</dc:creator>
      <pubDate>Sun, 11 Sep 2022 21:09:12 +0000</pubDate>
      <link>https://dev.to/yarixar/serializers-in-rails-1j2g</link>
      <guid>https://dev.to/yarixar/serializers-in-rails-1j2g</guid>
      <description>&lt;p&gt;I have finally reached the end of my boot camp, finishing my last code challenge with a high score. I'm feeling pretty good! Now I'm ready to talk about Serializers.&lt;/p&gt;

&lt;p&gt;In the controller, we can customize JSON data to a specific model using &lt;code&gt;include:&lt;/code&gt; or &lt;code&gt;.to_json(except: [:created_at, :updated_at])&lt;/code&gt;. The include option will let us nest associated data in our response. Ideally, we don't want to give this responsibility to the Controller because it'll just make it "chunky" as they say. This is why we use Serializers. &lt;/p&gt;

&lt;h4&gt;
  
  
  Installing Serializers
&lt;/h4&gt;

&lt;p&gt;To use serializers, you'll need to first check to see if this gem is already included in the Gemfile. If not, add &lt;code&gt;gem 'active_model_serializers'&lt;/code&gt; to the Gemfile. Then, bundle install. &lt;/p&gt;

&lt;h4&gt;
  
  
  Generating Serializer
&lt;/h4&gt;

&lt;p&gt;In the console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;rails&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="n"&gt;serializer&lt;/span&gt; &lt;span class="n"&gt;planet&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;The name of the serializer needs the same name as the model and written in lowercase.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After, this will generate an empty file that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PlanetSerializer&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveModel&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Serializer&lt;/span&gt;
  &lt;span class="n"&gt;attributes&lt;/span&gt; &lt;span class="ss"&gt;:id&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, it only has the attribute &lt;code&gt;:id&lt;/code&gt;. This will limit all of &lt;code&gt;Planet&lt;/code&gt;'s data to show only the &lt;code&gt;:id&lt;/code&gt; in JSON format. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qkIDPHhy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t9lun8y6vexqp5ta4sxe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qkIDPHhy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t9lun8y6vexqp5ta4sxe.png" alt="Empty JSON data" width="880" height="547"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So let's go ahead and add &lt;code&gt;Planet&lt;/code&gt;'s real attributes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PlanetSerializer&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveModel&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Serializer&lt;/span&gt;
  &lt;span class="n"&gt;attributes&lt;/span&gt; &lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:distance_from_earth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:nearest_star&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:image&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bFxl_rIp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sa8sewk5m3d7h11uxvq1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bFxl_rIp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sa8sewk5m3d7h11uxvq1.png" alt="Planet data" width="880" height="730"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This way we don't have to add &lt;code&gt;.to_json(except: [:created_at, :updated_at])&lt;/code&gt; in our Controller and it can be exclusive to just communicating with models and views. &lt;/p&gt;

&lt;h4&gt;
  
  
  Serializing Relationships
&lt;/h4&gt;

&lt;p&gt;Much like adding &lt;code&gt;include:&lt;/code&gt; in the controller, a serializer will let us nest associated data in our response. We already have associations in our models so we can put these relationships in the serializer too.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MissionSerializer&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveModel&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Serializer&lt;/span&gt;
  &lt;span class="n"&gt;attributes&lt;/span&gt; &lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;

  &lt;span class="n"&gt;has_one&lt;/span&gt; &lt;span class="ss"&gt;:planet&lt;/span&gt;
&lt;span class="k"&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 ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PlanetSerializer&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveModel&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Serializer&lt;/span&gt;
  &lt;span class="n"&gt;attributes&lt;/span&gt; &lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:distance_from_earth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:nearest_star&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:image&lt;/span&gt;

  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:missions&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QxBX0Ij3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bxob61vzxjal3isor47q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QxBX0Ij3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bxob61vzxjal3isor47q.png" alt="Nested missions" width="880" height="542"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;“Home.” Flatiron School, 9 May 2022, &lt;a href="https://flatironschool.com/"&gt;https://flatironschool.com/&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>rails</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Active Record Associations (one-to-many)</title>
      <dc:creator>YarixaR</dc:creator>
      <pubDate>Sun, 21 Aug 2022 00:17:00 +0000</pubDate>
      <link>https://dev.to/yarixar/active-record-associations-3jk5</link>
      <guid>https://dev.to/yarixar/active-record-associations-3jk5</guid>
      <description>&lt;p&gt;It's insane to think that I've already finished my 3rd phase of Bootcamp. Time is flying! In this phase, we finally ventured to the back end of software engineering where we learned a bit about ruby on rails and what happens "under the hood".&lt;br&gt;
For this blog, I've decided to briefly go over associations. &lt;/p&gt;
&lt;h2&gt;
  
  
  Why Associations?
&lt;/h2&gt;

&lt;p&gt;Declaring associations is a convenient way of connecting two Active Record models. This makes manipulating your data models in logical way. &lt;/p&gt;

&lt;p&gt;If you have 2 separate models, you can bring them together by using a foreign key in the migration folder. &lt;br&gt;
Here, we're keeping the user's foreign key stored in the candle's table. Since a candle belongs to a user, it should have a "user_id" to identify which user it belongs to.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;version: &lt;/span&gt;&lt;span class="mi"&gt;2022_08_15_211950&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;

  &lt;span class="n"&gt;create_table&lt;/span&gt; &lt;span class="s2"&gt;"users"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;force: :cascade&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt; &lt;span class="s2"&gt;"name"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="n"&gt;create_table&lt;/span&gt; &lt;span class="s2"&gt;"candles"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;force: :cascade&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt; &lt;span class="s2"&gt;"scent"&lt;/span&gt;
    &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt; &lt;span class="s2"&gt;"brand"&lt;/span&gt;
    &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;integer&lt;/span&gt; &lt;span class="s2"&gt;"user_id"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But in order for this to work, inside of active records, you need a special macro that ties them together and develops a relationship. This is called a one-to-many. &lt;/p&gt;

&lt;h3&gt;
  
  
  What is a macro?
&lt;/h3&gt;

&lt;p&gt;A macro is a method that writes code for us. If you're familiar with macros like &lt;strong&gt;attr_reader&lt;/strong&gt; and &lt;strong&gt;attr_accessor&lt;/strong&gt;, Active Record comes with a few handy macros that create new instance methods we can use with our classes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the users model, we should think that a user &lt;code&gt;has_many&lt;/code&gt; candles. The name "candles" has to be pluralized because a single user can have &lt;strong&gt;many&lt;/strong&gt; candles.&lt;/li&gt;
&lt;li&gt;In the candles model, candles &lt;code&gt;belong_to&lt;/code&gt; a user. You'll notice that "user" is written as singular because candles on belong to &lt;strong&gt;one&lt;/strong&gt; user.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# has_many&lt;/span&gt;

&lt;span class="c1"&gt;# app/models/user.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:candles&lt;/span&gt;

  &lt;span class="c1"&gt;# naming is plural&lt;/span&gt;
  &lt;span class="c1"&gt;# indicates a one-to-many association&lt;/span&gt;
  &lt;span class="c1"&gt;# a user can have many candles in this example&lt;/span&gt;
  &lt;span class="c1"&gt;# candles associated via `user_id`&lt;/span&gt;
&lt;span class="k"&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 ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# belongs_to&lt;/span&gt;

&lt;span class="c1"&gt;# app/models/candle.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Candle&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:user&lt;/span&gt;

  &lt;span class="c1"&gt;# must be singular&lt;/span&gt;
  &lt;span class="c1"&gt;# Sets up a 1:1 connection with another model - User in this case&lt;/span&gt;
  &lt;span class="c1"&gt;# Books associated to a user via `user_id` on candles table&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only after you ran &lt;code&gt;rake db:migrate&lt;/code&gt; and planted your seeds, you can go ahead and run &lt;code&gt;rake console&lt;/code&gt; to check if they're all properly connected. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8A8iVLmL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b4dun09c8bi3pxmwggh5.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8A8iVLmL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b4dun09c8bi3pxmwggh5.jpeg" alt="Drake meme" width="880" height="496"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;“Active Record Associations.” Ruby on Rails Guides, &lt;a href="https://guides.rubyonrails.org/association_basics.html#the-types-of-associations"&gt;https://guides.rubyonrails.org/association_basics.html#the-types-of-associations&lt;/a&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;“Home.” Flatiron School, 9 May 2022, &lt;a href="https://flatironschool.com/"&gt;https://flatironschool.com/&lt;/a&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ruby</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Introductory to useState</title>
      <dc:creator>YarixaR</dc:creator>
      <pubDate>Sun, 31 Jul 2022 03:07:00 +0000</pubDate>
      <link>https://dev.to/yarixar/introductory-to-usestate-172</link>
      <guid>https://dev.to/yarixar/introductory-to-usestate-172</guid>
      <description>&lt;p&gt;Finishing up my second phase of a boot camp, I thought I'd write a short post on React hooks. Specifically, &lt;code&gt;useState&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The hook, &lt;code&gt;useState&lt;/code&gt; is a function that React provides for us and it allows us to give our component some state. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is state?&lt;/strong&gt;&lt;br&gt;
State is data that changes over time as the user interacts with your app. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How do we update state?&lt;/strong&gt;&lt;br&gt;
By using React's &lt;code&gt;useState&lt;/code&gt; function of course!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useState&lt;/code&gt; will return an array that has two variables inside of it. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;State&lt;/code&gt;. Name the variable as a reference to the value of the state.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setState&lt;/code&gt;. This is a function returned by useState that re-renders the component caused by changes to the state.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setState&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;initial State&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;useState&lt;/code&gt; has a parameter that determines the initial value of that state. It takes a boolean, string, and number. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Warning! Learn from me...&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Call hooks at the most top level of a React function component, before a return. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Call hooks from custom hooks like &lt;code&gt;useEffect&lt;/code&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Let's make a count button. &lt;/p&gt;

&lt;p&gt;First, we need to import &lt;code&gt;useState&lt;/code&gt; from React to use our function.&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&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;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&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;button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;show&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&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;During the initial render, the returned state (count) is the same as the value passed as the first argument (0). The setCount function is used to update the state. It accepts new state value and re-renders the component.  &lt;/p&gt;

&lt;p&gt;We will need to add an event listener and another function to help us update the value of &lt;code&gt;count&lt;/code&gt; whenever the button is clicked.&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&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;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;increaseCount&lt;/span&gt; &lt;span class="o"&gt;=&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="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
           &lt;span class="cm"&gt;/* 0 + 1 */&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;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;increaseCount&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
             &lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;show&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;count&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;/button&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;Above, you see that &lt;code&gt;onClick&lt;/code&gt; function that will tell the button what to do when clicked. &lt;/p&gt;

&lt;p&gt;When clicked, it runs the function (increaseCount). &lt;/p&gt;

&lt;p&gt;Running the function, calls &lt;code&gt;setCount&lt;/code&gt; and tells React that the state, &lt;code&gt;count&lt;/code&gt;'s value has to update to 1. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sources&lt;/strong&gt;:&lt;br&gt;
“Home.” Flatiron School, 9 May 2022, &lt;a href="https://flatironschool.com/"&gt;https://flatironschool.com/&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;“React Interactivity: Events and State - Learn Web Development: MDN.” Learn Web Development | MDN, &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Client-side_JavaScript_frameworks/React_interactivity_events_state"&gt;https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Client-side_JavaScript_frameworks/React_interactivity_events_state&lt;/a&gt;. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>react</category>
    </item>
    <item>
      <title>parseInt() For Beginners</title>
      <dc:creator>YarixaR</dc:creator>
      <pubDate>Thu, 14 Jul 2022 02:36:50 +0000</pubDate>
      <link>https://dev.to/yarixar/parseint-for-beginners-4p79</link>
      <guid>https://dev.to/yarixar/parseint-for-beginners-4p79</guid>
      <description>&lt;p&gt;This is for anyone that's newly immersing themselves in the world of coding. If you are a beginner-level coder like me, you've looked into &lt;code&gt;parseInt()&lt;/code&gt; and became completely lost when it came to the "radix" part. Here is my simplest explanation of what the function &lt;code&gt;parseInt&lt;/code&gt; does. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is &lt;code&gt;parseInt&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;parseInt&lt;/code&gt; is a function that can take 2 arguments and it can either return an integer or a NaN (not a number). &lt;/p&gt;

&lt;p&gt;This is the syntax:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;parseInt(string)&lt;/code&gt;&lt;br&gt;
or &lt;br&gt;
&lt;code&gt;parseInt(string, radix)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Assuming you know all the different data types, for example: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;numbers&lt;/li&gt;
&lt;li&gt;strings&lt;/li&gt;
&lt;li&gt;booleans&lt;/li&gt;
&lt;li&gt;symbols&lt;/li&gt;
&lt;li&gt;objects&lt;/li&gt;
&lt;li&gt;null&lt;/li&gt;
&lt;li&gt;undefined&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then you already know that JavaScript will perform addition to a &lt;em&gt;number&lt;/em&gt; data type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 + 2 + 3;
//6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adding a &lt;em&gt;string&lt;/em&gt; to a &lt;em&gt;number&lt;/em&gt;, Javascript will pair them together and will return a &lt;em&gt;string&lt;/em&gt;. It cannot perform addition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'1' + 2 + 3
// '123'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now consider this example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MKPgpz5G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fugpmwzxavu3n2vsa0m8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MKPgpz5G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fugpmwzxavu3n2vsa0m8.png" alt="before parseInt" width="284" height="103"&gt;&lt;/a&gt;&lt;br&gt;
What's happening above is JavaScript does not see '5' as a number but as a &lt;em&gt;string&lt;/em&gt;. We're asking the &lt;em&gt;string&lt;/em&gt; to add 1 but instead of creating a new value, it will add the 1 next to the &lt;em&gt;string&lt;/em&gt;. If your goal is to convert a &lt;em&gt;string&lt;/em&gt; into a &lt;em&gt;number&lt;/em&gt;, that's when you'd use &lt;code&gt;parseInt&lt;/code&gt;.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wIjK-CqF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bxfassoenvtte0ltd16m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wIjK-CqF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bxfassoenvtte0ltd16m.png" alt="parseInt function" width="284" height="103"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;parseInt&lt;/code&gt; will remove the quotes from the string before performing the addition or any form of operation. Thus resulting in a whole integer. &lt;br&gt;
If ever you need a floating integer (a number with a decimal) to convert into a whole number, &lt;code&gt;parseInt&lt;/code&gt; can arrange that for you as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const num = 10.03
console.log(parseInt(num));

//  10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;parseInt(string, radix)&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Traditionally, &lt;code&gt;parseInt&lt;/code&gt; should take 2 arguments. The function parses (analyzes) a string and returns an integer of the specified radix. &lt;/p&gt;

&lt;h6&gt;
  
  
  What is a radix?
&lt;/h6&gt;

&lt;p&gt;A radix is the base in mathematical numeral systems. When using &lt;code&gt;parseInt&lt;/code&gt;, you will want to work with the base 10  because it uses the ten digits from 0 through 9. This is called the decimal system.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;parseInt("4", 10)
// 4

parseInt("4.444", 10)
// 4

parseInt("Jerry is 9", 10)
// NaN

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last thing you need to know is &lt;strong&gt;NaN&lt;/strong&gt;. This function can only read the first value starting the string. If it's not a number, it will return as &lt;strong&gt;NaN&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Source&lt;/em&gt;:&lt;br&gt;
“Radix.” Wikipedia, Wikimedia Foundation, 23 June 2022, &lt;a href="https://en.wikipedia.org/wiki/Radix"&gt;https://en.wikipedia.org/wiki/Radix&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;“Parseint() - Javascript: MDN.” JavaScript | MDN, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt&lt;/a&gt;. &lt;/p&gt;

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