<?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: EmilyFernschild</title>
    <description>The latest articles on DEV Community by EmilyFernschild (@emilyfernschild).</description>
    <link>https://dev.to/emilyfernschild</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%2F926957%2F411f82a1-c789-4bce-b147-f80632333bac.png</url>
      <title>DEV Community: EmilyFernschild</title>
      <link>https://dev.to/emilyfernschild</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/emilyfernschild"/>
    <language>en</language>
    <item>
      <title>Basics of Rails Password Protection</title>
      <dc:creator>EmilyFernschild</dc:creator>
      <pubDate>Fri, 18 Nov 2022 21:29:52 +0000</pubDate>
      <link>https://dev.to/emilyfernschild/basics-of-rails-password-protection-2fd1</link>
      <guid>https://dev.to/emilyfernschild/basics-of-rails-password-protection-2fd1</guid>
      <description>&lt;p&gt;In this blog, we will be using bcrypt for authentication in our rails backend. Password protection can be an extensive topic but I am going to keep this to just the 'need to know stuff' for now. Hopefully this will help you be able to implement authentication in your own project!&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia1.tenor.com%2Fimages%2Fe173ca79d571e30c562cc02db9fe1270%2Ftenor.gif%3Fitemid%3D4402312" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia1.tenor.com%2Fimages%2Fe173ca79d571e30c562cc02db9fe1270%2Ftenor.gif%3Fitemid%3D4402312" width="364" height="186"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  What is Authentication?
&lt;/h2&gt;

&lt;p&gt;Authentication in simple terms is a way of verifying who you are. I often mix up authentication and authorization: &lt;/p&gt;
&lt;blockquote&gt; Authentication is a confirmation of user identity, while authorization determines whether you can access a particular resource [1].&lt;/blockquote&gt; Hopefully this quick definition of both will help you not mix them up like me! Having your authentication work properly is important for authorization later, they rely on each other. 
&lt;h2&gt;
  
  
  Basics of bcrypt
&lt;/h2&gt;

&lt;p&gt;Important concepts to know about how bycrpt works is that it uses hashing and salt algorithms to encrypt passwords for protection. The hashing algorithm turns plain text passwords into a jumble of letters and numbers. A key aspect of this is that they can't be turned back into plain text after they have been hashed. Salting is an added layer on top of hashing that creates a more unique jumble of letters and numbers for every password. The combination of hashing and salting each password ensures that each encrypted password is very secure. &lt;/p&gt;
&lt;h2&gt;
  
  
  Three Main Steps
&lt;/h2&gt;

&lt;p&gt;1) The first step is to add the &lt;a href="https://github.com/bcrypt-ruby/bcrypt-ruby" rel="noopener noreferrer"&gt;bcrypt&lt;/a&gt; gem to your Gemfile.&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;gem&lt;/span&gt; &lt;span class="nx"&gt;install&lt;/span&gt; &lt;span class="nx"&gt;bcrypt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) In your Users table, the key word to have as your password column is &lt;em&gt;'password_digest'&lt;/em&gt;. ⭐ Note: only use &lt;em&gt;'password_digest'&lt;/em&gt; here and use just password everywhere else (including in your frontend!).&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;//schema.rb&lt;/span&gt;
 &lt;span class="nx"&gt;create_table&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;users&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;force&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;cascade&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;t&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;first_name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;last_name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;username&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;password_digest&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;datetime&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;created_at&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;precision&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
    &lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;datetime&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;updated_at&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;precision&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
 &lt;span class="nx"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3) In your model, in our case our User model, include the macro &lt;em&gt;'has_secure_password'&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="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="nx"&gt;has_secure_password&lt;/span&gt;
  &lt;span class="c1"&gt;//the rest of your model &lt;/span&gt;
&lt;span class="nx"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Example of use in a Login
&lt;/h2&gt;

&lt;p&gt;after you do these step you can write something like this for a login feature on your application:&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="nc"&gt;SessionsController&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;skip_before_action&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;authorize&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;only&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;
    &lt;span class="c1"&gt;// for login feat&lt;/span&gt;
    &lt;span class="nx"&gt;def&lt;/span&gt; &lt;span class="nx"&gt;create&lt;/span&gt;
      &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_by&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;username&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;username&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;user&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;authenticate&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;password&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
        &lt;span class="nx"&gt;session&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="nx"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&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;user&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;errors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Invalid username or password&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&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;unauthorized&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;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;These are the three steps to get your passwords protected. I hoped this helped in clearing up any confusion you may have had about encrypting passwords! If you missed it, here is the link to &lt;a href="https://github.com/bcrypt-ruby/bcrypt-ruby" rel="noopener noreferrer"&gt;bcrypt&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;Resources: &lt;br&gt;
[1] D. Mamla, “Authentication and authorization in rails tutorial,” Nopio, 10-May-2021. [Online]. &lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>Sinatra with Active Record: GET requests using "include:"</title>
      <dc:creator>EmilyFernschild</dc:creator>
      <pubDate>Thu, 27 Oct 2022 16:27:18 +0000</pubDate>
      <link>https://dev.to/emilyfernschild/sinatra-with-active-record-get-requests-using-include-39jn</link>
      <guid>https://dev.to/emilyfernschild/sinatra-with-active-record-get-requests-using-include-39jn</guid>
      <description>&lt;p&gt;&lt;em&gt;Happy Spooky Season!&lt;/em&gt; In this blog, I will be discussing a Ruby Sinatra backend with a React frontend. In this phase of my bootcamp, we started learning about the backend and things are finally starting to all come together. So I will be going over what GET requests look like from both backend and frontend along with how helpful &lt;code&gt;include&lt;/code&gt; can be! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h7aRjrRH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://64.media.tumblr.com/8ab8b0df76dcc7327c9ceb3cfccb65e7/1de5e488cfed2df2-ea/s540x810/c535ff8a539c8374091db244666166d9de576e83.gifv" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h7aRjrRH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://64.media.tumblr.com/8ab8b0df76dcc7327c9ceb3cfccb65e7/1de5e488cfed2df2-ea/s540x810/c535ff8a539c8374091db244666166d9de576e83.gifv" width="540" height="332"&gt;&lt;/a&gt;&lt;/p&gt;
 Hades gives spooky vibes right? 



&lt;h2&gt;
  
  
  Frontend GET request
&lt;/h2&gt;

&lt;p&gt;First, lets quickly review what a GET request is. &lt;/p&gt;
&lt;blockquote&gt; GET is an HTTP request method that is used to obtain resources from servers [1].&lt;/blockquote&gt; A GET request simply put "gets" data from your server, which in our case is our Sinatra backend. Let's take a look at what our request would look like from our front end:&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ./App.js&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;demons&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setDemons&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="nx"&gt;useEffect&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;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://localhost:9292/demons&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;r&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;r&lt;/span&gt;&lt;span class="p"&gt;.&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="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;data&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;setDemons&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;Here we are using our localhost url for our fetch so that we can &lt;em&gt;get&lt;/em&gt; our demons data from our backend. This is important because this is how we connect our frontend to our backend. &lt;/p&gt;
&lt;h2&gt;
  
  
  Backend GET
&lt;/h2&gt;

&lt;p&gt;To get your GET request working you need both you frontend fetch request and your backend request so that they can work together to pass the data from one to another. So our backend GET request would look 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;ApplicationController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Sinatra&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;set&lt;/span&gt; &lt;span class="ss"&gt;:default_content_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'application/json'&lt;/span&gt;

  &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s2"&gt;"/demons"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="n"&gt;demons&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Demon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;
      &lt;span class="n"&gt;demons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_json&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;This is telling us that when using the route "/demons" get all the demon data in our database and return it to json format. If you go to your localhost route, you can see the data that the request is retrieving. Here is a sample of what our demon data will look like: &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QxLD9hYn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k288y7p35nywsmtezeyz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QxLD9hYn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k288y7p35nywsmtezeyz.png" alt="without include" width="880" height="98"&gt;&lt;/a&gt;Perfect! Now you might be asking yourself, do we get the same data from our frontend? Lets find out, we can use &lt;code&gt;console.log()&lt;/code&gt; to view the data being sent to our frontend in our fetch request. The data in our console will look something like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--g1CFJFIJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bm97nrk2x9gzggr5eyec.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g1CFJFIJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bm97nrk2x9gzggr5eyec.png" alt="console result" width="381" height="200"&gt;&lt;/a&gt;&lt;br&gt;
Now we have successfully connected our frontend and backend. Congratulations you just learned how to use GET requests to retrieve data from our backend and use it in our frontend! &lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;code&gt;include:&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;All of our demons guard souls in the underworld, meaning we have a &lt;em&gt;one to many&lt;/em&gt; relationship where a demon &lt;em&gt;has many&lt;/em&gt; souls and a soul &lt;em&gt;belongs to&lt;/em&gt; a demon. So what if you wanted to not only fetch the demons but also its associated souls? we could do something 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="n"&gt;get&lt;/span&gt; &lt;span class="s2"&gt;"/demons"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="n"&gt;demons&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Demon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;
      &lt;span class="n"&gt;demons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;include: :souls&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;include:&lt;/code&gt; allows use to have access to the souls that are related to the demons. So now with this small change our database will look something like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EP4Kms_t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2gd9u9of07205q5qqt5b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EP4Kms_t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2gd9u9of07205q5qqt5b.png" alt="with include" width="872" height="331"&gt;&lt;/a&gt; This now gives us every soul associated with that particular demon with the use of a foreign key (in this case that would be the demon_id). The foreign key is what allows us to connect the demons to the souls and therefor allows us to &lt;em&gt;include&lt;/em&gt; this relationship in our database. &lt;br&gt;
Now we can have access to this souls data from our frontend! An idea for what we could do in the frontend now is something like listing the souls for each demon to display them on our website! 👻&lt;/p&gt;

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

&lt;p&gt;I hoped this blog helped you understand GET requests and all the ways you can connect your backend data to your frontend! I also hope that you can utilize &lt;code&gt;include:&lt;/code&gt; in your future projects. Happy coding!&lt;/p&gt;

&lt;p&gt;Resources:&lt;br&gt;
[1]  D. Landup, “Get HTTP request in react,” Stack Abuse, 28-Sep-2022. [Online]. Available: &lt;a href="https://stackabuse.com/get-http-request-in-react/"&gt;https://stackabuse.com/get-http-request-in-react/&lt;/a&gt;. [Accessed: 27-Oct-2022]. &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>ruby</category>
      <category>react</category>
      <category>programming</category>
    </item>
    <item>
      <title>Basics of useState hook: How to use it and Where to put it?</title>
      <dc:creator>EmilyFernschild</dc:creator>
      <pubDate>Thu, 06 Oct 2022 22:35:34 +0000</pubDate>
      <link>https://dev.to/emilyfernschild/basics-of-usestate-hook-how-to-use-it-and-where-to-put-it-3gj9</link>
      <guid>https://dev.to/emilyfernschild/basics-of-usestate-hook-how-to-use-it-and-where-to-put-it-3gj9</guid>
      <description>&lt;p&gt;As I went into learning React in phase 2 of my coding bootcamp, after just learning JavaScript in phase 1, I realized how much easier it could make my life. I was so excited to learn React but it came with its ups and downs. For me, the hardest concept to grasp was props and state. I had the hardest time figuring out where to put state! In the beginning I found that I would have to lift state very often. That would result in me getting confused within my code, not knowing which way was up or which way was down. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m1eikDCH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media1.tenor.com/images/f2acb3628808848e4c85576caf1653ae/tenor.gif%3Fitemid%3D11802292" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m1eikDCH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media1.tenor.com/images/f2acb3628808848e4c85576caf1653ae/tenor.gif%3Fitemid%3D11802292" width="498" height="258"&gt;&lt;/a&gt;&lt;/p&gt;
 Don’t you love that there's a Supernatural meme for everything? 



&lt;p&gt;Don’t worry though! I will give you all the tips and tricks that helped me in figuring out how to use state and especially where to put it! Hopefully it will prevent you from making similar mistakes that I kept making. 😊&lt;/p&gt;
&lt;h2&gt;
  
  
  Quick reminder of props:
&lt;/h2&gt;

&lt;p&gt;Before we get into state, it's important to understand what props are: &lt;/p&gt;

&lt;blockquote&gt; Props stand for properties and is a keyword used in React. These props pass the data between the different components [1].&lt;/blockquote&gt;

&lt;p&gt;A component in simpler terms is a function and a prop is an argument that is passed into that function. There are two key things to remember about props: that they only get &lt;em&gt;passed down&lt;/em&gt; from a parent to a child component and that the data that is being passed down &lt;em&gt;can’t be changed&lt;/em&gt; by the child. Here is an example of using props:&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="sr"&gt;/components/&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Home&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./Home&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// pass this data down as props to the child component&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../data/user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&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="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="na"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&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="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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="sr"&gt;/components/&lt;/span&gt;&lt;span class="nx"&gt;Home&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Home&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&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;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;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; is a Web Developer&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="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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;//=&amp;gt; Cindy is a Web Developer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we are importing data from an object (user) and passing down a prop of name to the child component (Home) from the parent component (App). Now in Home, we can access the name being passed down by the use of the curly braces and dot notation: &lt;code&gt;{props.name}&lt;/code&gt;. As you can see here the name being passed down from user is Cindy, there is no way to change this other than going into the object user and manually changing it. If the data is something that you want to change, well that's where the state hook comes in to save the day!&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a useState hook?
&lt;/h2&gt;

&lt;p&gt;You might be thinking to yourself, what is a hook? Well, React says that:&lt;br&gt;
&lt;/p&gt;
&lt;blockquote&gt; A hook is a special function that lets you “hook into” React features. For example, &lt;code&gt;useState&lt;/code&gt; is a hook that lets you add React state to function components [2]. &lt;/blockquote&gt; &lt;strong&gt;Awesome&lt;/strong&gt;, what is state exactly? Well state is data that is in your component, that is similar to a prop, but is expected to change over time. AKA users interacting with the DOM, for example a like button or a form submit. State is really helpful because it provides us a way of updating our data within our component. &lt;br&gt;
The way a state hook works is that &lt;code&gt;useState&lt;/code&gt; returns an array with 2 variables: a reference variable to the current value of state and a setter function for setting/updating state. &lt;em&gt;An important tidbit about setter functions is that it triggers a re-render of the page every time state is changed!&lt;/em&gt; This is why state is so great in comparison to vanilla JavaScript, it automatically re-renders only when it needs to based on if state is changed.&lt;br&gt;
I know this can be a little confusing, especially if you are mostly a visual learner like me, so let's look at some basic steps of how to use state!&lt;br&gt;
First step is to import &lt;code&gt;useState&lt;/code&gt;:&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&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="s2"&gt;react&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;Second is to create your new state variable, let’s create a like button:&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;likes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLikes&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next use your state, we want a button to display the increasing likes:&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;function&lt;/span&gt; &lt;span class="nx"&gt;LikeButton&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;likes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLikes&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="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;button&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;likes&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;button&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;Lastly, we want to set the state to update the number of likes every time the button is clicked:&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;function&lt;/span&gt; &lt;span class="nx"&gt;LikeButton&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;likes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLikes&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="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;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="nx"&gt;setLikes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;likes&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="si"&gt;}&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;likes&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;button&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;And that's it! Now we have a like button that will show the amount of times it's been clicked!&lt;/p&gt;

&lt;h2&gt;
  
  
  When do you need State?
&lt;/h2&gt;

&lt;p&gt;React give us three great questions to review when you’re wondering whether or not you need to create state:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Is it passed down from a parent via props? If so, you might not need state.&lt;/li&gt;
&lt;li&gt;Does it remain unchanged over time? If so, you might not need state.&lt;/li&gt;
&lt;li&gt;Can you compute it based on any other existing state or props in your component? If so, you might not need state [3].&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Tip💡: what I did when learning was write these on a sticky note and put it on my monitor so that I could easily refer back to it!&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Lifting State?
&lt;/h2&gt;

&lt;p&gt;I mentioned lifting state earlier and how when I was learning about &lt;code&gt;useState&lt;/code&gt; I found myself having to lift state often. That is because I would put state in the wrong place! Lifting state is moving state to the closest common ancestor of the components that need it. In simpler terms, it is moving state up to its parent component. Note: if there isn’t a good parent component, you can always make one! &lt;/p&gt;

&lt;h2&gt;
  
  
  Where should State live?
&lt;/h2&gt;

&lt;p&gt;Lifting state as a beginner can be confusing, so knowing where state should live is important! A lot of my instructors were teaching us in the beginning to make or draw out the hierarchy of the components to help with understanding which components are parents, siblings, or children of each other. Knowing this information is great for determining where state should go. After a lot of time being stubborn and thinking I could just visualize the hierarchy in my head, I’m here to tell you that my instructors were… that’s correct, you guessed it… they were right! So after getting confused with where state should live multiple times, I eventually tried physically drawing out the components hierarchy and just like that ... CLICK!&lt;br&gt;
I hope that all of you have that click moment too! So here are a few different tips and tricks for visualizing the components and deciding where state should live!&lt;/p&gt;

&lt;p&gt;You could try simply writing/typing out a component hierarchy like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    App
      |- Header
      |- List Container
          |- List
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or my personal favorite, drawing out a diagram like this: &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XdoiM_9n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vghfmrkhqvxfyuljonjp.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XdoiM_9n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vghfmrkhqvxfyuljonjp.PNG" alt="diagram" width="351" height="263"&gt;&lt;/a&gt;&lt;/p&gt;
If you're doing a small project you could just draw this too, just to have it right in front of you, choose whatever way works best for you!



&lt;p&gt;Or if you’re working on a larger project where there are a lot of components, you can use a program to help you create a diagram where you can continually add to it. This option is great because you can also add where you are putting your state in each component in your diagram as you go and keep going back to add to it as you expand your project, like this: &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--u_VVQsrA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x3jcq00puhxrujn9e8m8.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u_VVQsrA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x3jcq00puhxrujn9e8m8.PNG" alt="draw.io diagram" width="880" height="556"&gt;&lt;/a&gt;&lt;/p&gt;
This example is from a project that displays the name, image, and quote of characters on cards with an add new character form and a search bar!




&lt;p&gt;Here are some helpful programs that I found for creating diagrams: &lt;a href="https://app.diagrams.net/"&gt;draw.io&lt;/a&gt;, &lt;a href="https://www.figma.com/file/GBCGqs11KNeUyPksQzEMw0hr/Wireframing/duplicate?node-id=0%3A1"&gt;Figma&lt;/a&gt;, &lt;a href="https://www.lucidchart.com/pages/landing?utm_source=bing&amp;amp;utm_medium=cpc&amp;amp;utm_campaign=_chart_en_us_mixed_search_brand_bmm_&amp;amp;km_CPC_CampaignId=359804038&amp;amp;km_CPC_AdGroupID=1229254155804724&amp;amp;km_CPC_Keyword=%2Blucid%20%2Bcharts&amp;amp;km_CPC_MatchType=p&amp;amp;km_CPC_ExtensionID=%7Bextensionid%7D&amp;amp;km_CPC_Network=o&amp;amp;km_CPC_AdPosition=&amp;amp;km_CPC_Creative=&amp;amp;km_CPC_TargetID=kwd-76828563276432:loc-190&amp;amp;km_CPC_Country=44830&amp;amp;km_CPC_Device=c&amp;amp;km_CPC_placement=&amp;amp;km_CPC_target=&amp;amp;msclkid=49556ed6477816bee3780de916a53ffb"&gt;LucidChart&lt;/a&gt;, and &lt;a href="https://miro.com/apps/?&amp;amp;utm_source=bing&amp;amp;utm_medium=cpc&amp;amp;utm_campaign=S%7CBNG%7CBRN%7CUS%7CEN-EN%7CExact%7CPareto&amp;amp;utm_adgroup=1144592546656023&amp;amp;utm_content=Miro&amp;amp;utm_term=kwd-71537701149770:loc-190&amp;amp;matchtype=e&amp;amp;device=c&amp;amp;gclid=&amp;amp;msclkid=1643ab2722611c7c926dc8187398fab6"&gt;miro&lt;/a&gt;. Note: in the above diagrams I used draw.io!&lt;/p&gt;

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

&lt;p&gt;I hope this helped clear up any confusion about how &lt;code&gt;useState&lt;/code&gt; works and where to put state. For more resources here's a &lt;a href="https://youtu.be/0iNDB-2fg8A"&gt;YouTube video&lt;/a&gt; and &lt;a href="https://reactjs.org/docs/thinking-in-react.html#step-3-identify-the-minimal-but-complete-representation-of-ui-state"&gt;React link&lt;/a&gt; that I found helpful! Happy coding!🧑‍💻&lt;/p&gt;

&lt;p&gt;Resources:&lt;br&gt;
[1] “REACT PROPS: Different examples to implement react props,” EDUCBA, 12-Jul-2021. [Online]. &lt;br&gt;
[2] “Using the State Hook,” React. [Online].&lt;br&gt;
[3]“Thinking in react,” React. [Online]. &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>react</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Basics of For Loops: To For...in or For...of?</title>
      <dc:creator>EmilyFernschild</dc:creator>
      <pubDate>Fri, 16 Sep 2022 12:42:02 +0000</pubDate>
      <link>https://dev.to/emilyfernschild/basics-of-for-loops-to-forin-or-forof-1l83</link>
      <guid>https://dev.to/emilyfernschild/basics-of-for-loops-to-forin-or-forof-1l83</guid>
      <description>&lt;p&gt;Going into learning Javascript, I knew a little bit from my past about the basics of a For loop. I have learned it briefly in past classes and maybe even have used it before. When I began learning more about for statements, I struggled with for...in and for...of. So now I am here to help you! Learning coding can be scary at first, but it can also be really fun. Let me show you! &lt;/p&gt;

&lt;h2&gt;
  
  
  What is a For Loop?
&lt;/h2&gt;

&lt;p&gt;First, let’s make sure you understand for loops. To briefly define a for loop, MDN describes: "the for statement creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement (usually a block statement) to be executed in the loop [1]". In simpler terms, a for statement gives a beginning point for where the loop should start, a way to loop through, and finally an end so that the loop can stop and not be executed forever. It then executes that loop based on the statement that follows. Here’s a simple example of adding:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = '';
for (let i = 0; i &amp;lt; 5; i++) {
  a = a + i;
}
console.log(a);
//=&amp;gt; "01234
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A simple for loop made sense to me when I was learning, so when I was introduced for…in and for…of I had a lot of questions. My first though was, why not just use a regular for loop? Later as I kept learning, I was getting confused, what was the difference between a for…in and for…of anyway? Learning at a rapid pace, it can sometimes be easy to get confused or to forget something. So, it took me a bit to realize that a for…in is to iterate through an &lt;em&gt;object&lt;/em&gt; and for…of is to iterate through all iterable objects such as &lt;em&gt;arrays&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Do You Use a For...In?
&lt;/h2&gt;

&lt;p&gt;A for…in as MDN describes is a “statement [that] iterates over all enumerable string properties of an object (ignoring properties keyed by symbols), including inherited enumerable properties [2]”. In other words, the for…in iterates through an object's keys. Let’s say we have an object with seven key value pairs or seven dwarfs each with a number associated with them like this…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let object = { bashful: 1, doc: 2, dopey: 3, grumpy: 4, happy: 5, sleepy: 6, sneezy: 7 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's create a for…in loop to iterate through each of the seven characters in the object and log a sentence for each!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (const dwarf in object){
    console.log(`the ${object[dwarf]} dwarf is ${dwarf}`)
}
//=&amp;gt; "dwarf 1 is bashful"
//=&amp;gt; "dwarf 2 is doc"
//=&amp;gt; "dwarf 3 is dopey"
//=&amp;gt; "dwarf 4 is grumpy"
//=&amp;gt; "dwarf 5 is happy"
//=&amp;gt; "dwarf 6 is sleepy"
//=&amp;gt; "dwarf 7 is sneezy"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How Do You Use a For...Of?
&lt;/h2&gt;

&lt;p&gt;The difference of a for…of loop as w3schools describes is a “statement [that] loops through the values of an iterable object. It lets you loop over iterable data structures such as Arrays, Strings, Maps, NodeLists, and more [3]”. For learning sake we're going to be using an array for this example. Here is an array with three names…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = ['Robbie', 'Miley', 'Jackson'];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we're going to use a for…of loop to iterate through each name in the array and log each name with the last name attached!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (const name of array) {
   console.log(name + ' Stewart')
};
//=&amp;gt; Robbie Stewart 
//=&amp;gt; Miley Stewart 
//=&amp;gt; Jackson Stewart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;I hope I helped you understand the difference between for…in and for…of loops and when to properly use them. I also hope that through my examples I helped in showing you that coding can be fun!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References:&lt;/strong&gt; &lt;br&gt;
[1] “For - Javascript: MDN.” JavaScript | MDN, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for&lt;/a&gt;. &lt;br&gt;
[2] “For...in - Javascript: MDN.” JavaScript | MDN, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in&lt;/a&gt;.&lt;br&gt;
[3]  “The For Of Loop.” JavaScript for Of, &lt;a href="https://www.w3schools.com/js/js_loop_forof.asp"&gt;https://www.w3schools.com/js/js_loop_forof.asp&lt;/a&gt;.&lt;/p&gt;

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