<?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: ArmandeepKaur</title>
    <description>The latest articles on DEV Community by ArmandeepKaur (@codingkaur).</description>
    <link>https://dev.to/codingkaur</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%2F930886%2F47f7efcd-e80b-4f02-afe1-67f0e8e947de.png</url>
      <title>DEV Community: ArmandeepKaur</title>
      <link>https://dev.to/codingkaur</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codingkaur"/>
    <language>en</language>
    <item>
      <title>Authentication using React and Ruby on Rails: creating a login, staying logged in, and logging out</title>
      <dc:creator>ArmandeepKaur</dc:creator>
      <pubDate>Fri, 16 Dec 2022 18:03:54 +0000</pubDate>
      <link>https://dev.to/codingkaur/creating-a-login-page-using-react-and-ruby-on-rails-5615</link>
      <guid>https://dev.to/codingkaur/creating-a-login-page-using-react-and-ruby-on-rails-5615</guid>
      <description>&lt;p&gt;In this post, I will explain how to create a login page using a Ruby on Rails backend and React frontend.&lt;/p&gt;

&lt;h2&gt;
  
  
  Add sessions and cookies to application.rb
&lt;/h2&gt;

&lt;p&gt;In config/application.rb, add the following code:&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

    # Use SameSite=Strict for all cookies to help protect against CSRF
    config.action_dispatch.cookies_same_site_protection = :strict
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Update application_controller.rb
&lt;/h2&gt;

&lt;p&gt;In app/controllers/application.rb, add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;include ActionController::Cookies
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows all of our controllers to work with the cookies. After adding the above code, your application_controller.rb should look 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;class ApplicationController &amp;lt; ActionController::API
  include ActionController::Cookies
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Create a sessions_controller.rb file
&lt;/h2&gt;

&lt;p&gt;One can create a sessions_controller.rb file by entering the following code in the terminal. The command utilizes a rails generator to create a sessions controller.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails g controller Sessions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Create a login path
&lt;/h2&gt;

&lt;p&gt;In config/routes.rb, add the following route:&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;h2&gt;
  
  
  Create a login request on the frontend
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Login(){

    const [username, setUsername] = useState("")
    const [password, setPassword] = useState("")


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

     return (
        &amp;lt;div className="login-div"&amp;gt;
            &amp;lt;form  className="form" onSubmit={handlSubmit}&amp;gt;
                &amp;lt;input 
                    id="username"
                    type="text"
                    onChange={(e) =&amp;gt;   setUsername(e.target.value)}
                    value={username}
                    placeholder="username"/&amp;gt;
                &amp;lt;input 
                    id="password"
                    type="password"
                    value={password}
                    onChange={(e) =&amp;gt; setPassword(e.target.value)}
                    placeholder="password"
                /&amp;gt;
                &amp;lt;input 
                type="submit"
                value="Login"
                className="login_buttons"
                /&amp;gt;
                &amp;lt;h3&amp;gt;OR&amp;lt;/h3&amp;gt;
                &amp;lt;button onClick={handleAccount} className="login_buttons"&amp;gt;Create Account&amp;lt;/button&amp;gt;
            &amp;lt;/form&amp;gt;
        &amp;lt;/div&amp;gt;
    )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code create a login form and creates a post request for the login path defined in config/routes.rb. These steps will ensure that a person can login using their username and password.&lt;/p&gt;

&lt;h2&gt;
  
  
  Remain logged in
&lt;/h2&gt;

&lt;p&gt;To remain logged in, one must define a route that fetches the data for the user who matches the session id created in the last step. &lt;/p&gt;

&lt;p&gt;In config/routes.rb, define the following route:&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;In your user_controller.rb, create a show method that finds a user with the same session created in the login.&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;Finally, create a get request in React that fetches the data for that specific user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const [me, setMe] = useState([])


  useEffect(() =&amp;gt; {
    fetch("/me")
    .then((resp) =&amp;gt; resp.json())
    .then((info) =&amp;gt; setMe(info))
  },[])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Logging out
&lt;/h2&gt;

&lt;p&gt;In config/routes.rb, create the following route:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;In sessions controller, define a delete method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def destroy
  session.delete :user_id
  head :no_content
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In React, create a delete request like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function logout(){
  fetch("/logout", {
  method: "DELETE",
  }).then(navigate("/login"));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Creating a login page using ruby and rails and react can be very simple using the above code. One must define the correct routes, define the correct methods in the right controller, and create the correct requests on the frontend.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>CRUD in Ruby</title>
      <dc:creator>ArmandeepKaur</dc:creator>
      <pubDate>Mon, 28 Nov 2022 19:49:07 +0000</pubDate>
      <link>https://dev.to/codingkaur/crud-in-ruby-48pa</link>
      <guid>https://dev.to/codingkaur/crud-in-ruby-48pa</guid>
      <description>&lt;p&gt;This post goes over how to create routes in Ruby-Sinatra that respond to fetch requests, post requests, patch requests, and delete requests. In order for Ruby to act as the backend server and respond to requests from the frontend, one must have installed the needed dependencies, created necessary migrations, created seed data, set up models accurately, and created CRUD routes. &lt;/p&gt;

&lt;h2&gt;
  
  
  Install Necessary Dependencies
&lt;/h2&gt;

&lt;p&gt;In order to install necessary depending for ActiveRecord and Sinatra to work, run &lt;code&gt;bundle install&lt;/code&gt; in the terminal. &lt;/p&gt;

&lt;h2&gt;
  
  
  Create Migrations
&lt;/h2&gt;

&lt;p&gt;In order to create a migration, run &lt;code&gt;bundle exec rake db:create_migration NAME=create_&amp;lt;Name of migration&amp;gt;&lt;/code&gt;. &lt;br&gt;
For example, In order to create a migration for a model named Review run &lt;code&gt;bundle exec rake db:create_migration NAME=create_reviews&lt;/code&gt;. To create a migration for a model named Restaurant, run &lt;code&gt;bundle exec rake db:create_migration NAME=create_restaurants&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will create a migration file in the db folder. In the newly create migration file, one can create a table with specific attributes. &lt;/p&gt;

&lt;p&gt;For example, we can create the following table in db/migrate/20221114231536_create_reviews.rb:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class CreateReviews &amp;lt; ActiveRecord::Migration[6.1]
  def change
    create_table :reviews do |t|
      t.string :username
      t.string :date
      t.integer :rating 
      t.string :review
      t.belongs_to :restaurant
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This table contains a username which takes a string, a date which takes a string, a rating which takes an integer, a review which takes a string, and a foreign key for model Restaurant.&lt;/p&gt;

&lt;p&gt;we can create the following table in db/migrate/20221114231542_create_restaurants.rb:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class CreateRestaurants &amp;lt; ActiveRecord::Migration[6.1]
  def change
    create_table :restaurants do |t|
      t.string :name
      t.string :location
     end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This table contains attributes name and location both of which take a string.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create Seed Data
&lt;/h2&gt;

&lt;p&gt;After creating all necessary migrations, the next step is to create seed data in db/seeds.rb. Following is an example of some seed data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//create seed data for 
Snooze = Restaurant.create(name: "Snooze, an A.M. Eatery", location: "3940 5th Ave San Diego, CA 92103")

Din_Tai = Restaurant.create(name: "Din Tai Fung",location: "4301 La Jolla Village Dr Bldg P Unit 2000 San Diego, CA 92122")

//Review belongs to Snooze as shown under restaurant attribute
Review.create(username: "Camille Jones", date: "10/5/2021", rating: 5, review: "Very good breakfast items. We got the French toast and the shrimp and grits and the eggs benedict. I highly recommend this place and the staff was very attentive.", restaurant: Snooze)

//Review belongs to Din_Tai as shown under restaurant attribute
Review.create(username: "Camille Jones", date: "07/25/2022", rating: 5, review: "Created this account just to write this review because the service was EXCEPTIONAL :)We are very excited to go back again !", restaurant: Din_Tai)


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

&lt;/div&gt;



&lt;p&gt;In the example above, we create seed data for Restaurant first. We then use the Restaurant data in the Review data for the restaurant attribute to specify the relationship between a Review and a Restaurant (in other words, we specify which Review belongs to which restaurant).&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Models
&lt;/h2&gt;

&lt;p&gt;For the purposes of this post, A restaurant has many reviews and a review belongs to a restaurant. We can specify this relationship in app/restaurant.rb and app/review.rb.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app/restaurant.rb

class Restaurant &amp;lt; ActiveRecord::Base
    has_many :reviews
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app/review.rb
class Review &amp;lt; ActiveRecord::Base
    belongs_to :restaurant
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Set up CRUD responses (JSON format)
&lt;/h2&gt;

&lt;p&gt;After installing dependencies, creating migrations, creating seed data, and setting up models, we can finally define our CRUD routes in app/controllers/application_controller.rb responsible for responding to CRUD requests.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating a Get (Read) route for all Restaurant data and its associate Reviews&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The following code will do all of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;respond with all Restaurant instances in json format
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;get "/restaurants" do
  restaurants = Restaurant.all
  restaurants.to_json
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;respond with all Restaurant instances in json format including its associate reviews
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;get "/restaurants" do
  restaurants = Restaurant.all
  restaurants.to_json(include: :reviews)
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Creating a Post (Create) route&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In order to create a new Review, the post route will look like the following:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;post '/review' do
  review = Review.create(
    username: params[:username],
    date: params[:date],
    review: params[:review],
    rating: params[:rating],
    restaurant_id: params[:restaurant_id]
  )
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Creating a Delete route&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In order to delete a review, create a delete route. Add the /:id in the path and use .find() method to find a review by its id so that specific review can be deleted.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Creating a Patch (Update) route&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In order to respond to a Patch request, create a patch route. Add "/:id" to the path and use .find() method to find a review by its id so it can be updated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;patch '/review/:id' do
  review = Review.find(params[:id])
  review.update(
    date: params[:date],
    username: params[:username],
    rating: params[:rating],
    review: params[:review]
  )
  review.to_json
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>React Events: Simplified</title>
      <dc:creator>ArmandeepKaur</dc:creator>
      <pubDate>Fri, 04 Nov 2022 17:52:01 +0000</pubDate>
      <link>https://dev.to/codingkaur/events-in-react-simplified-2a2</link>
      <guid>https://dev.to/codingkaur/events-in-react-simplified-2a2</guid>
      <description>&lt;p&gt;In my previous &lt;a href="https://dev.to/codingkaur/javascript-events-simplified-34ep"&gt;post&lt;/a&gt;, I talked about Events and Event Handling in JavaScript. In this post, I wanted to continue building on that and review Events and Event Handling in React. Events in React are simpler to use than events in JavaScript. In this post, I will go over the steps needed to add an event listener to a React element and show examples of onClick, onSubmit, and onChange events.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding an Event Listener
&lt;/h2&gt;

&lt;p&gt;To add an event listener to a React Element:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add the event listener to the element of interest&lt;/li&gt;
&lt;li&gt;Create a callback function &lt;/li&gt;
&lt;li&gt;Pass the callback function as a function reference to the event listener&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example, if we have the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function example(){
  return (&amp;lt;button&amp;gt;Hello&amp;lt;button&amp;gt;)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add the event listener to the element of interest. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Event Listener names are a combination of &lt;code&gt;on&lt;/code&gt; and the event name itself, such as "Click". Additionally, the event Listener names are camelCased.&lt;/p&gt;

&lt;p&gt;The following is an example of the &lt;code&gt;onClick&lt;/code&gt; event listener:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function example(){
  return &amp;lt;button onClick={}&amp;gt;Hello&amp;lt;button&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create a callback function
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function example(){
  function eventHandler(){
    console.log("This is the callback function!")
  }
  return &amp;lt;button onClick={}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Pass in the callback function as a function reference
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function example(){
  function eventHandler(){
    console.log("This is the callback function!")
  }
  return &amp;lt;button onClick={eventHandler}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  onClick Event Listener Example
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Add the onClick event listener to the element of interest.&lt;/li&gt;
&lt;li&gt;Create a callback function that handles the event.&lt;/li&gt;
&lt;li&gt;Pass it as a function reference to the event listener.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function example(){
  //create a callback function 
  function handleClick(){
    console.log("this function handles the click event")
  }
  //add the event listener to the element of interest
  //pass in the callback function
  return &amp;lt;button onClick={handleClick}&amp;gt;&amp;lt;button&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  onSubmit Event Listener Example
&lt;/h2&gt;

&lt;p&gt;The onSubmit event listener is used for forms. The onSubmit event listener must be added to the form tag. Additionally, always add e.preventDefault() to prevent any unwanted default behavior.&lt;/p&gt;

&lt;p&gt;In order to use the onSubmit event listener:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add the onSubmit event listener to the element of interest.&lt;/li&gt;
&lt;li&gt;Create a callback function that handles the event.&lt;/li&gt;
&lt;li&gt;Pass it as a function reference to the event listener.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function form(){
  //create a callback function that handles the event listener
  function handleSubmit(e){
    //add e.preventDefault()
    e.preventDefault()
    console.log(this function handles the form submit event")
  }

  return(
    //add the event listener onSubmit to the `&amp;lt;form&amp;gt;` tag
    //pass in the callback function as a function reference
    &amp;lt;form onSubmit={handleSubmit}&amp;gt;
      &amp;lt;input type="text" name="name"&amp;gt;
    &amp;lt;form&amp;gt;
   )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  onChange Event Listener Example
&lt;/h2&gt;

&lt;p&gt;The onChange event listener is useful in handling changes to input fields. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add the onChange event listener to the element of interest.&lt;/li&gt;
&lt;li&gt;Create a callback function that handles the event.&lt;/li&gt;
&lt;li&gt;Pass it as a function reference to the event listener.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function form(){
  //create a callback function that handles the event
  function handleChange(){
    console.log("This function handles the onChange event!")}

  return(
    //add the event listener onChange to the `&amp;lt;input&amp;gt;` tag
    &amp;lt;form&amp;gt;
      &amp;lt;input type="text" name="name" onChange={handleChange}&amp;gt;
    &amp;lt;form&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;In conclusion, creating an event listener in React is much easier in comparison to JavaScript. One can add an event listener in 3 easy steps: 1. Add the event listener to the element of interest 2. Create a callback function 3. Pass the callback function as a function reference to the event listener. &lt;/p&gt;

&lt;p&gt;For more information, visit:&lt;br&gt;
&lt;a href="https://reactjs.org/docs/handling-events.html"&gt;Event Handling&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.w3schools.com/react/react_events.asp"&gt;React Events&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>events</category>
      <category>eventlistener</category>
      <category>eventhandler</category>
    </item>
    <item>
      <title>JavaScript Events: Simplified</title>
      <dc:creator>ArmandeepKaur</dc:creator>
      <pubDate>Fri, 14 Oct 2022 17:31:49 +0000</pubDate>
      <link>https://dev.to/codingkaur/javascript-events-simplified-34ep</link>
      <guid>https://dev.to/codingkaur/javascript-events-simplified-34ep</guid>
      <description>&lt;p&gt;When I started learning JavaScript, The first concept that I really struggled with was JavaScript Events. As a result of my experiences with JavaScript events, I wanted to create a blog simplifying JavaScript Events, how to listen for them, and how to respond to them. &lt;/p&gt;

&lt;h2&gt;
  
  
  What are JavaScript Events?
&lt;/h2&gt;

&lt;p&gt;To begin, JavaScript is able to listen for events happening in the browser. These events can include but are not limited to Keydown, Form Submission, Mouse Click, Mouseover, on-change, mouse-enter, focus, blur etc. In order to listen for these events, and to respond to these events, one can utilize the addEventListener() method.&lt;/p&gt;

&lt;h2&gt;
  
  
  addEventListener() Method
&lt;/h2&gt;

&lt;p&gt;.addEventListener() is a method in JavaScript that can be used to listen for and respond to events happening in the browser. The method takes in two arguments:&lt;/p&gt;

&lt;p&gt;Argument 1: the type of the event we are &lt;em&gt;listening&lt;/em&gt; for (eg. "click", "submit" etc.)&lt;/p&gt;

&lt;p&gt;Argument 2: A callback function that &lt;em&gt;responds&lt;/em&gt; to the specified event in a specified manner. &lt;/p&gt;

&lt;h2&gt;
  
  
  Steps to using .addEventListener()
&lt;/h2&gt;

&lt;p&gt;In order to use .addEventListener() we must:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Grab the element that we want to add an event listener to and save the element as a reference in a variable.&lt;/li&gt;
&lt;li&gt;Add the .addEventListener() method to the variable containing the element of interest. &lt;/li&gt;
&lt;li&gt;Pass the event type and a callback function as the arguments to the addEventListener() method&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 1: Grab the Element of Interest
&lt;/h2&gt;

&lt;p&gt;The following methods can be used to grab the element of interest:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;document.getElementById()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Note: This method will return the element with the requested Id. This method is the most specific method since all Ids are unique to their own elements.&lt;/p&gt;

&lt;p&gt;example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//the HTML Element we want to add an event to
&amp;lt;button id="button" className="btn"&amp;gt;&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Using, document.getElementById(),I entered the id of the button in the parenthesis, added quotations marks to the id, and saved the element to a variable called buttonById
const buttonById = document.getElementById("button")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;document.getElementsByClassName()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Note: This method will return a list of elements with the specified class name. This method is not recommended when we are looking to grab a specific element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//the HTML element we would like to grab
&amp;lt;button id="button" className="btn"&amp;gt;&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//used document.getElementByClassName, inserted the className into the parenthesis, and assigned the element to a variables called buttonByClass
const buttonByClass = document.getElementsByClassName("btn")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;document.getElementsByTagName()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Note: This method will return all elements with the specified Tag Name. This method is the least specific and is not recommended when we are looking to grab one specific element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//the HTML element we would like to grab
&amp;lt;button id="button" className="btn"&amp;gt;&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//used document.getElementsByTagName(), passed the Tag Name surrounded by quotation marks, and assigned to a variable called buttonByTag
const buttonByTag = document.getElementsByTagName("button")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Other methods include CSS Selectors such as:&lt;br&gt;
&lt;strong&gt;document.querySelector(selector)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;document.querySelector will return the first element that matches the requested selector. querySelector can be used to grab an element by its Id, Class Name, and Tag Name.&lt;/p&gt;

&lt;p&gt;If we look at the following HTML elements&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;button id="button1" className="btn"&amp;gt;button 1&amp;lt;/button&amp;gt;
&amp;lt;button id="button2" className="btn"&amp;gt;button 2&amp;lt;/button&amp;gt;
&amp;lt;button id="button3" className="btn"&amp;gt;button 3&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To grab the first element by its id using querySelector, we need to pass a # into the parenthesis followed by its Id.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const buttonQueryId = document.querySelector("#button")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To grab the first element by its Class Name using querySelector, we need to pass a . into the parenthesis followed by its className.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const buttonQueryClass = document.querySelector(".btn")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To grab the first element by its Tag Name using querySelector, we need to simply pass the Tag Name with quotation marks.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const buttonQueryTag = document.querySelector("button")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;document.querySelectorAll(selector)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;document.querySelectorAll() will return a nodeList of the elements that match the criteria of the selector. The method grabs elements by their Class Name and Tag Name.&lt;/p&gt;

&lt;p&gt;For example,&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;button id="button1" className="btn"&amp;gt;button 1&amp;lt;/button&amp;gt;
&amp;lt;button id="button2" className="btn"&amp;gt;button 2&amp;lt;/button&amp;gt;
&amp;lt;button id="button3" className="btn"&amp;gt;button 3&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to grab elements by their class:&lt;br&gt;
&lt;code&gt;const buttonsClass = document.querySelectorAll(".btn")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;to grab elements by their div:&lt;br&gt;
&lt;code&gt;const buttonsTagName = document.querySelectorAll("button")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To learn more about the above two methods, visit this &lt;a href="https://www.w3schools.com/jsref/met_document_queryselector.asp"&gt;website&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2: Add the addEventListener() method
&lt;/h2&gt;

&lt;p&gt;We can add the .addEventListener() method to the variable names that contain our HTML Element/s of interest.&lt;/p&gt;

&lt;p&gt;example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//.addEventListener() to a button we grabbed using getElementById and saved to a variables called buttonById

buttonById.addEventListener()

//.addEventListener() to a button we grabbed using querySelector and saved to a variable called buttonQueryClass

buttonQueryClass.addEventListener()

//.addEventListener to buttons we grabbed using querySelectorAll and saved to a variable called buttonsTagName 

buttonsTagName.addEventListener()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Pass in the arguments
&lt;/h2&gt;

&lt;p&gt;Now we can move on to the final step, which is passing in the arguments. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click Event&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For example, if we want add a &lt;strong&gt;click event&lt;/strong&gt; to our button with a call back function that will console.log "hello" for every click, we could write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//the HTML Element we want to click
&amp;lt;button id="button" className="btn"&amp;gt;&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//step 1: grab the element

const buttonById = document.getElementById("button")

//step 2: add the .addEventListener() method 
//step 3: pass in event type and callback function as arguments

buttonById.addEventListener("click", myCallbackFunction) 

//the callback function that will console.log "hello"

function myCallbackFunction(){
   console.log("hello")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Form Submission&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If we want to add an event listener to a form which will console.log "hello" every time it is submitted:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;We must add an eventListener to the &lt;/p&gt; element instead of the button element&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We must use "submit" as our event type &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We must add event.preventDefault() in the callback function to prevent any default behavior of the form  &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//basic HTML form 
&amp;lt;form id="form"&amp;gt;
  &amp;lt;label for="username"&amp;gt;Username&amp;lt;/label&amp;gt;
  &amp;lt;input type="text" id="name" name="name" /&amp;gt;
  &amp;lt;button type="submit"&amp;gt;Enter Username&amp;lt;/button&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//step1: grab the element

const form = document.getElementById("form")

//step2:add .addEventListener() method+ pass in the arguments

form.addEventListener("submit", myCallbackFunction)

function myCallbackFunction(event){
   event.preventDefault()
   console.log("hello")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;I hope this serves as a beginner's guide to understanding the very basics of JavaScript events. JavaScript events can get more complex, especially the callback functions. I kept the callback functions very basic -console.log("hello")- due to this complexity. Callback functions deserve their own separate blogs or two! &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To learn more about Callback functions, visit:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://dev.to/smileyshivam/javascript-callback-functions-what-are-callbacks-in-js-and-how-to-use-them-123o"&gt;What are Callbacks in JS and How to Use Them&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.devhandbook.com/javascript/dom/event-target/"&gt;event.target JavaScript&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To learn more about event types, visit:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit_event"&gt;Form Submission&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseover_event"&gt;Mouseover Event&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/keydown_event"&gt;Keydown&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event"&gt;Change Event&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/keyup_event"&gt;Keyup&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.w3schools.com/js/js_htmldom_elements.asp"&gt;Finding HTML Elements&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.w3schools.com/jsref/met_document_queryselector.asp"&gt;querySelector Method&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.w3schools.com/js/js_events.asp"&gt;JavaScript Events&lt;/a&gt;&lt;/p&gt;

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