<?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: AndyDziabo</title>
    <description>The latest articles on Forem by AndyDziabo (@andydziabo).</description>
    <link>https://forem.com/andydziabo</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%2F911317%2Fde643f4f-5d01-494b-942d-5f9b8e079b98.png</url>
      <title>Forem: AndyDziabo</title>
      <link>https://forem.com/andydziabo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/andydziabo"/>
    <language>en</language>
    <item>
      <title>Authentication of Users</title>
      <dc:creator>AndyDziabo</dc:creator>
      <pubDate>Tue, 25 Oct 2022 07:34:25 +0000</pubDate>
      <link>https://forem.com/andydziabo/authentication-of-users-2he0</link>
      <guid>https://forem.com/andydziabo/authentication-of-users-2he0</guid>
      <description>&lt;h1&gt;Introduction&lt;/h1&gt;

&lt;p&gt;In this blog we are going to be talking about a simple setup to authenticate your users using sessions. Using sessions allows you to persist the user as they interact with the application.
&lt;/p&gt;

&lt;h1&gt;Getting Started&lt;/h1&gt;

&lt;p&gt;The first step is to set up your routes in the &lt;code&gt;routes.rb&lt;/code&gt; file. We'll setup two custom routes, one for the login and one for the logout.
&lt;/p&gt;

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

&lt;p&gt;The first route will direct a &lt;code&gt;POST&lt;/code&gt; request to the &lt;code&gt;SessionsController&lt;/code&gt; and use the &lt;code&gt;create&lt;/code&gt; action. Here the &lt;code&gt;find_by&lt;/code&gt; method is used to search the database for the user that is attempting to login. If the user is located in the database, that user will then be set as the user that is held in session. 
&lt;/p&gt;

&lt;p&gt;By setting the user in sessions, that user will persist as the current user while they interact with the application. In the following code you can see the &lt;code&gt;create&lt;/code&gt; action that was used to achieve this.
&lt;/p&gt;

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

&lt;p&gt;
In the above code, you can also see the &lt;code&gt;destroy&lt;/code&gt; action defined. We get to this action using the second custom route from above. In that route we direct a &lt;code&gt;DELETE&lt;/code&gt; request to the &lt;code&gt;SessionsController&lt;/code&gt; and use the &lt;code&gt;destroy&lt;/code&gt; action.
&lt;/p&gt;

&lt;p&gt;
The &lt;code&gt;destroy&lt;/code&gt; action simply deletes the &lt;code&gt;user_id&lt;/code&gt; from session and by doing so the user is no longer persisted so functions that require a &lt;code&gt;user_id&lt;/code&gt; will no longer function.
&lt;/p&gt;

&lt;h1&gt;Frontend&lt;/h1&gt;

&lt;p&gt;
The final step is setting up the front end to actually make the fetch requests for logging in and logging out. First we'll look at logging in.
&lt;/p&gt;

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

&lt;p&gt;
The login is handled by setting up a form for the user to enter their &lt;code&gt;username&lt;/code&gt; and &lt;code&gt;password&lt;/code&gt;. When they submit the for a function is called that makes a fetch request using the login path and sending the username and password. The response is then handled with an &lt;code&gt;if/else&lt;/code&gt; statement. If the response is ok, then the login process can be completed. If there was a problem with the login, such as the user doesn't exist or the password was incorrect, then an error is returned and can be used to notify the user of the issue.
&lt;/p&gt;

&lt;p&gt;
The final step is to handle the logout when the user is finished using the app.
&lt;/p&gt;

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

&lt;p&gt;
When the user clicks on the logout button, a function is called which does a fetch request using the &lt;code&gt;logout&lt;/code&gt; route and sending the &lt;code&gt;DELETE&lt;/code&gt; request that will remove the &lt;code&gt;user_id&lt;/code&gt; from session. 
&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Dealing with multi-level nested API data and creating a seeding file with it</title>
      <dc:creator>AndyDziabo</dc:creator>
      <pubDate>Mon, 03 Oct 2022 01:35:23 +0000</pubDate>
      <link>https://forem.com/andydziabo/dealing-with-multi-level-nested-api-data-and-creating-a-seeding-file-with-it-1elg</link>
      <guid>https://forem.com/andydziabo/dealing-with-multi-level-nested-api-data-and-creating-a-seeding-file-with-it-1elg</guid>
      <description>&lt;p&gt;
Dealing with multi-level nested data from an API call can sometimes be overwhelming. We are going to go through an example from a project that I am currently working on and how it was broken down into smaller, more manageable pieces. The end goal is to create a seeding file to populate a data table.
&lt;/p&gt;

&lt;h1&gt;Evaluating the API Response&lt;/h1&gt;

&lt;p&gt;
The first step we need to do is look at the response that our &lt;code&gt;GET&lt;/code&gt; request will receive from the API. We will look at what data we get, and what data we want to extract to use in our app. For this example we are using the ESPN API, and our goal is to get a list of all the current NFL players. We also want to get some information about each player. In the NFL there are 32 teams, each team having 53 active players. That alone adds up to 1,696 players. Then add in the players on each teams practice squad, and players on injured reserve and the number climbs well over 2,000 players. Now consider that we want to collect info on each player such as: their team name, team location, position they play, their playing status, and a couple other things and its easy to see how this can become overwhelming very quickly.
&lt;/p&gt;

&lt;p&gt;
First things first, lets look at the API return and see what we are dealing with. The end point we are going to be using is:
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/apis/site/v2/sports/football/nfl/teams/team_id/roster
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
When using this endpoint, the &lt;code&gt;team_id&lt;/code&gt; will be replaced with the specific team that you want the roster info for. Here is the return when you use the &lt;code&gt;team_id&lt;/code&gt; of &lt;code&gt;23&lt;/code&gt;.
&lt;/p&gt;

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

&lt;p&gt;
We see that we have received an object with 6 keys: athletes, coach, season, status, team, and timestamp. So we see the team key with the info of the team and location of the team that the players on this roster play for. But the players are in another key that is split up into 3 array objects, one for offense, one for defense, and one for special teams. Lets look at the array items in the athlete key.
&lt;/p&gt;

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

&lt;p&gt;
Now we see that each item in the under the athletes key contains an array of players. Lets go further and look in the first &lt;code&gt;items&lt;/code&gt; array.
&lt;/p&gt;

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

&lt;p&gt;
And now inside one of the players.
&lt;/p&gt;

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

&lt;p&gt;
Lets not forget about the team info for this group of players.
&lt;/p&gt;

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

&lt;p&gt;
So now we can see all the info we are looking for, but this is for just one team, there are still 31 other teams!
&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa1rmx25v292hb1u7aesy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa1rmx25v292hb1u7aesy.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;
At this point its easy to feel overwhelmed but if we start to break it down into smaller steps it begins to feel much more manageable.
&lt;/p&gt;

&lt;h1&gt;Breaking it Down&lt;/h1&gt;

&lt;p&gt;There are many ways to approach a task like this. I find the easiest is to start at top level and work down through. As we move down through each level, take note if there is any data on that level that we want to capture. If there is, then capture it and then continue moving down through the levels. 
&lt;/p&gt;

&lt;p&gt;
The API endpoint returns all the info for a single team so we will have to repeat the call for each team. This tells us we will need some kind of loop to make a call for each one. Because we know that there are a total of 32 teams, we can simply use the &lt;code&gt;#times&lt;/code&gt; method to make a &lt;code&gt;GET&lt;/code&gt; request for each team. Looking into the &lt;code&gt;team_id&lt;/code&gt; we see that they start at 1 and increment up by one. We can declare a variable and set it to 1, then interpolate that variable into the API endpoint for the &lt;code&gt;team_id&lt;/code&gt;, then just increment the variable by one at the end of each loop.
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i = 0
32.times do
    res = RestClient.get "https://site.api.espn.com/apis/site/v2/sports/football/nfl/teams/#{i}/roster"
    player_hash = JSON.parse(res)

    end
    i = i + 1
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
We now have a loop that will return an object for each team and store that object in &lt;code&gt;player_hash&lt;/code&gt;. We can pull the team name, team location, and even the link to the team logo image from the &lt;code&gt;player_hash&lt;/code&gt;. We can now check those data points off the list of what we want to collect and look at the rest of the keys on this level to see if there is anything else to collect.
&lt;/p&gt;

&lt;p&gt;
We are not going to be using any of the data from the timestamp key, the status key, the season key, or the coach key. The only other key of interest is the athletes key. Looking at the athletes key, we can see that it is an array of 3 objects. We can use the&lt;code&gt;#each&lt;/code&gt; method to iterate through them and continue down through the levels looking for data to collect.
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;player_hash["athletes"].each do |group|
    group["items"]

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

&lt;/div&gt;



&lt;p&gt;
This takes us through the athletes array, which contains an &lt;code&gt;items&lt;/code&gt; array, and &lt;code&gt;position&lt;/code&gt; key: value pair. We are not going to be using the general &lt;code&gt;position&lt;/code&gt; information, so we know that there is no data to collect yet and we can continue on. We now iterate through the &lt;code&gt;items&lt;/code&gt; array which is an array containing the player objects. These objects contain all the player information that we want to collect and we can check all the data points we are looking for off our list. We now have access to everything we need to use the &lt;code&gt;#create&lt;/code&gt; method and populate the &lt;code&gt;players&lt;/code&gt; table in our database.
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;group["items"].each do |p|
    Player.create(
        name: p["fullName"],
        position: p["position"]["name"],
        number: p["jersey"],
        status: p["status"]["name"],
        is_drafted: false,
        team_name: player_hash["team"]["name"],
        team_location: player_hash["team"]["location"],
        team_logo: player_hash["team"]["logo"],
        image: p["headshot"]
    )
end      
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
As a last little touch we can give some feed back while the seeding process is taking place with some &lt;code&gt;puts&lt;/code&gt;. Below is the completed seeds.rb file.
&lt;/p&gt;

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

&lt;p&gt;
If you look at the code you will see the added line:
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;puts "Seeding #{player_hash["team"]["name"]} players ..."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; 
If you are still unclear with what exactly is happening, this line might help to visualize what is going on. Lets see the seeding in action.
&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpqdqs0qs1ggjd388n09h.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpqdqs0qs1ggjd388n09h.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;
We have now sucessfully seeded our &lt;code&gt;players&lt;/code&gt; table with all 2,393 NFL players! 
&lt;/p&gt;

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

&lt;p&gt;
A note for those that were looking closely, if you noticed that the &lt;code&gt;.times&lt;/code&gt; was set to 34 instead of 32, good eye. This was done to compensate for the &lt;code&gt;team_id&lt;/code&gt; that the API uses. In the id list, it skips numbers 31 and 32 so the &lt;code&gt;team_id&lt;/code&gt; go from 1 to 30, then skips to 33 and 34.
&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>api</category>
      <category>database</category>
    </item>
    <item>
      <title>How to pass data between sibling components in React</title>
      <dc:creator>AndyDziabo</dc:creator>
      <pubDate>Mon, 12 Sep 2022 01:42:49 +0000</pubDate>
      <link>https://forem.com/andydziabo/how-to-pass-data-between-sibling-components-in-react-2cjg</link>
      <guid>https://forem.com/andydziabo/how-to-pass-data-between-sibling-components-in-react-2cjg</guid>
      <description>&lt;p&gt; To tackle the challenge of passing data between sibling components, we are going to be looking at how data is passed between components in React. We use components to perform various functions that our application needs to be able to run as intended. First, lets start by defining what a component is:&lt;br&gt;

&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fopixx9cw8qumep6ux66g.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fopixx9cw8qumep6ux66g.jpg"&gt;&lt;/a&gt;
&lt;br&gt;
For this example, we will be using function components. We'll start out with 4 components:&lt;br&gt;
a parent component named &lt;code&gt;App&lt;/code&gt;&lt;br&gt;

&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fug2dt06bhn5cvtvusprd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fug2dt06bhn5cvtvusprd.jpg"&gt;&lt;/a&gt;
&lt;br&gt;
A child of &lt;code&gt;App&lt;/code&gt; named &lt;code&gt;Search&lt;/code&gt;&lt;br&gt;

&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn8gfxgl8s3fk9x3kel0s.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn8gfxgl8s3fk9x3kel0s.jpg"&gt;&lt;/a&gt;
&lt;br&gt;
Another child of &lt;code&gt;App&lt;/code&gt; named &lt;code&gt;Container&lt;/code&gt;&lt;br&gt;

&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff52w56fta28noa77edr4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff52w56fta28noa77edr4.jpg"&gt;&lt;/a&gt;
&lt;br&gt;
And a child of &lt;code&gt;Container&lt;/code&gt; named &lt;code&gt;Item&lt;/code&gt;&lt;br&gt;


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

&lt;br&gt;
Here is how the components are arranged in our tree:&lt;br&gt;

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

&lt;/p&gt;

&lt;h1&gt;Parent to Child&lt;/h1&gt;

&lt;p&gt;
One way to get components to start talking to each other is using props. A parent component can pass an argument down to a child component as a prop. An example of this can be seen between the &lt;code&gt;Container&lt;/code&gt; component and the &lt;code&gt;Item&lt;/code&gt; component. &lt;code&gt;Container&lt;/code&gt; fetches a list of data and stores it in the variable &lt;code&gt;list&lt;/code&gt;. The array of &lt;code&gt;list&lt;/code&gt; is then iterated through using the &lt;code&gt;map()&lt;/code&gt; method. Each list item is then passed to the &lt;code&gt;Item&lt;/code&gt; component using the prop named &lt;code&gt;listItem&lt;/code&gt;. &lt;code&gt;Item&lt;/code&gt; then takes the data passed through the prop &lt;code&gt;listItem&lt;/code&gt; and uses it to display the information it contains. This alone does not solve our challenge because only the child component that the prop is passed to has access to that prop and any other sibling components will not. If we want to use the &lt;code&gt;Search&lt;/code&gt; component to perform a search on the list that is held in the &lt;code&gt;Container&lt;/code&gt; component, we will have to find a way to get data between the two. Since the sibling components cannot talk to each other, we will need to use the parent component as a go between. 
&lt;/p&gt;

&lt;h1&gt;Child to Parent&lt;/h1&gt;

&lt;p&gt;
So a parent can send data to a child using a prop, but how can the child send data to the parent? A prop can also be used to send a function. If we use this feature to send a call back function to the child, we will then be able to use inverse data flow to get information back to the parent. To do this, we will set up new variable that uses State to update it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const [query, setQuery] = useState(');


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

&lt;/div&gt;

&lt;p&gt;
&lt;br&gt;
We will then send &lt;code&gt;setQuery&lt;/code&gt; to the &lt;code&gt;Search&lt;/code&gt; component as a prop.&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;Search onQuery={setQuery} /&amp;gt;


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

&lt;/div&gt;

&lt;p&gt;
&lt;br&gt;
Here is what it looks like added to the &lt;code&gt;App&lt;/code&gt; component:&lt;br&gt;

&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frnpl98mkwgh89laf9ywi.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frnpl98mkwgh89laf9ywi.jpg"&gt;&lt;/a&gt;
&lt;br&gt;
Next we look at the &lt;code&gt;Search&lt;/code&gt; component. We accept the prop and deconstruct it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

function Search({ onQuery })


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

&lt;/div&gt;

&lt;p&gt;
&lt;br&gt;
Now instead of simply doing a console log of the search entry, we can pass the search query back to the &lt;code&gt;App&lt;/code&gt; component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

onQuery(e.target.value);


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

&lt;/div&gt;

&lt;p&gt;
&lt;br&gt;
Here is what it looks like added to the &lt;code&gt;Search&lt;/code&gt; compenent:&lt;br&gt;

&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdjftewg5dyifouev7v20.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdjftewg5dyifouev7v20.jpg"&gt;&lt;/a&gt;
&lt;br&gt;
With the search query data now in the &lt;code&gt;App&lt;/code&gt; component, we can simply pass that data down to the &lt;code&gt;Container&lt;/code&gt; component as a prop.&lt;br&gt;

&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdbf2e5pf2zyq9ry3j5la.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdbf2e5pf2zyq9ry3j5la.jpg"&gt;&lt;/a&gt;
&lt;br&gt;
We have successfully passed data from sibling to sibling using the parent as a go between. The query data from the &lt;code&gt;Search&lt;/code&gt; component is now available in the &lt;code&gt;Container&lt;/code&gt; component and the query data can be used to do a search on the &lt;code&gt;list&lt;/code&gt; array.
&lt;/p&gt;

</description>
      <category>react</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Adding a button inside a clickable element</title>
      <dc:creator>AndyDziabo</dc:creator>
      <pubDate>Sat, 20 Aug 2022 14:37:57 +0000</pubDate>
      <link>https://forem.com/andydziabo/adding-a-button-inside-a-clickable-element-47gj</link>
      <guid>https://forem.com/andydziabo/adding-a-button-inside-a-clickable-element-47gj</guid>
      <description>&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;The goal of this task is to add a &lt;code&gt;Delete&lt;/code&gt; button to a clickable list item. The trick for me was that the list item itself was a clickable element. When one of the list items is selected, the details of that list item are displayed on another section of the page. The &lt;code&gt;Delete&lt;/code&gt; button's job is simply to delete the list item associated with it. The issue is that whenever the &lt;code&gt;Delete&lt;/code&gt; button is clicked, it will delete the list item as intended, however because it is a child node of the list item, the list item's click event is also triggered. 
&lt;/p&gt;

&lt;h2&gt;The Code&lt;/h2&gt;

&lt;p&gt;Getting into the code, the first section is a function that builds the list of films.&lt;/p&gt;

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

&lt;p&gt;The list is made using a function that utilizes a &lt;code&gt;for...in&lt;/code&gt; statement to iterate through the film list object. It creates a new list item and populates that list item with the appropriate info for that individual film. It also creates a button with a click event that calls another function which carries out the deleting of the list item. The button is then appended as a child of the list item and the list item appended to the unordered list.&lt;/p&gt;

&lt;p&gt;Now that we have a complete list of the films, we add an event listener to the film list. The event listener calls a function that will display the details of the selected film.&lt;/p&gt;

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



&lt;h2&gt;Building the &lt;code&gt;Delete&lt;/code&gt; button&lt;/h2&gt;

&lt;p&gt;The next step is to make the &lt;code&gt;Delete&lt;/code&gt; button functionable. The button already returns an event response with the &lt;code&gt;onclick&lt;/code&gt; attribute, which is set to call the &lt;code&gt;deleteFilm&lt;/code&gt; function.&lt;/p&gt;

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

&lt;p&gt;The &lt;code&gt;deleteFilm&lt;/code&gt; function takes the &lt;code&gt;id&lt;/code&gt; of the selected film and passes it to a &lt;code&gt;fetch DELETE&lt;/code&gt; request. When the &lt;code&gt;fetch DELETE&lt;/code&gt; request returns, it calls the &lt;code&gt;getFilms&lt;/code&gt; function and repopulates the film list to reflect the change. The issue is that as the code is written now, when the &lt;code&gt;Delete&lt;/code&gt; button is clicked the associated film is deleted as desired, but then we get an error.&lt;/p&gt;

&lt;p&gt;This error happens because when the &lt;code&gt;Delete&lt;/code&gt; button is clicked it functions as desired, but the &lt;code&gt;Delete&lt;/code&gt; button is a child of the list item it's associated with. This means that when the &lt;code&gt;Delete&lt;/code&gt; button is clicked, not only is it's event handler triggered, the event handler for the list item is also triggered. This calls the function that populates the details of the film. The error happens because it is trying to display the details of the film that has been deleted and the info no longer exists.&lt;/p&gt;

&lt;p&gt;Now I'm sure this issue could be handled with some conditional statements that look at each of the events that are triggered and use some attributes to only allow the desired event to fire. Before doing all that, I wanted to see if there was a simpler solution, so I took to Google to see. On the &lt;a href="https://www.w3schools.com/jsref/event_stoppropagation.asp"&gt;W3 schools&lt;/a&gt; website I found the solution I was looking for - the &lt;code&gt;stopPropagation()&lt;/code&gt; method. Here is the definition and usage from the W3 schools website:&lt;/p&gt;

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

&lt;p&gt;This method is exactly what we are looking for and simply adding this one line to the code resolves the error we are getting. It allows the delete function to run, but then blocks the event of the parent node (the list item) from being triggered. Here is the delete function with &lt;code&gt;stopPropagation()&lt;/code&gt; method added.&lt;/p&gt;

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

&lt;p&gt;We now have a functioning list in which we can select the film that we wish to see the details for, as well as delete films we no longer want on the list.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://www.w3schools.com/jsref/event_stoppropagation.asp"&gt;&lt;/a&gt;&lt;a href="https://www.w3schools.com/jsref/event_stoppropagation.asp"&gt;https://www.w3schools.com/jsref/event_stoppropagation.asp&lt;/a&gt;&lt;/p&gt;

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