<?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: Erick Vargas</title>
    <description>The latest articles on DEV Community by Erick Vargas (@erick12).</description>
    <link>https://dev.to/erick12</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%2F850786%2F6d5d3e6a-0946-48e6-9a9a-b1a171a566d0.png</url>
      <title>DEV Community: Erick Vargas</title>
      <link>https://dev.to/erick12</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/erick12"/>
    <language>en</language>
    <item>
      <title>Setting up Password Authentication With B-Crypt</title>
      <dc:creator>Erick Vargas</dc:creator>
      <pubDate>Wed, 29 Jun 2022 16:00:40 +0000</pubDate>
      <link>https://dev.to/erick12/setting-up-password-authentication-with-b-crypt-261o</link>
      <guid>https://dev.to/erick12/setting-up-password-authentication-with-b-crypt-261o</guid>
      <description>&lt;p&gt;Phase-4 at FlatIron school brought-forth several new topics including rails, validations, deployment, and authorization. One of the topics I was having trouble with in this phase was password authentication. As such I chose to write my phase-4 blog about the topic. While I had a hard time wrapping my head around this topic, I am writing this to further push my understanding and to give a different point of view, perhaps to someone who is also struggling with the concept. For this example we'll be using rails and react to set up password authentication for users. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Cookies&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before we can begin adding our routes and creating our methods, we need to make sure cookies are enabled in the application configuration file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore

#This is for security
config.action_dispatch.cookies_same_site_protection = :strict

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

&lt;/div&gt;



&lt;p&gt;We will also need to make sure to add &lt;em&gt;&lt;strong&gt;include ActionController::Cookies&lt;/strong&gt;&lt;/em&gt; in our application controller in order for all controllers to have access to cookies. Cookies are important because it is what will allow us to store the user in a session. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Login&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;We'll start with the login route. For a user to log in we will need a route and a post method in the &lt;strong&gt;&lt;em&gt;SessionsController&lt;/em&gt;&lt;/strong&gt;, as we are creating a session for the user that will be logged in.&lt;br&gt;
The route we'll use will also be a post request and go in the routes.rb file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;post "/login", to: "sessions#create"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;strong&gt;&lt;em&gt;Sessionscontroller&lt;/em&gt;&lt;/strong&gt; we'll add&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class SessionsController &amp;lt; ApplicationController
  def create
    user = User.find_by(username: params[:username])
    if user&amp;amp;.authenticate(params[:password])
      session[:user_id] = user.id
      render json: user, status: :created
    else
      render json: { error: "Invalid username or password" }, status: :unauthorized
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above we are creating a session, where if the password passed in from the params matches the password we have stored in the database, we set the session [:user_id] to the users id. &lt;br&gt;
The front end will look something 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;function Login({ onLogin }) {
  const [username, setUsername] = useState("");
  const [password, setPassword] = useState("");

  function handleSubmit(e) {
    e.preventDefault();
    fetch("/login", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ username, password }),
    })
      .then((r) =&amp;gt; r.json())
      .then((user) =&amp;gt; onLogin(user));
  }

  return (
    &amp;lt;form onSubmit={handleSubmit}&amp;gt;
      &amp;lt;input
        type="text"
        value={username}
        onChange={(e) =&amp;gt; setUsername(e.target.value)}
      /&amp;gt;
      &amp;lt;input
        type="password"
        value={password}
        onChange={(e) =&amp;gt; setPassword(e.target.value)}
      /&amp;gt;
      &amp;lt;button type="submit"&amp;gt;Login&amp;lt;/button&amp;gt;
    &amp;lt;/form&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above we can see that we are making our post request passing in the username and password. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Staying Logged In&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One thing to note is when we refresh the page we lose the value of state which above we have set to &lt;em&gt;password&lt;/em&gt; and &lt;em&gt;username&lt;/em&gt;. Refreshing the page would log the user out and thus would have to log in again. We can handle this by retrieving the user data from the database once the user logs in. For this we would have to create a separate fetch route, that comes from the &lt;strong&gt;&lt;em&gt;UsersController&lt;/em&gt;&lt;/strong&gt; rather than the SessionsController.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;get "/me", to: "users#show"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and in the "UsersController" we add a find method to find the user associated with said account.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class UsersController &amp;lt; ApplicationController
  def show
    user = User.find_by(id: session[:user_id])
    if user
      render json: user
    else
      render json: { error: "Not authorized" }, status: :unauthorized
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the front end we can do a fetch request to keep the user logged in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
  const [user, setUser] = useState(null);

  useEffect(() =&amp;gt; {
    fetch("/me").then((response) =&amp;gt; {
      if (response.ok) {
        response.json().then((user) =&amp;gt; setUser(user));
      }
    });
  }, []);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Creating A User&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;To keep things short B-crypt is a gem that allows us to store our passwords in the database in a secure way. Passwords stored as a hash rather than the real value of the password which would be a security risk. When we create a password in a data-table it isn't stored in a password column, but instead in a column named &lt;em&gt;password_digest&lt;/em&gt;. It is important that this column be named &lt;em&gt;password_digest&lt;/em&gt; if we want to use B-crypt. In the user model we'll make sure to add &lt;strong&gt;&lt;em&gt;has_secure_password&lt;/em&gt;&lt;/strong&gt; in order to hash and salt all passwords.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User &amp;lt; ApplicationRecord
  has_secure_password
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;&lt;em&gt;has_secure_password&lt;/em&gt;&lt;/strong&gt; we added also provides two instances for our "User" model which are &lt;em&gt;password&lt;/em&gt; and &lt;em&gt;password_confirmation&lt;/em&gt; This will allow us to create a new user and add password and password-confirmation fields in a signup form. &lt;br&gt;
*Another bonus is it allows us to raise exceptions to handle any errors. &lt;/p&gt;

&lt;p&gt;Just like before we'll add a create method for a signup form in our front end.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class UsersController &amp;lt; ApplicationController
  def create
    user = User.create(user_params)
    if user.valid?
      render json: user, status: :created
    else
      render json: { errors: user.errors.full_messages }, status: :unprocessable_entity
    end
  end

  private

  def user_params
    params.permit(:username, :password, :password_confirmation)
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The params we are permitting for our create method are the username, password and password-confirmation fields which can be seen below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function SignUp({ onLogin }) {
  const [username, setUsername] = useState("");
  const [password, setPassword] = useState("");
  const [passwordConfirmation, setPasswordConfirmation] = useState("");

  function handleSubmit(e) {
    e.preventDefault();
    fetch("/signup", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        username,
        password,
        password_confirmation: passwordConfirmation,
      }),
    })
      .then((r) =&amp;gt; r.json())
      .then(onLogin);
  }

  return (
    &amp;lt;form onSubmit={handleSubmit}&amp;gt;
      &amp;lt;label htmlFor="username"&amp;gt;Username:&amp;lt;/label&amp;gt;
      &amp;lt;input
        type="text"
        id="username"
        value={username}
        onChange={(e) =&amp;gt; setUsername(e.target.value)}
      /&amp;gt;
      &amp;lt;label htmlFor="password"&amp;gt;Password:&amp;lt;/label&amp;gt;
      &amp;lt;input
        type="password"
        id="password"
        value={password}
        onChange={(e) =&amp;gt; setPassword(e.target.value)}
      /&amp;gt;
      &amp;lt;label htmlFor="password_confirmation"&amp;gt;Confirm Password:&amp;lt;/label&amp;gt;
      &amp;lt;input
        type="password"
        id="password_confirmation"
        value={passwordConfirmation}
        onChange={(e) =&amp;gt; setPasswordConfirmation(e.target.value)}
      /&amp;gt;
      &amp;lt;button type="submit"&amp;gt;Submit&amp;lt;/button&amp;gt;
    &amp;lt;/form&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Deleting a Session&lt;/strong&gt;&lt;br&gt;
We can now sign in and sign up users, but what if we want to delete the current session or logout? It is very similar, where we'll need a route in our route.rb file, a delete method in our "SessionsController" and a request made from our front end, that will be triggered when we click on a Logout button.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#route
delete "/logout", to: "sessions#destroy"

#method in SessionsController
def destroy
  session.delete :user_id
  head :no_content
end

#react/frontEnd request 
function Navbar({ onLogout }) {
  function handleLogout() {
    fetch("/logout", {
      method: "DELETE",
    }).then(() =&amp;gt; onLogout());
  }

  return (
    &amp;lt;header&amp;gt;
      &amp;lt;button onClick={handleLogout}&amp;gt;Logout&amp;lt;/button&amp;gt;
    &amp;lt;/header&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;User authentication for phase-4 was by far one of the most challenging topics for me to comprehend at first, but was still very engaging to learn. As I finish phase-4, I am now able to create full-stack applications. However, there is still much to learn and I am eager to see what else there is out there. &lt;/p&gt;

&lt;p&gt;Sources: &lt;br&gt;
&lt;a href="https://guides.rubyonrails.org/v4.1.4/action_controller_overview.html#accessing-the-session"&gt;Accessing A Session &lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.usenix.org/legacy/event/usenix99/provos/provos.pdf"&gt;B-Crypt&lt;/a&gt; &lt;br&gt;
&lt;a href="https://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html"&gt;Has-Secure Password&lt;/a&gt;&lt;br&gt;
FlatIronSchool &lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>Creating web-API routes with Active Record &amp; Sinatra</title>
      <dc:creator>Erick Vargas</dc:creator>
      <pubDate>Tue, 07 Jun 2022 04:20:55 +0000</pubDate>
      <link>https://dev.to/erick12/creating-web-api-routes-with-active-record-sinatra-acp</link>
      <guid>https://dev.to/erick12/creating-web-api-routes-with-active-record-sinatra-acp</guid>
      <description>&lt;p&gt;Javascript, and react have been some very fun and interesting topics to learn about in these last couple of weeks. Phase-3 which included: Ruby, Active Record and Sinatra is no exception. It is very exciting to think that I can now create the not only the front-end of an application but now the back-end as-well. A topic that certainly stood out for me was building Sinatra applications, but more specifically, building web-API routes. &lt;/p&gt;

&lt;p&gt;For this example we'll be using a data table called Dogs with the following attributes: name, breed, age, trait and image, as-well as a Dog model. Our goal is to develop web-based API routes that can be used to receive requests from a front end application in the JSON format.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Dog &amp;lt; ActiveRecord::Base 

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

&lt;/div&gt;



&lt;p&gt;These routes will go in a folder named controller. While not doing this won't kill your app, it is a common pattern in web development followed by many web app developers. This pattern is known as the MVC or Model-View-Controller, which is used to separate the different types of components an app might have.  The include the models which work with the database, and controllers which are in charge of receiving user requests. &lt;br&gt;
Depending on the front end request the user makes, we will modify our web-API routes. We will mainly focus on the GET, POST, PATCH and DELETE crud methods. We'll start with the GET request. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GET Request&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;We first want to let the application controller know this is a get request, instead of using def which is how we begin most ruby methods, we'll start with get.  Then we define what we want our route to be, in our case we'll use &lt;strong&gt;&lt;em&gt;"/dogs"&lt;/em&gt;&lt;/strong&gt;. Thanks to active record we can also access all our dogs from the database using &lt;strong&gt;&lt;em&gt;Dog.all&lt;/em&gt;&lt;/strong&gt;, we then convert these objects to JSON formatted strings and thats it!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ApplicationController &amp;lt; Sinatra::Base
  set :default_content_type, 'application/json'


  get '/dogs' do
    dogs = Dog.all
    dogs.to_json
  end

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

&lt;/div&gt;



&lt;p&gt;We can double check to make sure our web-API route works by making a fetch request, which should return an array of objects with the correct keys corresponding to our column names. Which it indeed it does!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
        "id": 162,
        "name": "Bella",
        "breed": "French",
        "age": 2,
        "trait": "Curious",
        "image": "https://images.unsplash.com/photo-1616312513065-28cf4313abda?ixlib=rb-1.2.1&amp;amp;ixid=MnwxMjA3fDB8MHxwaG90by1yZWxhdGVkfDE0fHx8ZW58MHx8fHw%3D&amp;amp;w=1000&amp;amp;q=80"
    },
    {
        "id": 163,
        "name": "Tony",
        "breed": "Beagle",
        "age": 1,
        "trait": "Couragous",
        "image": "https://images.unsplash.com/photo-1611306133736-56a3b973b2cc?ixlib=rb-1.2.1&amp;amp;ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8YmVhZ2xlJTIwZG9nfGVufDB8fDB8fA%3D%3D&amp;amp;w=1000&amp;amp;q=80"
    }
  ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;POST&lt;/strong&gt; &lt;br&gt;
If the user wants add a new dog to our database, we need a way to access the data submitted in the body of the request. We can access this data through the &lt;a href="https://flatironschool.com/blog/how-params-works-in-rails/"&gt;params hash&lt;/a&gt;. &lt;br&gt;
 To make the post route we can use use the same route used for the patch request, as the user is adding an additional dog instance to our already existing dog database. We are however creating a new dog, instead of using &lt;em&gt;&lt;strong&gt;Dog.all&lt;/strong&gt;&lt;/em&gt; which lists all our dog instances in the data base, we would instead use &lt;strong&gt;&lt;em&gt;Dog.create&lt;/em&gt;&lt;/strong&gt; followed by our params to match our database attributes to the received data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;post "/dogs" do
    dog = Dog.create(name: params[:name], breed: params[:breed], age: params[:age], trait: params[:trait], image: params[:image])
    dog.to_json
 end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;PATCH&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A patch request is similar to a post request but with an added bonus. If a user wants to edit the name of one of dogs in the database, we need a way for our route to specifically edit that particular dog. We'll define our route as "&lt;strong&gt;&lt;em&gt;/dogs/:id&lt;/em&gt;&lt;/strong&gt;" as each of our dog instances has its own id thanks to active record. We start by first finding the specified dog by the user. Once again we use active record, &lt;strong&gt;&lt;em&gt;Dog.find(params[:id])&lt;/em&gt;&lt;/strong&gt;. Now that we have the specified dog, we can update our database accordingly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; patch '/dogs/:id' do
    dog = Dog.find(params[:id])
    dog.update(
      name: params[:name],
      breed: params[:breed],
      age: params[:age], 
      trait: params[:trait],
      image: params[:image]
    )
    dog.to_json
  end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;DELETE&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;A delete response much like the patch request needs the id of the specified dog we want to delete. The route we'll use is &lt;strong&gt;&lt;em&gt;"dogs/:id"&lt;/em&gt;&lt;/strong&gt;, find the dog in our database and then delete said dog, &lt;strong&gt;&lt;em&gt;Dog.find(params[:id]).destroy&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; delete "/dogs/:id" do
    dog = Dog.find(params[:id])
    dog.destroy
    dog.to_json
  end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are many possibilities when accessing the data in our database when making API routes. We can make it so we only return the first five instances in our database Dog.all.limit(5) in a fetch request, or order them alphabetically. We can edit how we want our data to be returned to the user based on the request.  &lt;/p&gt;

&lt;p&gt;Being able to set up our own web API is the highlight of this phase for me, although learning active record is a close second. I am very excited to see what new things I'll learn in the ruby on rails section, and what kind of projects I'll be able to create. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Components &amp; Props</title>
      <dc:creator>Erick Vargas</dc:creator>
      <pubDate>Mon, 16 May 2022 15:07:41 +0000</pubDate>
      <link>https://dev.to/erick12/components-props-466i</link>
      <guid>https://dev.to/erick12/components-props-466i</guid>
      <description>&lt;p&gt;&lt;strong&gt;Intro about React&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;With a new phase comes new things to learn, and this phase the focus revolved around REACT. With react we are able to vastly expand on what we had learned about basic Javascript. &lt;/p&gt;

&lt;p&gt;To make use of REACT and apply what we have learned our group decided to create an app that made use of of Reacts many functions. Our goal was to create an app that would minimally have multiple components, make use of client side routes, and be able to incorporate an external API. The API we decided on was a database comprised of zoo-animals which included several facts about such animals. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Making use of Components&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;One of my favorite things about react is the use of components and how easy it is to keep track of ones code. You can easily find exactly what you're looking for, granted you organized your own code to begin with. &lt;br&gt;
Our first step consisted of fetching data from our json file, and setting that equal to our state. However using state will cause our fetch function to re-render again and again as soon as the data extracted is set equal to state. To prevent this we used the useEffect hook to make sure our fetch function only runs once. &lt;/p&gt;

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

&lt;p&gt;Now that we have access to our data, we could then pass this data as a prop to our ZooAnimalList component in order to extract specific parts of our object data such as name, and id just to name a few.&lt;/p&gt;

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

&lt;p&gt;Having extracted the data we wanted, we could now pass on these pieces our ZooAnimalCard component which is in charge of actually displaying these pieces of information on to our DOM with the desired HTML. &lt;/p&gt;

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

&lt;p&gt;Passing data between components is not the only use of props. We can also pass on functions that can be defined in one component and then be used in a completely different one. We managed to pass our data as props from one component to another in order to display our animal cards on screen with the appropriate information. However we also want to be able to submit our own card if we wish to do so. The steps needed for this would be very similar. &lt;br&gt;
We first made an AnimalForm component which took user input through onchange, and onSubmit events. We make a post request to our server. Once a response is received we pass it our handleAddAnimal function as newAnimal. &lt;/p&gt;

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

&lt;p&gt;The handleAddAnimal function was defined in our ZooAnimalPage component but was passed down through props. We can call a function even though it is in a completely different file! By updating our state value to include the newly added user input we can then update the DOM directly.  &lt;/p&gt;

&lt;p&gt;Components and props have been one of the most interesting and fun concepts I have learned to date. The way they work together to create such great products, it is no surprise why REACT so widely used. While there is still a lot to learn, it is definitely exciting to think of the types of applications I'll be able to create in the future. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>react</category>
      <category>programming</category>
    </item>
    <item>
      <title>HTTP Verbs &amp; JSON</title>
      <dc:creator>Erick Vargas</dc:creator>
      <pubDate>Mon, 25 Apr 2022 04:13:08 +0000</pubDate>
      <link>https://dev.to/erick12/http-verbs-json-4k58</link>
      <guid>https://dev.to/erick12/http-verbs-json-4k58</guid>
      <description>&lt;p&gt;As someone who is new to software engineering there have been topics thrown at me left and right. While some of them are easier than others to understand, there was a particular topic that peaked my interest and that is the way JSON and servers communicate with one another.  Before that I think some background information on how the internet works could help digest the information a little easier. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JSON&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;JavaScript Object Notation (JSON) is a type of data-interchange format used by many different coding languages including JavaScript. JSON is used as a medium of communication between a client and a server, which you can then use transport or store data between the two. An advantage JSON has over other data formats is the way its data is stored. The data is stored as text based data which is then arranged similarly as an object, or array. &lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Fetch/Get&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There will be times when we want to retrieve data from other servers/databases using JSON. We can do this through a Get request. &lt;br&gt;
When we want to retrieve data we start with the fetch function, fetch("_____"). Where we pass an argument which contains the source or source path of the information we want to obtain. &lt;/p&gt;

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

&lt;p&gt;During this step a response comes back as a promise and we then pass it to our .then() function which converts it to a JSON format, or a format we can work with. In order to then pass this converted data to our next .then function however we have to return such data. If we are using arrow notation we don't have to physically type return as it is implied with arrow notation. &lt;/p&gt;

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

&lt;p&gt;We then follow up with a second .then() function, that allows us receive the returned data and pass to a function where we can now work with the data. &lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Errors and catch:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There will be times where you will run into errors (shoker), If something goes wrong and json fails to retrieve data, we wouldn't know immediately, or at least where the error could be coming from. For this we add the catch function to give us a hint as to what could be the source of the error.  &lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Get&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Unlike fetch which only retrieves data, post on the other hand allows us to update our database with new data. While fetch only required us to have the path to our desired data as an argument, for POST and the following HTTP verbs we need to pass a second argument in the form of an object.&lt;/p&gt;

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

&lt;p&gt;This object must include three components, the method, a header, and a body. The header for this case would be "POST" as we are posting data on to our database. There are more verbs that do different things but that will be later on. &lt;br&gt;
The header key contains the "content type" we are sending which in our case is in the json format, and the type of information we will accept, which is also in json format. &lt;br&gt;
The body key is where we add the data that we want to pass to the server. JSON however will only store text-based data so we convert any data we are passing as objects into strings. We do that through the stringify method. The data is passed and returns as a string which is then ready to be sent. All together this looks like this: &lt;/p&gt;

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

&lt;p&gt;With the next couple of methods, the structure will remain relatively the same, the biggest change would be the verb we use as the method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Put&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;The put request method is structured  similar, but instead of of posting or adding something to the database, it instead updates an already existing set of data. In the fetch data path, an identifier is needed to access the specific data set you want to update. Most likely will be the id of the data set you want to update. The method type would change to put, and the the data you wish to add would then go in the stringify function. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DELETE&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The delete request method simply deletes the specified data in the database. Again we alter the endpoint of our fetch request and change the method type to DELETE. In this case we do not need a body as we are not sending any data but instead sending a request to delete the specified data. &lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;HEAD&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This method is very similar to get/fetch except it does not require a response. This method is mainly used to to obtain meta-information without transferring actual data. Such as testing hyperlinks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Trace&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Trace is used to receive trace calls or signals that report how long a certain action is taking. This is done by looping a certain action which in turn return a trace call with data that can then be used for debugging or improving certain aspects of a program.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Options&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Options is straightforward and describes the communication options for the target source. How one can access/use resources and what methods one can use to access them. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Connect&lt;/strong&gt;  &lt;/p&gt;

&lt;p&gt;This method allows for a two way communication between the the server and the client.&lt;/p&gt;

&lt;p&gt;Communication between servers can be tricky to understand at first, but the more you learn about it the more fascinated you become. I've used a computer for almost my entire life and never thought I'd understand how computers and servers communicate. I know it's not the entire puzzle but knowing how a small piece works is intriguing and only makes me look forward to learning more about software engineering. &lt;/p&gt;

&lt;p&gt;Sources: &lt;br&gt;
&lt;a href="https://www.oracle.com/technical-resources/articles/java/json.html"&gt;https://www.oracle.com/technical-resources/articles/java/json.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.pragmaticapi.com/blog/2013/02/14/restful-patterns-for-the-head-verb/"&gt;https://www.pragmaticapi.com/blog/2013/02/14/restful-patterns-for-the-head-verb/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element"&gt;https://developer.mozilla.org/en-US/docs/Web/HTML/Element&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
