<?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: emi kojima</title>
    <description>The latest articles on DEV Community by emi kojima (@emiko).</description>
    <link>https://dev.to/emiko</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%2F158497%2F161d1627-18e6-4d41-8fad-6e794e3e9d79.jpg</url>
      <title>DEV Community: emi kojima</title>
      <link>https://dev.to/emiko</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/emiko"/>
    <language>en</language>
    <item>
      <title>My First CLI Project - 46 Books to Read by WOC</title>
      <dc:creator>emi kojima</dc:creator>
      <pubDate>Tue, 30 Apr 2019 21:09:58 +0000</pubDate>
      <link>https://dev.to/emiko/my-first-cli-project-46-books-to-read-by-woc-2jjh</link>
      <guid>https://dev.to/emiko/my-first-cli-project-46-books-to-read-by-woc-2jjh</guid>
      <description>&lt;h1&gt;
  
  
  My First CLI Project - 46 Books to Read by WOC in 2018
&lt;/h1&gt;

&lt;p&gt;At the onset of this project, students are asked to fill out a Project Ideation Form and submit it for review. When I was filling out the form, I had all these grand ideas and I wanted my app to do everything (list by books, list by author, list by the month the book is to be released, list by rating) but when I got feedback about my project idea it suggested I keep it simple (or &lt;a href="http://blog.flatironschool.com/5-steps-finishing-coding-project//"&gt;KISS&lt;/a&gt; - one of the design principles in programming). I am so glad that I listened to this feedback. &lt;/p&gt;

&lt;p&gt;I began my project by watching &lt;a href="https://www.youtube.com/watch?v=_lDExWIhYKI"&gt;Avi's great video&lt;/a&gt;, which walks you through how to create a gem using bundler (typing: bundle gem cli_app_name_of_project). I highly recommend watching this video before you begin your project. It allows you to see how Avi thinks through making a CLI Gem project and I used it as a frame of reference to begin my own. &lt;/p&gt;

&lt;p&gt;After I used gem bundler to set up the skeleton of my project and I set up my environment so that my pry, nokigiri, open uri would work. I started the project by using pen and paper and drawing out a flowchart of what I wanted in my user to encounter. &lt;br&gt;
It looked like this (but with a lot more cross-outs and squiggly lines):&lt;/p&gt;

&lt;p&gt;GREETING --&amp;gt; LIST OF BOOKS--&amp;gt; ASK FOR INPUT (what book do you want to see more of?) --&amp;gt; LIST MORE INFO --&amp;gt; GO BACK TO LIST/ SELECT ANOTHER BOOK--&amp;gt; EXIT. &lt;/p&gt;

&lt;p&gt;Then, I got to it! I began coding by writing the code that I wish I had (as Avi states multiple times in his CLI Gem Walkthrough video).  I started with coding my CLI class. I coded a greeting method, then made an outline method for list_books, and so on, so I had a frame to build out from. Here's the &lt;a href="https://www.youtube.com/watch?v=mJL5HqNb2xg&amp;amp;feature=youtu.be"&gt;walkthrough&lt;/a&gt; of my project! &lt;/p&gt;

&lt;p&gt;This project really pushed me to think through a project from inception to completion, something I hadn't done yet, and WOW, did I learn a whole lot!  &lt;/p&gt;

</description>
      <category>cli</category>
    </item>
    <item>
      <title>Integrating JQuery Front End to my RoR App (I HAVE THAT!)</title>
      <dc:creator>emi kojima</dc:creator>
      <pubDate>Tue, 30 Apr 2019 21:07:57 +0000</pubDate>
      <link>https://dev.to/emiko/integrating-jquery-front-end-to-my-ror-app-i-have-that-jnm</link>
      <guid>https://dev.to/emiko/integrating-jquery-front-end-to-my-ror-app-i-have-that-jnm</guid>
      <description>&lt;h1&gt;
  
  
  Integrating JQuery Front End to my RoR App (I HAVE THAT!)
&lt;/h1&gt;

&lt;h1&gt;
  
  
  The Beginning
&lt;/h1&gt;

&lt;p&gt;For this final project, I decided to turn my Rails app, &lt;a href="https://github.com/emikojima/i-have-that-v2"&gt;I HAVE THAT!&lt;/a&gt;, a community lending app, into an app with JQuery front end. To read more about the app and its features, click &lt;a href="http://emikojima.com/rails_final_project_i_have_that_v_2"&gt;here.&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;I struggled quite a bit with this project.  One of the biggest challenges I had with this assignment was a theoretical one. Why and on which page views should I integrate a JQuery front end and what reasoning and implications would my choices have on the user experience while fulfilling the &lt;a href="https://learn.co/tracks/full-stack-web-development-v6/rails-and-javascript/project-mode/rails-app-with-a-jquery-front-end#requirements"&gt;project requirements&lt;/a&gt;. The next mental hurdle I had was in thinking through how to adjust already exisiting code that wasn't written with a different front end integration in mind. &lt;/p&gt;

&lt;p&gt;I wrote down each question in a bullet point list:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What page views can be replaced with JQuery and AJAX to optimize user experience?&lt;/li&gt;
&lt;li&gt;Will my JQuery front end replace my rails front end completely? &lt;/li&gt;
&lt;li&gt;What implications will my choices have on my app as a whole and user experience?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With these questions in mind, I started by watching some video tutorials from the wonderful Tech Coaches of Flatiron School: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=XxzayZma5Ew"&gt;Submitting a Form with AJAX &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://instruction.learn.co/student/video_lectures#/146"&gt;JQuery Office Hours&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=G9K7-lDXXss"&gt;JQuery JS API Lab&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  The Process
&lt;/h1&gt;

&lt;p&gt;Watching these videos gave me a good idea of where and how to start building out my new Javascript (JS) front end. The first thing I decided to do was to map out which page views I wanted to dynamically load on to the DOM. &lt;/p&gt;

&lt;p&gt;The 'Community' page (&lt;code&gt;users#index&lt;/code&gt;) in my app lists all the Users of the app and their location. Each username was linked to their &lt;code&gt;users#show&lt;/code&gt; page and clicking on the link would redirect you to that page which displayed the items the user has listed for lending. I knew I wanted to hijack that click event and use AJAX to display the users' items and to add a &lt;code&gt;NEXT&lt;/code&gt; button dynamically so that each time the button was pressed, the DOM would update to display the next users' information and items. &lt;/p&gt;

&lt;p&gt;I made a list of to-dos to accomplish this task. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Generate Users &lt;a href="https://github.com/rails-api/active_model_serializers"&gt;Serializer &lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Generate &lt;code&gt;users.js&lt;/code&gt; (delete &lt;code&gt;users.coffee&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Make  to inject data in the application layout. 
&lt;/li&gt;
&lt;li&gt;Hijack click-event.&lt;/li&gt;
&lt;li&gt;Make AJAX request for data.
*Edit Users Controller &lt;code&gt;show&lt;/code&gt; action to render JSON&lt;/li&gt;
&lt;li&gt; Parse Data - maybe create Class and prototypes? &lt;/li&gt;
&lt;li&gt; Inject data into appropriate  along with a button for &lt;code&gt;NEXT&lt;/code&gt;


&lt;p&gt;I then made a list of to-dos to accomplish the task of hijacking my NEXT button and displaying the next user. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Figure out where to put the method or function of getting the next user (user.rb, users.js, users_controller?)&lt;/li&gt;
&lt;li&gt;Figure out the method or function of getting the next user&lt;/li&gt;
&lt;li&gt;Pass in the user :id to the NEXT button function&lt;/li&gt;
&lt;li&gt;Make an AJAX request for data&lt;/li&gt;
&lt;li&gt;Delete DOM&lt;/li&gt;
&lt;li&gt;Inject new data into DOM with &lt;code&gt;NEXT&lt;/code&gt; button. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once I accomplished this, I moved on to my next task and so on until I had created an app that was very close to a single page app (SPA) - other than my login and logout pages, my app was all JQuery and AJAX with no redirects. &lt;/p&gt;

&lt;h1&gt;
  
  
  The Takeaways
&lt;/h1&gt;

&lt;p&gt;Upon creating a very close cousin of the SPA (see below), I started to realize the limitations  of SPAs and read a few articles (like this great &lt;a href="https://medium.com/@pshrmn/demystifying-single-page-applications-3068d0555d46"&gt;one&lt;/a&gt;) that exposed the shortcomings of what I had coded. I also read this GREAT &lt;a href="http://voidcanvas.com/should-i-use-ajax-if-yes-when-where-and-why/"&gt;article&lt;/a&gt; about the best uses of AJAX (the when and where of AJAX use) and the disadvantages of using AJAX, an article I wish I had read before starting this project! &lt;/p&gt;

&lt;p&gt;The other big takeaway is that planning is &lt;strong&gt;invaluable&lt;/strong&gt;. For example, I had a list of items on my &lt;code&gt;items#index&lt;/code&gt; view that rendered each item by name, description, the user the item belongs to, and if the item was available to borrow. I had the item name in &lt;/p&gt;

&lt;h3&gt; and the rest of the information in &lt;/h3&gt;

&lt;p&gt;. So when I decided to have a search field, because I had other text with &lt;/p&gt;

&lt;h3&gt; in the DOM, I had a difficult time isolating the item name &lt;h3&gt; for my search function. And even though the fix was relatively painless - I had to put the &lt;h3&gt; inside a &lt;/h3&gt;

&lt;/h3&gt;
&lt;/h3&gt;

&lt;/li&gt;
&lt;li&gt; - it caused me to go back and code that in when I should have had a list item in a &lt;/li&gt;
&lt;li&gt; as basic protocol. You never know how your app will need to expand (what it will need to integrate, what functionality will need to be added) and it is always good practice to be neat, write clean code, and be smart about your HTML markup (another big takeaway - give &lt;code&gt;id&lt;/code&gt;s or &lt;code&gt;class&lt;/code&gt;es to your s)!

&lt;p&gt;The thing I love about coding is that there is such a wealth of information out there because there is such a generous community of coders and more you look, the more there is to learn. And while this can feel overwhelming at times, it also feels so generative and exciting. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/sYTClzsmiRc"&gt;Here&lt;/a&gt;'s the video walkthrough of the final product. &lt;/p&gt;

&lt;p&gt;Onward! &lt;/p&gt;

&lt;p&gt;Happy coding. &lt;/p&gt;


&lt;/li&gt;


&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>rails</category>
      <category>jquery</category>
      <category>ajax</category>
    </item>
    <item>
      <title>RAILS Final Project: I HAVE THAT! (v.2)</title>
      <dc:creator>emi kojima</dc:creator>
      <pubDate>Tue, 30 Apr 2019 21:06:37 +0000</pubDate>
      <link>https://dev.to/emiko/rails-final-project-i-have-that-v-2-1od4</link>
      <guid>https://dev.to/emiko/rails-final-project-i-have-that-v-2-1od4</guid>
      <description>&lt;p&gt;Draft&lt;/p&gt;

&lt;h1&gt;
  
  
  RAILS Final Project: I HAVE THAT! (v.2)
&lt;/h1&gt;

&lt;p&gt;For my Rails Final Project, I decided to convert my Sinatra Final Project app, I Have That!, into a Rails app and to add functionality that I wished that I had added when making this app using Sinatra.&lt;/p&gt;

&lt;p&gt;I Have That! is a community lending app that allows users to sign up and list items they want to lend out to their community and borrow things from the list of available items. My hopes in creating this app are to help foster a more sustainable world through sharing the resources we already have and to create community with people who are willing to do the same who happen to live in your area. The Sinatra version of this app did not have a location attribute for the user, so users could not filter the list of items by location. This was a function that I wanted to add to the Rails version of this app. Another functionality I wanted to add was an ability to have each item belong to a category and to list items by category instead of having one big list of items.&lt;/p&gt;

&lt;p&gt;In order to build out these functionalities, I first needed to decide on my models and their relationships. I did this with a pen and paper, making models with their attributes and deciding which models will have a belongs_to and which will have the has_many and has_many through relationship. After a few passes, I decided to have a User model, Item model, and Category model. The Item model would act as my join table and have the user_id and category_id attributes. The User model would have :username, :email, :password_digest (for has_secure_password), :state, and :city attributes. The Category model would have :name and :description as its attributes.&lt;/p&gt;

&lt;p&gt;After I had drawn up my models and felt sure of them, I began the task of building my rails app. In my terminal, I ran &lt;code&gt;rails new i-have-that-v2&lt;/code&gt; to get the file structure of my rails app. After that, I added my models and built my migrations by running &lt;code&gt;rails g migration name_of_my_migration&lt;/code&gt; to create my migration files. To fulfill the nested resources requirement, I decided that I would nest my Items in my Users. Which means that rather than having /items/new, I would have users/:id/items/new.&lt;/p&gt;

&lt;p&gt;My routes.rb looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rails.application.routes.draw do
  get 'auth/:provider/callback', to: 'sessions#create'
  get 'auth/failure', to: redirect('/')
  resources :categories
  resources :users, except:[:show]
  resources :users, only:[:show] do
  resources :items 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So, to make a new item (through my nested resource, I would have to pass in the user and the item to the form. It took me a while to figure out how to do this, but this is my Items Controller :new action code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ItemsController &amp;lt; ApplicationController
def new 
@user = find_user 
@item = Item.new(user_id: @user.id) 
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After setting the @user and @item, I passed both of these variables into the item/new/htmll.erb form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;%= form_for [@user, @item] do |f| %&amp;gt;
  &amp;lt;%= f.hidden_field :user_id %&amp;gt;
  &amp;lt;%= f.label :name %&amp;gt;
  &amp;lt;%= f.text_field :name %&amp;gt;&amp;lt;br&amp;gt;
  &amp;lt;%= f.label :description %&amp;gt;
  &amp;lt;%= f.text_field :description %&amp;gt;&amp;lt;br&amp;gt;
  &amp;lt;%= f.label :category_id, "Item Category" %&amp;gt;
  &amp;lt;%= f.collection_select(:category_id, Category.all, :id, :name, { :prompt =&amp;gt; 'Select a Category' }) %&amp;gt;&amp;lt;br&amp;gt;
  &amp;lt;%= f.label :available, "Check this box if this item is available for lending"%&amp;gt;
  &amp;lt;%= f.check_box :available %&amp;gt;&amp;lt;br&amp;gt;
  Didn't see a category for your item? Create one &amp;lt;%= link_to "here:", new_category_path %&amp;gt;&amp;lt;br&amp;gt;
  &amp;lt;%= f.submit %&amp;gt;
&amp;lt;% end %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this form, the &lt;code&gt;f.hidden_field :user_id&lt;/code&gt;  is a hidden field that is acting as the connector between the item and the user. When this form is submitted, this field will be in the params as item[:user_id] and its value will be assigned as the id of the @user in the creation of this item object.&lt;/p&gt;

&lt;p&gt;I built all of my app and made sure everything was working before I added OmniAuth and I am so glad that I did this. This is because the biggest challenge that I had (and I had a lot of challenges completing this project!) was to get my OmniAuth Google to work. I tried installing OmniAuth google from this tutorial . I ran into problems right away when my omniauth google gem would not bundle. After a lot of help from an amazing TC (JENN!), we (and by we, I mean Jenn) figured out that to get omniauth google to bundle, we had to have the latest version of it specified in the gemfile (&lt;code&gt;gem ‘omniauth-google-oauth2’, ‘~&amp;gt; 0.5.3’&lt;/code&gt;), unlike omniauth-facebook, which bundles just fine without specifying a version. The only other hiccup I ran into with OmniAuth Google was an error that kept popping up in my SessionsController.&lt;/p&gt;

&lt;p&gt;The error kept saying that I did not have a method ‘env’ in this line of code: @user = User.from_omniauth(env["omniauth.auth"]); after some googling, I found a quick fix. All I had to do was add ‘request’ before the env: @user = User.from_omniauth(request.env["omniauth.auth"]);.&lt;/p&gt;

&lt;p&gt;Once I got my OmniAuth working, I felt like I had just completed running a marathon and I took myself out for icecream and maybe I cried a little out of joy!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/58unnv2kqDM"&gt;Here’s&lt;/a&gt; the video walkthrough of my project!&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>codeschool</category>
    </item>
    <item>
      <title>JavaScript: What is `this`?!</title>
      <dc:creator>emi kojima</dc:creator>
      <pubDate>Tue, 30 Apr 2019 21:04:01 +0000</pubDate>
      <link>https://dev.to/emiko/javascript-what-is-this-27kl</link>
      <guid>https://dev.to/emiko/javascript-what-is-this-27kl</guid>
      <description>&lt;h1&gt;
  
  
  JavaScript: What is &lt;code&gt;this&lt;/code&gt;?!
&lt;/h1&gt;

&lt;p&gt;*As you read this blog post, it might be helpful to open up your Developer Console (Windows: Ctrl + Shift + J or Mac: Cmd + Option + J) and code along with me. *&lt;/p&gt;

&lt;h1&gt;
  
  
  Overview: &lt;code&gt;this&lt;/code&gt; 
&lt;/h1&gt;

&lt;p&gt;In Javascript, &lt;code&gt;this&lt;/code&gt; is all about context and &lt;em&gt;this&lt;/em&gt; can be very confusing.&lt;br&gt;
What I mean ☝ is that the value of &lt;code&gt;this&lt;/code&gt; is dependent on its execution context (ie: where &lt;code&gt;this&lt;/code&gt; is being called) - meaning that depending on where &lt;code&gt;this&lt;/code&gt; is being executed/called, what &lt;code&gt;this&lt;/code&gt; references will change. That's what can make &lt;code&gt;this&lt;/code&gt; so useful. In order to understand the value of &lt;code&gt;this&lt;/code&gt; we need to look to the call site (execution context).&lt;br&gt;
Try pasting this code in your console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(this);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You should see something like this after you hit the return key :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;which means that &lt;code&gt;this&lt;/code&gt; refers to the global window object. This makes sense becuase we called &lt;code&gt;this&lt;/code&gt; from the console (its execution context), which by default refers to the global window object.&lt;br&gt;
In a simple function call, the execution context for &lt;code&gt;this&lt;/code&gt; refers to the global (window) object by default.&lt;br&gt;
Here's an example of that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function hey () {
 console.log("Hey, I'm a simple function call");
 console.log(this === window); 
}
hey()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You shoud see this in your console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hey, I'm a simple function call
true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, in a simple funcion call, &lt;code&gt;this === window&lt;/code&gt;, which is why the console.log came back as &lt;code&gt;true&lt;/code&gt;.&lt;br&gt;
Now put this into your console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var animal = { 
 cat : function() {
 console.log(this)
 console.log(this.sound)
 },
 sound: "hrrrrmph"
}
animal.cat()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You should see something like this in your console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat: ƒ, sound: "hrrrrmph"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;followed by:&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hrrrrmph
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;in the above context, &lt;code&gt;this&lt;/code&gt; refers to the animal object and &lt;code&gt;this.sound&lt;/code&gt; becomes the animal object's sound value ("hrrrrmph").&lt;br&gt;
Ok, lets' take it up another level!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var animal = {
bear: function () {
 var sound = "ROARRRR"
 
 this.cat()
 },
cat : function() {
 console.log(this.sound)
 },
 sound: "hrrrrmph"
}
animal.bear()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What do you think will happen when we run the above code?&lt;br&gt;
Here, by calling &lt;code&gt;animal.bear()&lt;/code&gt; we call &lt;code&gt;this.cat()&lt;/code&gt; which logs in our console "hrrrrmph". That's becuase the execution context for &lt;em&gt;this&lt;/em&gt; in &lt;code&gt;this.cat()&lt;/code&gt; is animal. So calling &lt;code&gt;animal.bear()&lt;/code&gt; calls &lt;code&gt;animal.cat()&lt;/code&gt; which &lt;code&gt;console.logs(this.sound)&lt;/code&gt;.&lt;br&gt;
What if we moved the var sound = "ROARRRR" from the bear to cat?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var animal = {
bear: function () {
 this.cat()
 },
cat : function() {
 var sound = "ROARRRR"
 console.log(this.sound)
 },
 sound: "hrrrrmph"
}
animal.bear()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Did you guess that we would still get "hrrrrmph"?! &lt;br&gt;
I didn't! But like I said before,&lt;em&gt;THIS&lt;/em&gt; is complicated! Let's remember the other thing I said before. &lt;code&gt;this&lt;/code&gt; is all about context (execution context to be exact). Let's walk through it together.&lt;br&gt;
When animal.bear() is being called, the &lt;code&gt;this&lt;/code&gt; in &lt;code&gt;this.cat()&lt;/code&gt; is the animal object. So the &lt;code&gt;this&lt;/code&gt; in the &lt;code&gt;console.log(this.sound)&lt;/code&gt; is the animal object! This is where the call was executed, which means that we are console logging &lt;code&gt;animal.sound&lt;/code&gt;.&lt;br&gt;
What about this code below? Try to figure out the execution context of &lt;code&gt;this&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var animal = {
bear: function () {
 this.cat()
 },
cat : function() {
 var sound = "ROARRRR"
 console.log(this.sound)
 },
 sound: "hrrrrmph"
}
animal.cat()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;this&lt;/code&gt; still refers to the animal object! So, we still get &lt;code&gt;hrrrrmph&lt;/code&gt;&lt;br&gt;
OK, let's try to get deeper into &lt;code&gt;this&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var animal = 'Cat'
var forest = {
 animal: 'Bear',
 ocean: {
 animal: 'Pelican',
 animalName: function() {
 return this.animal
 }
 }
}
forest.ocean.animalName()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;OK, BEFORE we go on to see what the code above will return, let's talk about the scope of each animal.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cat&lt;/strong&gt; 🐱 belongs to the GLOBAL 🌐 window .&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bear&lt;/strong&gt; 🐻 belongs to the FOREST 🌲 scope. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pelican&lt;/strong&gt; 🐦 belongs to OCEAN 🌊 (forest.ocean)
Now back to the code. 
What do you think &lt;code&gt;this&lt;/code&gt; refers to in the context &lt;code&gt;forest.ocean.animalName()&lt;/code&gt;? It's the &lt;code&gt;forest.ocean&lt;/code&gt; object! So, this.animal (which is the same as forest.ocean.animal) returns "Pelican" because the Pelican belongs to the OCEAN.
GOOD.
What if we added this to the code to the console:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var animalName = forest.ocean.animalName
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;making &lt;code&gt;animalName&lt;/code&gt; a function that returns &lt;code&gt;this.animal&lt;/code&gt; Now, run this code in your console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;animalName()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;what do you think we would see returned? What is the execution context of &lt;code&gt;this&lt;/code&gt;? If you said global, you are right! Running the code &lt;code&gt;animalName()&lt;/code&gt; is equivalent to running this.animal, and since we are executing this code from the global window, it's just like calling window.animal, which is "Cat"!&lt;br&gt;
How would I be able to use the animalName() (which returns &lt;code&gt;this.animal&lt;/code&gt;) to return "Bear"?&lt;/p&gt;
&lt;h1&gt;
  
  
  📞 The call 
&lt;/h1&gt;

&lt;p&gt;That's where the .call() function comes in. &lt;a href="https://www.w3schools.com/js/js_function_call.asp"&gt;Here's &lt;/a&gt;what the W3Shool says about the .call(): &lt;br&gt;
&lt;em&gt;**With call(), an object can use a method belonging to another object. *&lt;/em&gt;*&lt;br&gt;
Sounds simple enough. Let's code it in! Since we know from ☝ that Bears belong to FOREST, let's pass forest in to the .call function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;animalName.call(forest)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And TA-DAAA, we get "Bear" &lt;br&gt;
What would we get if we executed this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;animalName.call(window)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That's right, we'd get "Cat".&lt;br&gt;
Now that we have a handle on &lt;code&gt;this&lt;/code&gt;, let's talk about when using &lt;code&gt;this&lt;/code&gt; won't work for us.&lt;/p&gt;
&lt;h1&gt;
  
  
  The ➡️ arrow function and &lt;code&gt;this&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;Arrow functions are a more syntactically concise way of writing function expressions and some argue are easier to read. You can read more about arrow functions &lt;a href="https://medium.freecodecamp.org/when-and-why-you-should-use-es6-arrow-functions-and-when-you-shouldnt-3d851d7f0b26"&gt;here&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions"&gt;here&lt;/a&gt;. But here's the briefest of overviews:&lt;br&gt;
A regular function is written like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function animalName () {
 return this.animal
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here's how the same function looks with an arrow function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arrowAnimalName =&amp;gt; () =&amp;gt; this.animal
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I like them both, but I'm a gemini, so that's to be expected.&lt;br&gt;
BUT why am I bringing up arrow functions in relation to &lt;code&gt;this&lt;/code&gt;, you ask.&lt;br&gt;
It's because in arrow functions &lt;code&gt;this&lt;/code&gt; is lexically bound - meaning that &lt;code&gt;this&lt;/code&gt; never changes 😱.&lt;br&gt;
&lt;a href="https://medium.freecodecamp.org/when-and-why-you-should-use-es6-arrow-functions-and-when-you-shouldnt-3d851d7f0b26"&gt;Cynthia Lee&lt;/a&gt; on FreeCodeCamp breaks it down like this:&lt;br&gt;
**&lt;em&gt;In classic function expressions, the this keyword is bound to different values based on the context in which it is called. With arrow functions however, this is lexically bound. It means that it uses this from the code that contains the arrow function.&lt;/em&gt;&lt;br&gt;
**&lt;br&gt;
Which means that we can't use .call(), or .apply() or .bind() with arrow functions because &lt;code&gt;this&lt;/code&gt; is lexically bound to the where the arrow function is executed.&lt;br&gt;
So if we run&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arrowAnimalName.call(forest) 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;we will get "Cat" because the &lt;code&gt;this&lt;/code&gt; is lexically bound to the Global window where we defined this arrow function.&lt;br&gt;
All of &lt;code&gt;this&lt;/code&gt; can get really confusing and maybe feel overwhelming. But you know what? We will always have &lt;code&gt;console.log(this)&lt;/code&gt; and &lt;code&gt;debugger&lt;/code&gt; to help us find out what &lt;code&gt;this&lt;/code&gt; really is, and THAT leaves me feeling pretty 🌈 .&lt;br&gt;
Thanks for reading!&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>React/Redux: Adding a search feature</title>
      <dc:creator>emi kojima</dc:creator>
      <pubDate>Sat, 20 Apr 2019 01:05:32 +0000</pubDate>
      <link>https://dev.to/emiko/how-to-create-a-search-bar-for-your-react-redux-app-1jmk</link>
      <guid>https://dev.to/emiko/how-to-create-a-search-bar-for-your-react-redux-app-1jmk</guid>
      <description>&lt;h2&gt;
  
  
  The App
&lt;/h2&gt;

&lt;p&gt;Ever since I can remember I've always loved reading. As a kid, I loved going to libraries and bookstores and seeing endless shelves of books. All those options of what I could choose to read felt exciting and expansive. Fast forward to me as an adult walking into a library or bookstore and seeing the same endless shelves of books; now libraries and bookstores are my &lt;a href="https://en.wikipedia.org/wiki/Bermuda_Triangle"&gt;Burmuda Triangle&lt;/a&gt;. As soon as I enter a library or book store, I can't remember any of the books I wanted to read or any of the book recs from my friends. My mind goes B L A N K.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/mPytjcsG3XS4o/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/mPytjcsG3XS4o/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Studies have shown that having too much choice actually prohibits us from making sound informed decisions (which you can learn more about by listening &lt;a href="https://www.wnycstudios.org/story/91640-choice"&gt;here&lt;/a&gt;).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This conundrum is what led me to the creation of my app BookMark (React/Redux, Rails backend, and using the NYT API), where users can peruse the New York Times Bestseller lists by genre, get some information about each book, read the book review, make a note about a book, and click on a book card to save a book to their 'reading list'. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/d7ndZcNhmGd0VaDdT9/giphy-downsized-large.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/d7ndZcNhmGd0VaDdT9/giphy-downsized-large.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Search Function
&lt;/h2&gt;

&lt;p&gt;After I created the basics of my app, I wanted to create a search function so that users could search through their saved books by entering a search term. &lt;/p&gt;

&lt;p&gt;The structure of React made this seamless because &lt;a href="https://reactjs.org/"&gt;React&lt;/a&gt; is component based, which means you can make a Search component and plug that component into where you want the search box to appear. &lt;/p&gt;

&lt;p&gt;I started by doing a &lt;strong&gt;thorough&lt;/strong&gt; internet search on how other people were accomplishing searches on their React Apps. After looking at a lot of examples and code, I decided on the best approach to take that made the most sense for my app.  &lt;/p&gt;

&lt;p&gt;I then started making a list of things I would need to do to make this functionality. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create Search component - create a simple search box using &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Place the Search component into the UserBooks component  (where you can see the list of user's books).&lt;/li&gt;
&lt;li&gt;Tie input of the search (the search term) from the Search component to the UserBooks component.&lt;/li&gt;
&lt;li&gt;Create the logic:  how do I filter the list of user books according to search term &amp;amp; write the function(s).&lt;/li&gt;
&lt;li&gt;Test to make sure the functionality works. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Step 1. I created a simple presentational Search component that takes in user input. I knew that each time a user typed something in the box, I wanted to trigger my search function. For this, I knew I had to create an onChange event handler so when the time came, I could send that input to my yet-to-be-written search function.&lt;/p&gt;

&lt;p&gt;Step 2. I then decided to place the component at the top of my UserBooks component so that the search box appears at the top of that component. &lt;/p&gt;

&lt;p&gt;Step 3. Since my Search component is the child component to UserBooks component, I had to somehow figure out a way to pass back the search term to the UserBooks component. I did this by creating a callback() function in the UserBooks component and passing that down to the Search component through props and then setting what I got back to my UserBooks local state. &lt;/p&gt;

&lt;p&gt;In my Search component, I used an &lt;code&gt;onChange&lt;/code&gt; event handler and used my callback inside the onChange like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Search = (props) =&amp;gt; {
   return (
     &amp;lt;&amp;gt;
     &amp;lt;form&amp;gt;
       &amp;lt;input
         placeholder="Search for..."
         onChange={(event)=&amp;gt;props.callback(event.target.value)}
       /&amp;gt;
     &amp;lt;/form&amp;gt;
     &amp;lt;/&amp;gt;
   )}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;and in my UserBooks component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; callback = (term ) =&amp;gt; {
    this.setState({
           term: term 
        })
  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, my search term is connected to my UserBooks component. &lt;/p&gt;

&lt;p&gt;Step 4.  I knew that in my UserBooks component, I was already displaying all of the user's books, so I need to figure out a way to filter out the books that did not match the user's search term so that only the books that matched remain showing. I also needed to figure out what I wanted the search term to search through (author name, title, book description, user's note about the book).&lt;/p&gt;

&lt;p&gt;I decided that I wanted the search to work on all of the book's attributes. I began by drawing out what I wanted to happen. &lt;/p&gt;

&lt;p&gt;User's books (with search box) are displayed =&amp;gt; search term is entered =&amp;gt; some kind of filter function =&amp;gt; user books that match the search term remain displayed.&lt;/p&gt;

&lt;p&gt;In my UserBooks component, I was already iterating through the list of user's books and passing each book to the UserBookCard component, so I knew this would be the place to put my search filter logic. I started by writing a filter function that would take the book and the search term as arguments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;filterIt = (book, searchTerm) =&amp;gt; {
return book.title.includes(searchTerm) || book.author.includes(searchTerm) || book.description.includes(searchTerm)
} 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The code above takes a book and checks to see if the book has the characters of your search term. The includes() method returns a boolean depending on whether or not the string contains that characters of your input. If the string contains the characters of your input, the method returns true. In my filterIt() function, I am saying, return the book if any of the book attributes includes the search term. *It's important to note that .includes() is case sensitive, so you will have to .toLowerCase() your input and book attributes. &lt;/p&gt;

&lt;p&gt;So now, I have a filterIt() function that returns the book that has the search term within it. NOW, WHAT. &lt;/p&gt;

&lt;p&gt;With the list of books I already have and the books that are getting returned by my filterIt() function, I can use the .filter() method which &lt;a href="https://www.w3schools.com/jsref/jsref_filter.asp"&gt;"creates an array filled with all array elements that pass a test (provided as a function)"&lt;/a&gt;, to get the user's books that contain the search term and then iterate through the filtered list and pass the book on to my UserBookCard component like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const booksList = books.filter(this.filterIt(this.state.term)).map(book =&amp;gt; {return (&amp;lt;UserBookCard
          key={book.id}
          book={book}
          deleteUserBook={this.props.deleteUserBook}
          userId={this.props.user}
          addBookNote={this.props.addBookNote} /&amp;gt;)
        }
    )
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Step 5. Testing. Debugging. Testing. Debugging. Testing. &lt;/p&gt;

&lt;h2&gt;
  
  
  The Result
&lt;/h2&gt;

&lt;p&gt;And this is what it looks like in action! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/NsBjrSdM1OexBac4la/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/NsBjrSdM1OexBac4la/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading and happy coding! &lt;/p&gt;

</description>
      <category>react</category>
      <category>redux</category>
      <category>rails</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
