<?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: Joan Indiana Lyness</title>
    <description>The latest articles on DEV Community by Joan Indiana Lyness (@joanrig).</description>
    <link>https://dev.to/joanrig</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%2F219434%2Ff70e7fb3-2ce7-4f07-8960-9b794fe46a46.jpeg</url>
      <title>DEV Community: Joan Indiana Lyness</title>
      <link>https://dev.to/joanrig</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/joanrig"/>
    <language>en</language>
    <item>
      <title>How I Deployed my React-on-Rails app to Heroku</title>
      <dc:creator>Joan Indiana Lyness</dc:creator>
      <pubDate>Mon, 19 Aug 2019 19:16:55 +0000</pubDate>
      <link>https://dev.to/joanrig/how-i-deployed-my-react-on-rails-app-to-heroku-3758</link>
      <guid>https://dev.to/joanrig/how-i-deployed-my-react-on-rails-app-to-heroku-3758</guid>
      <description>&lt;p&gt;A space odyssey ….&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mt6yDsgo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AUInVHxDBimowrdiuJSzmaA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mt6yDsgo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AUInVHxDBimowrdiuJSzmaA.png" alt="whos-who-congress-app"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I just graduated from the Flatiron School, and I’m pretty happy with my final project, a Congress Who’s Who app — an educational game. For the longest time, it was stuck on my computer. People who saw it wanted to know how they could use it. Could they download it? (Why yes, if you want to follow these 20 steps to create a Git account and fork and clone two repos, bundle them and run them on the right servers ….)&lt;/p&gt;

&lt;p&gt;What I wanted to say was YES, here’s the link! And now I have one. My app lives here: &lt;a href="https://congress-front-end.herokuapp.com/"&gt;https://congress-front-end.herokuapp.com/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you’re having trouble deploying your React-on-Rails app to Heroku, especially if yours makes calls to an external API (application programming interface), read on, and learn from my mistakes!&lt;/p&gt;

&lt;p&gt;Note: there are a few blog posts on how to do this, plus the Heroku documentation itself. Some parts of both worked for me, some didn’t. I’m this combination of steps, in this order, works for you!&lt;/p&gt;

&lt;h3&gt;
  
  
  Assumptions (pre-requisites!)
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://medium.com/media/255d34d133519651a4668449fec9e1b1/href"&gt;&lt;/a&gt;&lt;a href="https://medium.com/media/255d34d133519651a4668449fec9e1b1/href"&gt;https://medium.com/media/255d34d133519651a4668449fec9e1b1/href&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This blog post assumes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You have a working Rails API back-end.&lt;/li&gt;
&lt;li&gt;Your Rails API back-end database is Postgres.&lt;/li&gt;
&lt;li&gt;You have a working React front-end.&lt;/li&gt;
&lt;li&gt;Both are tracked in separate Github repositories.&lt;/li&gt;
&lt;li&gt;You have a Heroku account. (If not, follow&lt;a href="https://signup.heroku.com/"&gt;simple sign-up directions here&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;You have installed the Heroku CLI (if not &lt;a href="https://devcenter.heroku.com/articles/heroku-cli#getting-started"&gt;follow these steps&lt;/a&gt;)&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  PART ONE: Deploy your Rails API back-end
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Step 1. Create a separate branch on Github for this effort.
&lt;/h4&gt;

&lt;p&gt;My back-end was named congress-api. I created a new branch called deploy_to_heroku. After creation, github automatically moved me to my new branch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ♥ git co -b deploy\_to\_heroku

[**10:03:55**] (deploy\_to\_heroku) congress\_api

// ♥
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, if everything goes wrong, you can at least revert back to your previous branch.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2. If this is your 2nd, 5th or … 20th attempt … clean up your previous mess.
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;(skip this step if this is your first effort to deploy to Heroku)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/media/59b26ad2e0dd7cb3096055d9f96d810d/href"&gt;&lt;/a&gt;&lt;a href="https://medium.com/media/59b26ad2e0dd7cb3096055d9f96d810d/href"&gt;https://medium.com/media/59b26ad2e0dd7cb3096055d9f96d810d/href&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I tried and tried many times before I succeeded! During those previous efforts, I created and re-created the app on Heroku. Those previous efforts were done incorrectly and derailed my subsequent efforts. If you are in the same boat, go to your Heroku dashboard, and get rid of any previous attempts to create this particular app on Heroku. In my case, I had multiple copies of my app under different names.&lt;/p&gt;

&lt;p&gt;AND … if along the way, you followed advice like I did to run a ‘yarn build’ of your project, you will need to delete your yarn.lock file from the root of your file tree. Heroku does not use yarn.lock.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3. Add a Procfile
&lt;/h3&gt;

&lt;p&gt;What’s a procfile? It tells Heroku which commands to run to start your app!&lt;/p&gt;

&lt;p&gt;Call it “Procfile” &lt;em&gt;with no extension and add it to your root directory.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I followed &lt;a href="https://devcenter.heroku.com/articles/preparing-a-codebase-for-heroku-deployment#3-add-a-procfile"&gt;Heroku’s documentation&lt;/a&gt; to the letter and made mine look exactly like their sample code for Rails:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;web: bundle exec rails server -p $PORT
worker: bundle exec rake jobs:wor
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4. Configure PUMA server
&lt;/h3&gt;

&lt;p&gt;I followed &lt;a href="https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server"&gt;these steps&lt;/a&gt; from the Heroku documentation, but I am not sure they are enabled, since I do not call Puma in my Procfile (with a line like this `web: bundle exec puma -C config/puma.rb)&lt;/p&gt;

&lt;p&gt;I’ll update this post when I have time to experiment with removing my Puma.rb file to see what happens.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 6. Create a Heroku remote.
&lt;/h3&gt;

&lt;p&gt;In terminal, navigate to your app’s root directory and &lt;a href="https://devcenter.heroku.com/articles/git#creating-a-heroku-remote"&gt;follow these instructions&lt;/a&gt;from Heroku documentation.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 7. Add Heroku Postgres to your App&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://devcenter.heroku.com/articles/heroku-postgresql#provisioning-heroku-postgres"&gt;Follow these steps&lt;/a&gt; from Heroku documentation&lt;/p&gt;

&lt;p&gt;Then, in terminal run Heroku config. This will bring up the url for your new Heroku Postgres add-on. (it’s very long. I’m just showing the first part of mine below).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ePTSRTGA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/694/1%2AcJi8a4YBCACFtwUQJpq_4g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ePTSRTGA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/694/1%2AcJi8a4YBCACFtwUQJpq_4g.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Important!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Grab that database url (“postgres:// etc”) and in your database.yml file, set DATABASE_URL = &amp;lt;”postgres:// etc”&amp;gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 6. Deploy!
&lt;/h3&gt;

&lt;p&gt;From the Heroku documentation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X1QIE2Hv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AndLgejXpSQRjliTU01bt3g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X1QIE2Hv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AndLgejXpSQRjliTU01bt3g.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then navigate to the url for your new app. (If you can’t find it, go to your Heroku dashboard, click on the name of your new app, then click on “Open App” at the right. )&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7Cgg92QK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AxR50fymEeG_zWO2O8pLC7A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7Cgg92QK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AxR50fymEeG_zWO2O8pLC7A.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Grab that url, you will need it for Part Two!&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 7. See ….. nothing?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://medium.com/media/2704ed6a06c6106b65eb5a0353d5b4c8/href"&gt;&lt;/a&gt;&lt;a href="https://medium.com/media/2704ed6a06c6106b65eb5a0353d5b4c8/href"&gt;https://medium.com/media/2704ed6a06c6106b65eb5a0353d5b4c8/href&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Unless you found and followed steps to &lt;a href="https://devcenter.heroku.com/articles/heroku-postgres-import-export"&gt;import your Postgres database&lt;/a&gt; as is, you deployed your app with an empty database like I did. So, after my ‘successful’ deploy, I was expecting to see the same thing I saw on my back-end page during development: lots of JSON data. Like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cXBqju-Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Ay5jnKJq5blAgiWFyMUHp6A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cXBqju-Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Ay5jnKJq5blAgiWFyMUHp6A.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But instead, I saw:&lt;/p&gt;

&lt;p&gt;[]&lt;/p&gt;

&lt;p&gt;Why? Because I deployed an app with an empty database.&lt;/p&gt;

&lt;p&gt;If you aren’t making calls to an external database, you can fix this by running:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
heroku rake db:create&lt;br&gt;
heroku rake db:migrate&lt;br&gt;
heroku rake db:seed&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;(Heroku does not support heroku rake:db reset.) &lt;a href="https://devcenter.heroku.com/articles/rake"&gt;See documentation.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;BUT, if your app makes calls to an external API, this won’t work! Why not? Read on …&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 8. If your app makes calls to an external API you must ….
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;…use Heroku to configure envrionment variables.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;OK, so this is the step that just about killed me. After doing lots of Googling and reading lots of documentation and Stack Overflow posts, I managed to get to the point where all I had to do was seed my database. But when I ran Heroku rake db:seed, I got a fail.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GLChW-us--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Ay5iWDd4-BozORbGih-lztg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GLChW-us--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Ay5iWDd4-BozORbGih-lztg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So the big hint was, check out /app/db/seeds.rb:9&lt;/p&gt;

&lt;p&gt;What was there? The results of my API call, which by the way, worked perfectly in development.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sq6Nvv2x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AOaCES2_AMDdzEXLaZIHnmQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sq6Nvv2x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AOaCES2_AMDdzEXLaZIHnmQ.png" alt=""&gt;&lt;/a&gt;worked in development, why not in Heroku?&lt;/p&gt;

&lt;p&gt;I tried sticking a pry into my production code, and could not hit it. Then, much Googling later, I discovered that Heroku does NOT recognize ENV variables, at least not in API calls. If you want to use your API key in an app you are deploying to Heroku, you must configure those variables in the Heroku CLI.&lt;/p&gt;

&lt;p&gt;These, my friends, &lt;a href="https://devcenter.heroku.com/articles/config-vars#using-the-heroku-cli"&gt;are the magic steps.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For me, they looked like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--J-5N3SaH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/836/1%2AVEBloW6mPs0cZC0hN6IX6Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--J-5N3SaH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/836/1%2AVEBloW6mPs0cZC0hN6IX6Q.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;etc.&lt;/p&gt;

&lt;p&gt;After I got that straight, I ran heroku rake db:seed&lt;/p&gt;

&lt;p&gt;and was THRILLED to see my database actually loading.&lt;/p&gt;

&lt;p&gt;If you’re following along, take a break, do a little dance, and take heart! You have just finished the hardest part. Part Two is way way easier, I promise.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/media/d594902b7f42e974ad45508f599e87c5/href"&gt;&lt;/a&gt;&lt;a href="https://medium.com/media/d594902b7f42e974ad45508f599e87c5/href"&gt;https://medium.com/media/d594902b7f42e974ad45508f599e87c5/href&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  PART TWO: Deploy your React Front End
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;(way easier than part one!)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I followed&lt;/em&gt; &lt;a href="https://dev.to/smithmanny/deploy-your-react-app-to-heroku-2b6f"&gt;&lt;em&gt;these directions from Shakhor Smith&lt;/em&gt;&lt;/a&gt; &lt;em&gt;(thx Shakhor!). The following is a distillation of his directions plus a few of my own observations and screenshots.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;These instructions assume that you created your React front-end with create-react-app. Before you continue, go into terminal and navigate to the root directory of your React front end.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 1. Set up your front end to call the new url for your back end.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;During development, I ran my rails back end on &lt;a href="http://localhost/3000"&gt;http://localhost/3000&lt;/a&gt;. So all my fetches to my back end used &lt;a href="http://localhost/3000"&gt;http://localhost/3000&lt;/a&gt; as a base URL. But with my back end actually up and running on Rails, I had to update those urls — um, all 10 of them.&lt;/p&gt;

&lt;p&gt;When I set up those fetch calls, I typed the urls out like so:&lt;/p&gt;

&lt;p&gt;fetch(&lt;a href="http://localhost/3000/search/senators"&gt;http://localhost/3000/search/senators&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;That’s fine for one or maybe two fetches, but I had more than 10. So to prepare for my Heroku deploy, I did what I should have done in the first place. I abstracted the base url into its own constant:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
const API_BASE_URL="&lt;a href="http://localhost/3000"&gt;http://localhost/3000&lt;/a&gt;"&lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;fetch(API_BASE_URL+’/search/senators’)&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I did that for 16 fetches in two files. (command D comes in handy! — select the first instance of a string, hit command D repeatedly to select subsequent instances, then type the replacement string, and voila!)&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2. Figure out which versions of node and npm you are running.
&lt;/h3&gt;

&lt;p&gt;Run npm -v&lt;/p&gt;

&lt;p&gt;and node-v&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eu5D_i8M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/732/1%2AqxID1JWBpk9Tv28TWjgqlw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eu5D_i8M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/732/1%2AqxID1JWBpk9Tv28TWjgqlw.png" alt=""&gt;&lt;/a&gt;I’m running npm version 6.9.0 and node version 12.4.0&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3. Add an engines object to package.json
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pHDHS4yo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/486/1%2AnsLvlE5blfralsAm1jkWPA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pHDHS4yo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/486/1%2AnsLvlE5blfralsAm1jkWPA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;following Shakhor’s lead, I put mine right above dependencies (I don’t know if order matters, let me know if you know it does!)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9aL8UPK5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/830/1%2A86NQRMTvAbWcR2WQTtKtgg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9aL8UPK5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/830/1%2A86NQRMTvAbWcR2WQTtKtgg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4. Create a remote Heroku repository the easy way.
&lt;/h3&gt;

&lt;p&gt;Open up your Heroku dashboard in your browser. Click ‘New’ and select “Create new App”&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oZbS74ny--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AYYTQofKRZuknfLO1Mq3tfA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oZbS74ny--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AYYTQofKRZuknfLO1Mq3tfA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You’ll see a dashboard for the app you just created. Go to the deploy tab and follow directions to connect your app to Github, enable automatic deploys, and choose the branch you are deploying (in my case it was deploy_to_github). Don’t forget to click the deploy branch when you are done!&lt;/p&gt;

&lt;p&gt;If you get fail message the deploy tab, plus a log of what went wrong. The messages are very clear, and include clear instructions on how to fix what went wrong.&lt;/p&gt;

&lt;p&gt;My deploy failed because of conflicting yarn.lock and package.json file; the error message clearly told me to delete yarn.lock. Then you have to save that change and re-run git push heroku master to push that change to Heroku.&lt;/p&gt;

&lt;p&gt;After fixing two such errors, success! I finally got my React and Rails App running on Heroku!!!&lt;/p&gt;

&lt;p&gt;Here’s hoping this reduces your deployment time to less than one hour!!!&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactonrails</category>
      <category>railsapi</category>
    </item>
    <item>
      <title>Ruby code challenge: airplane movies</title>
      <dc:creator>Joan Indiana Lyness</dc:creator>
      <pubDate>Mon, 05 Aug 2019 04:22:32 +0000</pubDate>
      <link>https://dev.to/joanrig/ruby-code-challenge-airplane-movies-4f61</link>
      <guid>https://dev.to/joanrig/ruby-code-challenge-airplane-movies-4f61</guid>
      <description>&lt;p&gt;My introduction to data’s Big O&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BqXQskxP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AAeoGNlEK6Roj4C5UnyUsSw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BqXQskxP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AAeoGNlEK6Roj4C5UnyUsSw.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s say you’re taking a long-ish flight. You’ve got a list of in-flight movies and how long they are. You decide you want to watch exactly two movies, and you want to know if any two on the list have a total run time that is exactly equal to your flight time. (We’re assuming of course, that the flight duration is set in stone haha).&lt;/p&gt;

&lt;p&gt;Easy enough if you’re looking at a list of five, or maybe even 10 movies. But what if you don’t know how many movies are in your list. You might have access a huge database of movies. Your list could include 10,000 movies!&lt;/p&gt;

&lt;p&gt;The challenge: Write a Ruby method that returns true if any two movies add up to flight time, and false if not.&lt;/p&gt;

&lt;p&gt;BUT — there are two extra conditions.&lt;/p&gt;

&lt;p&gt;First, you don’t want to watch the same movie twice. So if your flight time is 100 minutes, and your list of movies has only one movie that is 50 minutes long, you want your method to return false. But if you have two movies in your list, and both are 50 minutes long, you want your method to return true.&lt;/p&gt;

&lt;p&gt;Second — and this is the hard part, you need to pay attention to run time. As in you want your method’s Big O notation to O(n) or better. …&lt;/p&gt;

&lt;h3&gt;
  
  
  What’s the Big O ?
&lt;/h3&gt;

&lt;p&gt;Big O notation is a way to calculate how the size of your data impacts how fast your code runs, or how much RAM it uses. Bottom line: it is way faster to find things in hashes than arrays, since you can call them by key instead of looping through each element in order; and nested loops are evil!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.honeybadger.io/blog/a-rubyist-s-guide-to-big-o-notation/"&gt;The Rubyist’s Guide to Big O Notation&lt;/a&gt; is easy to read and includes this helpful chart of smiley faces:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rp1EEwsv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2A6sRkME2d1RfAdStXWXepjA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rp1EEwsv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2A6sRkME2d1RfAdStXWXepjA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So why are nested loops evil? Let’s say you have code like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B60gc68G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/602/1%2APdb5fFC53iyRjVJ2pFzn5w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B60gc68G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/602/1%2APdb5fFC53iyRjVJ2pFzn5w.png" alt=""&gt;&lt;/a&gt;the evil nested loop&lt;/p&gt;

&lt;p&gt;If your school has 10 students and your city has 10 residents, you have to make 10 x 10 comparisons to run this code, or 10². That’s not bad, but what if your school and your town each have 1,000 residents? now you're up to 1,000² comparisons.&lt;/p&gt;

&lt;p&gt;If you think, like I did … well, we can cut down on the length of the operation by filtering out certain values first, so at least we’re comparing smaller arrays … that doesn’t work either. Big O notation is conceptual and assumes the worst-case scenario. Any array of any size has &lt;em&gt;n&lt;/em&gt; elements; if you do two nested loops through arrays, you are always doing n² operations. It doesn’t matter if n is &lt;em&gt;actually&lt;/em&gt; just 1. What matters is how the value of n² changes with the size of the dataset.&lt;/p&gt;

&lt;p&gt;So how do we get out of this mess?&lt;/p&gt;

&lt;h3&gt;
  
  
  Hashes to the rescue!
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YKo8l_NV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/599/1%2AdOqdhX5-JPS0StGAtgG-5w.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YKo8l_NV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/599/1%2AdOqdhX5-JPS0StGAtgG-5w.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hashes are way faster to search because you call values by key, instead of iterating through a whole array to find a value (What is happening under the hood? Do posts really magically return values without looking at more than one key? &lt;a href="https://www.bigocheatsheet.com/"&gt;Here’s a post that gets into the details&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;I was curious to see how this worked out for my particular challenge.&lt;/p&gt;

&lt;p&gt;My first effort involved the dreaded, nested loop:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--u8yVL9O---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Ad9yI1QDQiZ9ut1r1G7IJCw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u8yVL9O---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Ad9yI1QDQiZ9ut1r1G7IJCw.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I was curious about the run time with different datasets. I found this great explanation of &lt;a href="https://skorks.com/2010/03/timing-ruby-code-it-is-easy-with-benchmark/"&gt;how to use Benchmark.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Inside my code editor, I added a few lines, including one to randomly generate an array of 10,000 movies, and an impossible flight time (so the method would be forced to look at every single movies element before finishing).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KrzrEqkp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Ah_9EVFdPPM1CbGIkDMmBfg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KrzrEqkp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Ah_9EVFdPPM1CbGIkDMmBfg.png" alt=""&gt;&lt;/a&gt;inside file: test.rb&lt;/p&gt;

&lt;p&gt;Then I typed ‘ruby test.rb’ to run the code and got this run time: about 4.3 seconds, or 4327.5660001673 milliseconds.&lt;/p&gt;

&lt;h3&gt;
  
  
  With Hashes
&lt;/h3&gt;

&lt;p&gt;Next, I tried a solution with hashes:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ssp9Prgc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2A7Ftlg1gWJJXcxg6IerBdGA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ssp9Prgc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2A7Ftlg1gWJJXcxg6IerBdGA.png" alt=""&gt;&lt;/a&gt;2 * O(n), one for each (not-nested) loop&lt;/p&gt;

&lt;p&gt;Run time: 3.5880000796169 milliseconds — that is 1,200 x faster!!!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---zb-WrT0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ABHQBW7oM1wTYxZVPaB35Hw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---zb-WrT0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ABHQBW7oM1wTYxZVPaB35Hw.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  No hash, no nested loops
&lt;/h3&gt;

&lt;p&gt;Next, a solution with no hash, one loop and one operation to pull out common elements from two loops:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--li6b9y3h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ABeVdbtKsnc44HdhOqlMfCQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--li6b9y3h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ABeVdbtKsnc44HdhOqlMfCQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Interestingly —&lt;em&gt; this method was actually faster than the one with the hash&lt;/em&gt;:.78899995423853 milliseconds.&lt;/p&gt;

&lt;p&gt;Finally I tried a super elegant solution from a classmate. This one takes advantage of &lt;a href="https://ruby-doc.org/core-2.4.1/Array.html"&gt;Ruby’s built-in combination method&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--l50e0Io6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Al8kRBNs9DgQuLSH_n-4u0A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--l50e0Io6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Al8kRBNs9DgQuLSH_n-4u0A.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is short, sweet and wins at code golf. But it is apparently not very efficient under the hood. My computer stalled when I ran this method with an array of 100,000 elements. Here is the time with 10,000 elements: run time: 11.73972999989428 seconds. That’s 3,200 times longer than the hash method … and 15,000 times longer than the comparison method. Yikes!&lt;/p&gt;

&lt;p&gt;Here is the chart: (blank spaces mean the method took so long to run that I had to abort after several minutes of inactivty).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jEIRj_3C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Ae4pWV4t-xQdRSn88QF3U5Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jEIRj_3C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Ae4pWV4t-xQdRSn88QF3U5Q.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The winners for handling data in large arrays (10,000+ elements):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;First place:&lt;/strong&gt; Ruby’s &amp;amp;&amp;amp; method for comparing arrays.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Second place:&lt;/strong&gt;  hash&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Third place:&lt;/strong&gt; nested loop&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fourth place:&lt;/strong&gt; Ruby’s built-in combination method.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Moral of the story: if you want your code to run fast, avoid nested loops!&lt;/p&gt;

</description>
      <category>programming</category>
    </item>
    <item>
      <title>Rails API Project Part 3: Adding a front end with React &amp; Redux</title>
      <dc:creator>Joan Indiana Lyness</dc:creator>
      <pubDate>Mon, 22 Jul 2019 19:18:40 +0000</pubDate>
      <link>https://dev.to/joanrig/rails-api-project-part-3-adding-a-front-end-with-react-redux-2i43</link>
      <guid>https://dev.to/joanrig/rails-api-project-part-3-adding-a-front-end-with-react-redux-2i43</guid>
      <description>&lt;p&gt;and how to call the custom sorting methods we created on the back end.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AOvnN9oKxsVwO9HohamyhDQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AOvnN9oKxsVwO9HohamyhDQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;a href="https://dev.to/joanrig/rails-api-project-part-2-the-art-of-seeding-2dej-temp-slug-9969270"&gt;previous post&lt;/a&gt; we covered how to finish setting up our Who’s Who Congress Rails API. Now we need a front end where we display all that awesome data in fun ways.&lt;/p&gt;

&lt;p&gt;I built my front end with React and Redux.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is React?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F200%2F1%2AkcTJ5BkjLWRM2IIb3Uye9A.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F200%2F1%2AkcTJ5BkjLWRM2IIb3Uye9A.jpeg"&gt;&lt;/a&gt;React logo&lt;/p&gt;

&lt;p&gt;It’s a Javascript library that was created by Facebook. We use it to create modular user interfaces, where each thing we present on the page is a component. Each component has its own code, which makes it easy for teams to work on the code. Then based on what view you want to render, you can choose which components to display.&lt;/p&gt;

&lt;p&gt;In my app, I have a SenateContainer component that has a child component called SenatorsList. SenatorsList in turn, has its own child component called SenatorCard. By the time I’m done with my project, SenatorCard will have its own components, like SocialMedia Links, etc.&lt;/p&gt;

&lt;p&gt;Then, when I build my senate page, I simply load these building blocks into one file. Take a look at the picture at the top of this post. It includes many components: a Home Page component that includes a NavBar component (the four large buttons across the top of the screen); and the SenatorsList component which includes the sorting buttons, search bar and senators card.&lt;/p&gt;

&lt;p&gt;Components can have state and methods, which they can pass down to their child components. React has methods for passing these things (props, short for properties) down to child components, but it can get cumbersome when your components have components that have components.&lt;/p&gt;

&lt;h3&gt;
  
  
  Enter: Redux
&lt;/h3&gt;

&lt;p&gt;Redux is a tool for managing state. A list component’s state, for example, is which items are currently on the list. With Redux, workflow goes like this:&lt;/p&gt;

&lt;p&gt;You have an object with an initial state. Let’s say you have a to-do list with no items in it. So List’s initial state is an empty array: [].&lt;/p&gt;

&lt;p&gt;Your user clicks a button to add a to-do item.&lt;/p&gt;

&lt;p&gt;When they submit the new item, the List component calls an action from a list of actions that live in a separate file.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;reducer&lt;/strong&gt; sets a new state for the component based on the action. So if the action was add a to-do item, the reducer uses a spread operator to makes a copy of the current state (cardinal rule: never ever amend state, just make a copy and amend the copy) and add the newToDo.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function ListReducer(state = [], action) {
 switch(action.type) {
 case 'ADD\_TODO':
 // adds todo`
 return { ...state, newTodo }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, List’s state has been updated in the store. If this confuses you, you are not alone! Here is an excellent super-short primer called &lt;a href="https://dev.to/hemanth/explain-redux-like-im-five"&gt;“Explain Redux Like I’m Five.”&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  So about that API call …
&lt;/h3&gt;

&lt;p&gt;To set up our app to update with data from our API, we need to set an initial state for the component will hold our data. Let’s focus on our SenatorsList component, which I set up with an initial state of an empty array of senators.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2A8xYVVHAgA73tP0Fv6AXP2w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2A8xYVVHAgA73tP0Fv6AXP2w.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And my view looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2A-5sD598isd9f-CJuZSCdWA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2A-5sD598isd9f-CJuZSCdWA.png"&gt;&lt;/a&gt;notice: no senators listed&lt;/p&gt;

&lt;p&gt;When my user clicks the red ‘Senate’ button I want the page to load all 100 senators (which I formatted as flashcards, using React ui semantic).&lt;/p&gt;

&lt;p&gt;I set up an event listener on the button so that when it is clicked, it will call an action from my Actions/senate file:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AAB4-KDiNNbcRDg5CJXRdoQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AAB4-KDiNNbcRDg5CJXRdoQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The action, fetchSenate() uses, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch" rel="noopener noreferrer"&gt;the fetch method&lt;/a&gt; to call the API we set up earlier; put sthe response in JSON, and then tell the Reducer, hey Mr. Reducer, pls go ahead and dispatch the action with the type “SET_SENATE”, and feed it the data I just stored in the variable ‘senate’.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2Aq1bjppIs7S1WG__3e4Wrzw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2Aq1bjppIs7S1WG__3e4Wrzw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The reducer then updates the app’s state.senate with an array of 100 senators! This updated state lives in our store.&lt;/p&gt;

&lt;h3&gt;
  
  
  Back at the ranch …
&lt;/h3&gt;

&lt;p&gt;Meanwhile, SenatorsList has no idea what’s going on. It sent off a request for senators, and 100 senators came back, but they came back to the store. To give SenatorsList access to its own state, we have to connect it to the store. We use Redux’s built-in ‘connect’ tool to do this:&lt;/p&gt;

&lt;p&gt;At the bottom of our SenatorsList component:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2A6reVr0sqWur3puDbpp9ruw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2A6reVr0sqWur3puDbpp9ruw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Without Redux, we would normally have one line here: export default SenatorsList. But we’ve added more information here — before we export SenatorsList, we’re connecting it to the store, where we want it to grab its own state and its actions (the ones living in the actions/senate.js file).&lt;/p&gt;

&lt;p&gt;The export line starts out 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;export default SenatorsList connect()(SenatorsList)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Connect takes two arguments. The first is mapStateToProps, which lets the component (once it’s connected to the store), call its state as a prop (shot for property). Here’s what mapStateToProps looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mapStateToProps = state =\&amp;gt; ({senate: state.senate})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code says — go to the store, grab my state (state.senate) and from now on, let me access my state as this.props.senate.&lt;/p&gt;

&lt;p&gt;But SenatorsList wants more than just its state. It also wants access to the actions in the store that change its state. Back to our connection:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2A6reVr0sqWur3puDbpp9ruw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2A6reVr0sqWur3puDbpp9ruw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The second argument connect takes is mapDispatchToProps. You can either &lt;a href="https://react-redux.js.org/using-react-redux/connect-mapdispatch" rel="noopener noreferrer"&gt;write out the full method&lt;/a&gt; or add a list of the actions as a comma-separated list surrounded by curly braces. This says: while you’re at the store, grab these actions, and map those to props too, so I can call this.props.fetchSenate, etc.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AB75ZHwlbnRygChj68H60HQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AB75ZHwlbnRygChj68H60HQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Think of the connect, mapStateTo Props and mapDispatch to props as a way for your humble component to go to the store to get superpowers!&lt;/p&gt;

&lt;p&gt;Now that SenatorsList knows its own state, it uses each senator’s information to render a SenatorCard, and then displays them in a grid.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AT34ycbNNTRf04BeBX_MsIw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AT34ycbNNTRf04BeBX_MsIw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Let’s pass our superpowers down to some other buttons.
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AvFacc_DqdV1Qf6xqAmgoOg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AvFacc_DqdV1Qf6xqAmgoOg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My senate view includes five sorting buttons. Each one is tied to a custom sorting method that we defined in our Rails API, and to a search term that we can feed to our API to get back the list of sorted senators.&lt;/p&gt;

&lt;p&gt;These buttons look a lot like the Senate button but they work a bit differently.&lt;/p&gt;

&lt;p&gt;The Senate button doesn’t directly fire the fetch action. Instead, it routes the viewer to the Senate route, which mounts the SenateContainer component, which mounts the SenatorsList component, … which automatically fires the fetch action when it loads. In other words, when you click Senate, I want you to automatically see all 100 senators.&lt;/p&gt;

&lt;p&gt;Now that you’re looking at all 100 senators, you have the choice of clicking one of the sorting buttons to re-sort the senators. Instead of mounting other components, I want these buttons to reshuffle the cards that are already on the page. So I want each button to call its corresponding action directly.&lt;/p&gt;

&lt;p&gt;To do that, I have to give each button the power to call that action. We could make each button its own component and have it go to the store to fetch its action, but there’s an easier way. We can pass superpowers down from SenatorsList to its children, each of the buttons! Here’s what they look like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2Aak5uRow6CI8ogZcK_HrxDA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2Aak5uRow6CI8ogZcK_HrxDA.png"&gt;&lt;/a&gt;inside the return method in SenatorsList&lt;/p&gt;

&lt;p&gt;Since these Buttons are being rendered inside of the SenatorsList component, they are children of SenatorsList, and SenatorsList can give each one the power it needs to perform its mission. Let’s see, the ‘Loyalists’ button will need to access this.props.fetchLoyalist, ‘Mavericks’ will need this.props.fetchMavericks, etc.&lt;/p&gt;

&lt;p&gt;Now, when each button is clicked, it will fire the action (stored in actions/senate.js), which in turn will tell the reducer which of its actions to fire …. then the reducer updates the store. Since SenatorsList can now access its state as props, it automatically updates to show its new state: a list of 100 senators sorted by (whichever button we clicked!)&lt;/p&gt;

&lt;p&gt;Here’s what the Loyalists sort looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AYijF_q0nm7BgM_I1CCEf6A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AYijF_q0nm7BgM_I1CCEf6A.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you’ve read this far, I hope this helped you understand React and Redux.&lt;/p&gt;

&lt;p&gt;Here is a link to the repos for this project: &lt;a href="https://github.com/joanrig/congress_front_end" rel="noopener noreferrer"&gt;Front end&lt;/a&gt; &amp;amp; the&lt;a href="https://github.com/joanrig/congress_api" rel="noopener noreferrer"&gt;back-end Rails API&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>railsapi</category>
      <category>react</category>
      <category>redux</category>
    </item>
  </channel>
</rss>
